From fe6b316bbfab98e198b5f9f7bb7cb56376a064fa Mon Sep 17 00:00:00 2001 From: Charles Severance Date: Sun, 22 Nov 2026 17:43:58 -0500 Subject: [PATCH] Add exact-byte unique indexes for context, link, and resource link ids. Co-authored-by: Cursor --- lib/src/Services/Admin/AdminService.php | 6 ++++++ lib/src/Services/Lti/database.php | 21 +++++++++++++++++-- .../Admin/AdminServiceNaturalKeyTest.php | 12 +++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/src/Services/Admin/AdminService.php b/lib/src/Services/Admin/AdminService.php index f03801fd2..b7fece7a8 100644 --- a/lib/src/Services/Admin/AdminService.php +++ b/lib/src/Services/Admin/AdminService.php @@ -919,6 +919,9 @@ public static function identityAsciiColumns() { * 254 characters, so 255 would reject a legal value. 512 covers it. * service_key is lis_outcome_service_url. 1024. A COMPACT index holds 767 bytes, * and 1024 plus key_id does not fit, so that alter waits until the table is DYNAMIC. + * context_key, link_key, and resource_link_id are varchar(255) and NOT NULL. + * Their uniques match the sha256 uniques: (key_id, context_key), + * (link_key, context_id), and (context_id, resource_link_id). * string_text stays TEXT. A translation string is not one of these columns. * * The fifth value is the unique constraint name without the database prefix. @@ -935,6 +938,9 @@ public static function naturalKeyAsciiColumns() { array('lti_user', 'user_key', 'user_id', 512, 'lti_user_const_user_key', array('key_id', 'user_key_bin'), true), array('profile', 'profile_key', 'profile_id', 512, 'profile_const_key', array('profile_key_bin'), false), array('lti_service', 'service_key', 'service_id', 1024, 'lti_service_const_key', array('key_id', 'service_key_bin'), false), + array('lti_context', 'context_key', 'context_id', 255, 'lti_context_const_context_key', array('key_id', 'context_key_bin'), false), + array('lti_link', 'link_key', 'link_id', 255, 'lti_link_const_link_key', array('link_key_bin', 'context_id'), false), + array('lti_content', 'resource_link_id', 'content_id', 255, 'lti_content_const_resource_link', array('context_id', 'resource_link_id_bin'), false), ); } diff --git a/lib/src/Services/Lti/database.php b/lib/src/Services/Lti/database.php index 0cf650ecc..f1d89a6b9 100644 --- a/lib/src/Services/Lti/database.php +++ b/lib/src/Services/Lti/database.php @@ -296,6 +296,8 @@ previous_context_id INTEGER NULL, context_sha256 CHAR(64) NOT NULL, context_key VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + -- ascii_bin ignores trailing spaces. This is the exact bytes, for the unique index. + context_key_bin VARBINARY(255) GENERATED ALWAYS AS (CAST(context_key AS BINARY)) STORED, deleted TINYINT(1) NOT NULL DEFAULT 0, secret VARCHAR(128) NULL, @@ -381,6 +383,7 @@ ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `{$CFG->dbprefix}lti_context_const_1` UNIQUE(key_id, context_sha256), + CONSTRAINT `{$CFG->dbprefix}lti_context_const_context_key` UNIQUE(key_id, context_key_bin), -- Deployment scope and registration ownership reference (context_id, key_id) -- so a course link cannot leave this tenant key. CONSTRAINT `{$CFG->dbprefix}lti_context_const_2` UNIQUE(context_id, key_id), @@ -504,6 +507,8 @@ link_id INTEGER NOT NULL AUTO_INCREMENT, link_sha256 CHAR(64) NOT NULL, link_key VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + -- ascii_bin ignores trailing spaces. This is the exact bytes, for the unique index. + link_key_bin VARBINARY(255) GENERATED ALWAYS AS (CAST(link_key AS BINARY)) STORED, deleted TINYINT(1) NOT NULL DEFAULT 0, context_id INTEGER NOT NULL, @@ -586,6 +591,7 @@ ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `{$CFG->dbprefix}lti_link_const_1` UNIQUE(link_sha256, context_id), + CONSTRAINT `{$CFG->dbprefix}lti_link_const_link_key` UNIQUE(link_key_bin, context_id), CONSTRAINT `{$CFG->dbprefix}lti_link_const_guid` UNIQUE (`guid`), CONSTRAINT `{$CFG->dbprefix}lti_link_const_pk` PRIMARY KEY (link_id) ) ENGINE = InnoDB DEFAULT CHARSET=utf8"), @@ -1205,6 +1211,8 @@ custom JSON NULL, resource_link_id VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + -- ascii_bin ignores trailing spaces. This is the exact bytes, for the unique index. + resource_link_id_bin VARBINARY(255) GENERATED ALWAYS AS (CAST(resource_link_id AS BINARY)) STORED, -- lti_sha256(resource_link_id). The same value as lti_link.link_sha256 -- when the grade column is created. resource_link_sha256 CHAR(64) NOT NULL, @@ -1229,6 +1237,7 @@ CONSTRAINT `{$CFG->dbprefix}lti_content_const_1` UNIQUE (link_id), CONSTRAINT `{$CFG->dbprefix}lti_content_const_2` UNIQUE (context_id, resource_link_sha256), + CONSTRAINT `{$CFG->dbprefix}lti_content_const_resource_link` UNIQUE (context_id, resource_link_id_bin), CONSTRAINT `{$CFG->dbprefix}lti_content_chk_1` CHECK ( target IN ('window', 'iframe', 'inline') ), @@ -3108,7 +3117,8 @@ } // End of the November 2026 inbound registration detour. - // user_key, subject_key, profile_key, and service_key. Widths are per column. + // user_key, subject_key, profile_key, service_key, context_key, link_key, + // and resource_link_id. Widths are per column. // A value longer than that width, or a character outside ASCII, leaves the // column alone. COMPACT indexes 767 bytes. service_key is varchar(1024), // so the column alter waits until the table row format is DYNAMIC. @@ -3414,11 +3424,18 @@ $natural_groups = (int) \Tsugi\Util\U::get($natural_group_rows[0], 'duplicate_groups', 0); $natural_group_word = $natural_groups === 1 ? 'group' : 'groups'; if ( $natural_groups > 0 ) { + $natural_table_has_key_id = false; + foreach ( $natural_described[$natural_physical] as $natural_key_row ) { + if ( \Tsugi\Util\U::getIgnoreCase($natural_key_row, 'Field') === 'key_id' ) { + $natural_table_has_key_id = true; + break; + } + } $natural_has_key_id = in_array('key_id', $natural_unique_columns, true); $natural_example_select = "MIN({$natural_id}) AS example_id, COUNT(*) AS n"; if ( $natural_has_key_id ) { $natural_example_select .= ', key_id'; - } else { + } else if ( $natural_table_has_key_id ) { $natural_example_select .= ', MIN(key_id) AS key_id, MAX(key_id) AS key_id_max'; } $natural_dup_examples = $PDOX->allRows( diff --git a/lib/tests/Services/Admin/AdminServiceNaturalKeyTest.php b/lib/tests/Services/Admin/AdminServiceNaturalKeyTest.php index f54d66fa4..e6cfe12f0 100644 --- a/lib/tests/Services/Admin/AdminServiceNaturalKeyTest.php +++ b/lib/tests/Services/Admin/AdminServiceNaturalKeyTest.php @@ -16,6 +16,9 @@ public function testAsciiWithinTheLimitIsAccepted() { public function testNullIsRefusedWhenTheColumnRequiresAValue() { $this->assertSame('profile_key has no value.', AdminService::naturalKeyProblem('profile_key', null)); $this->assertSame('service_key has no value.', AdminService::naturalKeyProblem('service_key', null)); + $this->assertSame('context_key has no value.', AdminService::naturalKeyProblem('context_key', null)); + $this->assertSame('link_key has no value.', AdminService::naturalKeyProblem('link_key', null)); + $this->assertSame('resource_link_id has no value.', AdminService::naturalKeyProblem('resource_link_id', null)); } public function testACharacterOutsideAsciiIsRefused() { @@ -39,6 +42,15 @@ public function testAValuePastTheLimitIsRefused() { 'service_key is 1025 characters and the limit is 1024.', AdminService::naturalKeyProblem('service_key', str_repeat('c', 1025)) ); + $this->assertNull(AdminService::naturalKeyProblem('context_key', str_repeat('d', 255))); + $this->assertSame( + 'link_key is 256 characters and the limit is 255.', + AdminService::naturalKeyProblem('link_key', str_repeat('d', 256)) + ); + $this->assertSame( + 'resource_link_id is 256 characters and the limit is 255.', + AdminService::naturalKeyProblem('resource_link_id', str_repeat('e', 256)) + ); } public function testANonStringIsRefused() {