From ccabaf76796b7c4a280ee913a2ed0f7a6ee65d3f Mon Sep 17 00:00:00 2001 From: Charles Severance Date: Sun, 22 Nov 2026 01:48:27 -0500 Subject: [PATCH 1/4] Add a one-shot script to drop the inbound registration table. Co-authored-by: Cursor --- admin/lti-maint/README.md | 18 ++ .../lti-maint/undo-registration-migration.php | 215 ++++++++++++++++++ 2 files changed, 233 insertions(+) create mode 100644 admin/lti-maint/undo-registration-migration.php diff --git a/admin/lti-maint/README.md b/admin/lti-maint/README.md index d5b57ca60..1613e8bd9 100644 --- a/admin/lti-maint/README.md +++ b/admin/lti-maint/README.md @@ -61,3 +61,21 @@ php backfill-guid.php --seconds=15 ``` `--table` is one of `lti_user`, `lti_context`, `lti_key`, or `lti_link`. Omit it to process all four. Each batch is its own transaction and holds locks on the updated rows until it commits. A later run skips rows that already have a guid. + +## When to run `undo-registration-migration.php` + +A short-lived inbound registration added `lti_registration` and five columns on `lti_key`: `lti_registration_id`, `deployment_status`, `deployment_approved_at`, `platform_guid`, and `dynamic_registration_unlocked_at`. `admin/upgrade.php` does not create them and does not drop them. This script drops the foreign key, then those columns, then the table. + +Run it once, from a checkout that no longer contains that registration code. A later `php admin/upgrade.php` on that checkout will not put the table back. Each statement auto-commits. A second run finds nothing left and exits. + +The script stops if `lti_registration` still has rows, if a key still points at a registration, or if one of those columns holds a value. Copy the issuer, client id, and URLs back onto `lti_key` and delete the registration rows first. The script does not copy them. + +From the site's Tsugi checkout: + +``` +cd admin/lti-maint +php undo-registration-migration.php +php undo-registration-migration.php apply +``` + +The first command is a dry run. `apply` prints a warning and waits ten seconds, then drops. diff --git a/admin/lti-maint/undo-registration-migration.php b/admin/lti-maint/undo-registration-migration.php new file mode 100644 index 000000000..8b90a133a --- /dev/null +++ b/admin/lti-maint/undo-registration-migration.php @@ -0,0 +1,215 @@ +dirroot) !== realpath(__DIR__ . '/../..') ) { + fwrite(STDERR, "dirroot is {$CFG->dirroot}, expected this checkout.\n"); + exit(1); +} + +$apply = false; +foreach ( array_slice($argv, 1) as $arg ) { + if ( $arg === 'apply' ) { + $apply = true; + continue; + } + if ( $arg === 'dry' || $arg === 'dry-run' || $arg === '--dry-run' ) { + continue; + } + fwrite(STDERR, "Unknown argument: {$arg}\n"); + fwrite(STDERR, "Usage: php undo-registration-migration.php [apply]\n"); + exit(1); +} + +LTIX::getConnection(); + +if ( $PDOX->inTransaction() ) { + fwrite(STDERR, "Refusing to run inside a transaction.\n"); + exit(1); +} + +$p = $CFG->dbprefix; +$keyTable = $p.'lti_key'; +$regTable = $p.'lti_registration'; +$fkName = $p.'lti_key_ibfk_1'; +$columns = array( + 'lti_registration_id', + 'deployment_status', + 'deployment_approved_at', + 'platform_guid', + 'dynamic_registration_unlocked_at', +); + +/** + * @param string $line + */ +function undo_registration_out($line) +{ + echo $line."\n"; + flush(); +} + +/** + * @param \Tsugi\Util\PDOX $PDOX + * @param string $sql + * @param array $params + * @return array|null + */ +function undo_registration_row($PDOX, $sql, $params = array()) +{ + $q = $PDOX->queryReturnError($sql, $params); + if ( ! $q->success ) { + fwrite(STDERR, $q->errorImplode."\n"); + exit(1); + } + $row = $q->fetch(\PDO::FETCH_ASSOC); + $q->closeCursor(); + if ( $row === false ) return null; + return $row; +} + +/** + * @param \Tsugi\Util\PDOX $PDOX + * @param string $sql + */ +function undo_registration_exec($PDOX, $sql) +{ + undo_registration_out($sql); + $q = $PDOX->queryReturnError($sql); + if ( ! $q->success ) { + fwrite(STDERR, $q->errorImplode."\n"); + exit(1); + } +} + +$regExists = $PDOX->metadata($regTable) !== false; +$keyExists = $PDOX->metadata($keyTable) !== false; +if ( ! $keyExists ) { + fwrite(STDERR, "{$keyTable} is missing.\n"); + exit(1); +} + +$present = array(); +foreach ( $columns as $column ) { + if ( $PDOX->columnExists($column, $keyTable) ) { + $present[] = $column; + } +} + +$fk = undo_registration_row( + $PDOX, + "SELECT CONSTRAINT_NAME + FROM information_schema.TABLE_CONSTRAINTS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = :TABLE_NAME + AND CONSTRAINT_NAME = :CONSTRAINT_NAME + AND CONSTRAINT_TYPE = 'FOREIGN KEY'", + array(':TABLE_NAME' => $keyTable, ':CONSTRAINT_NAME' => $fkName) +); +$fkExists = is_array($fk); + +$blockers = array(); +if ( $regExists ) { + $countRow = undo_registration_row($PDOX, "SELECT COUNT(*) AS n FROM {$regTable}"); + $regCount = (int) ($countRow['n'] ?? 0); + undo_registration_out("{$regTable} rows: {$regCount}"); + if ( $regCount > 0 ) { + $blockers[] = "{$regTable} has {$regCount} row(s). Copy issuer, client id, and URLs back onto {$keyTable}, delete those rows, then run this again."; + } +} else { + undo_registration_out("{$regTable} is already absent"); +} + +foreach ( $present as $column ) { + $countRow = undo_registration_row( + $PDOX, + "SELECT COUNT(*) AS n FROM {$keyTable} WHERE {$column} IS NOT NULL" + ); + $filled = (int) ($countRow['n'] ?? 0); + undo_registration_out("{$keyTable}.{$column}: present, {$filled} non-null"); + if ( $filled > 0 ) { + $blockers[] = "{$keyTable}.{$column} is set on {$filled} key(s). This script does not keep that value."; + } +} + +foreach ( $columns as $column ) { + if ( ! in_array($column, $present, true) ) { + undo_registration_out("{$keyTable}.{$column} is already absent"); + } +} + +if ( $fkExists ) { + undo_registration_out("foreign key {$fkName} is present"); +} else { + undo_registration_out("foreign key {$fkName} is already absent"); +} + +if ( count($blockers) > 0 ) { + foreach ( $blockers as $blocker ) { + fwrite(STDERR, $blocker."\n"); + } + exit(1); +} + +$work = $fkExists || count($present) > 0 || $regExists; +if ( ! $work ) { + undo_registration_out("Nothing to drop."); + exit(0); +} + +if ( ! $apply ) { + if ( $fkExists ) undo_registration_out("Would drop foreign key {$fkName}"); + foreach ( $present as $column ) { + undo_registration_out("Would drop {$keyTable}.{$column}"); + } + if ( $regExists ) undo_registration_out("Would drop table {$regTable}"); + undo_registration_out("Dry run. php admin/lti-maint/undo-registration-migration.php apply"); + exit(0); +} + +undo_registration_out("This IS NOT A DRILL. Dropping the registration table and columns in 10 seconds."); +sleep(10); + +if ( $fkExists ) { + undo_registration_exec($PDOX, "ALTER TABLE `{$keyTable}` DROP FOREIGN KEY `{$fkName}`"); +} +foreach ( $present as $column ) { + undo_registration_exec($PDOX, "ALTER TABLE `{$keyTable}` DROP COLUMN `{$column}`"); +} +if ( $regExists ) { + undo_registration_exec($PDOX, "DROP TABLE `{$regTable}`"); +} + +undo_registration_out("Done."); From a0c2ae11c7f02a2c57cbe8c49426304b336cdab2 Mon Sep 17 00:00:00 2001 From: Charles Severance Date: Sun, 22 Nov 2026 01:48:28 -0500 Subject: [PATCH 2/4] Clear a stale platform keyset when its URL changes, and send that URL through OIDC login. Co-authored-by: Cursor --- .../templates/Admin/key/key-detail.php | 13 ++++++++++++ lib/src/Core/LTIX.php | 20 +++++++++++++++++++ .../Services/Settings/DynamicRegistration.php | 5 +++++ lti/oidc_login.php | 2 +- 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/src/Controllers/templates/Admin/key/key-detail.php b/lib/src/Controllers/templates/Admin/key/key-detail.php index 9bcb70024..4315a82ab 100644 --- a/lib/src/Controllers/templates/Admin/key/key-detail.php +++ b/lib/src/Controllers/templates/Admin/key/key-detail.php @@ -70,6 +70,9 @@ $key_key = U::get($_POST,'key_key'); $deploy_key = U::get($_POST,'deploy_key'); +$old_keyset_url = null; +$saw_old_keyset = false; + // Check the complex validation if ( count($_POST) > 0 && U::get($_POST,'doUpdate') && !empty($key_id) ) { $row = $PDOX->rowDie( "SELECT * FROM {$CFG->dbprefix}lti_key @@ -87,6 +90,8 @@ $old_key_key = $row['key_key']; $old_deploy_key = $row['deploy_key']; + $old_keyset_url = $row['lms_keyset_url']; + $saw_old_keyset = true; $lms_issuer_val = array_key_exists('lms_issuer', $_POST) ? $_POST['lms_issuer'] : $row['lms_issuer']; $lms_client_val = array_key_exists('lms_client', $_POST) ? $_POST['lms_client'] : $row['lms_client']; $posted_secret = array_key_exists('secret', $_POST) ? $_POST['secret'] : $row['secret']; @@ -121,6 +126,14 @@ $query_fields, $allow_edit, $allow_delete); if ( $row === CrudForm::CRUD_FAIL || $row === CrudForm::CRUD_SUCCESS ) { + if ( $row === CrudForm::CRUD_SUCCESS && isset($_POST['doUpdate']) && $saw_old_keyset + && array_key_exists('lms_keyset_url', $_POST) ) { + $posted_keyset = trim((string) $_POST['lms_keyset_url']); + $stored_keyset = trim((string) $old_keyset_url); + if ( $posted_keyset !== $stored_keyset ) { + LTIX::clearKeysetCache($key_id); + } + } header('Location: '.$from_location); return; } diff --git a/lib/src/Core/LTIX.php b/lib/src/Core/LTIX.php index 0a03a4ffe..a4cb545aa 100644 --- a/lib/src/Core/LTIX.php +++ b/lib/src/Core/LTIX.php @@ -1151,6 +1151,26 @@ public static function displayNameFromClaim($claim) { return $retval; } + /** + * Drop a stored platform keyset. The next launch fetches it again. + * + * Call this when the keyset URL on the key changes. A launch's login + * timestamp and the cache fill itself are not URL changes. + * + * @param mixed $key_id + */ + public static function clearKeysetCache($key_id) { + global $PDOX, $CFG; + $id = (int) $key_id; + if ( $id < 1 || ! isset($PDOX) || ! $PDOX || ! isset($CFG) ) return; + $PDOX->queryDie( + "UPDATE {$CFG->dbprefix}lti_key + SET lms_cache_keyset = NULL, lms_cache_pubkey = NULL, lms_cache_kid = NULL + WHERE key_id = :ID", + array(':ID' => $id) + ); + } + /** * Load the data from our lti_ tables using one long LEFT JOIN * diff --git a/lib/src/Services/Settings/DynamicRegistration.php b/lib/src/Services/Settings/DynamicRegistration.php index 29d568f8b..d5fb01676 100644 --- a/lib/src/Services/Settings/DynamicRegistration.php +++ b/lib/src/Services/Settings/DynamicRegistration.php @@ -468,6 +468,11 @@ public static function run($user_id, $tsugi_key, $unlock_code, $openid_configura echo("Unable to store LMS platform configuration on the key\n"); return ''; } + $old_keyset = trim((string) U::get($row, 'lms_keyset_url')); + $new_keyset = trim((string) $jwks_uri); + if ( $old_keyset !== $new_keyset ) { + LTIX::clearKeysetCache($tsugi_key); + } error_log("Stored issuer $issuer / $client_id in lti_key table"); diff --git a/lti/oidc_login.php b/lti/oidc_login.php index 1cc288883..6895ae6e0 100644 --- a/lti/oidc_login.php +++ b/lti/oidc_login.php @@ -81,7 +81,7 @@ $issuer_key = $row['issuer_key']; $platform_public_key = $row['lti13_platform_pubkey']; $our_kid = $row['lti13_kid']; -$our_keyset_url = $row['lti13_keyset']; +$our_keyset_url = $row['lti13_keyset_url']; $our_keyset = $row['lti13_keyset']; $signature = \Tsugi\Core\LTIX::getBrowserSignature(); From 8359050bf93efcb284e59d2453d409bf0716ccc6 Mon Sep 17 00:00:00 2001 From: Charles Severance Date: Sun, 22 Nov 2026 02:04:48 -0500 Subject: [PATCH 3/4] Stop upgrade from dropping a live foreign-key index and from reporting identity columns that are already ascii. Co-authored-by: Cursor --- lib/src/Services/Lti/database.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/src/Services/Lti/database.php b/lib/src/Services/Lti/database.php index ff5959ad1..e048a6251 100644 --- a/lib/src/Services/Lti/database.php +++ b/lib/src/Services/Lti/database.php @@ -1817,9 +1817,28 @@ // The unique guid key is added later in this upgrade. This pass must not // drop it when the table already has it. $keep_guid_index = $CFG->dbprefix."lti_key_const_guid"; + // InnoDB names a foreign key's supporting index after the constraint. + // DROP INDEX fails while that constraint still exists. + $foreign_key_indexes = array(); + $foreign_key_rows = $PDOX->allRows( + "SELECT CONSTRAINT_NAME AS constraint_name + FROM information_schema.TABLE_CONSTRAINTS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = :table_name + AND CONSTRAINT_TYPE = 'FOREIGN KEY'", + array(':table_name' => $CFG->dbprefix."lti_key") + ); + if ( is_array($foreign_key_rows) ) { + foreach ( $foreign_key_rows as $foreign_key_row ) { + $foreign_key_name = \Tsugi\Util\U::get($foreign_key_row, 'constraint_name'); + if ( ! is_string($foreign_key_name) || $foreign_key_name === '' ) continue; + $foreign_key_indexes[$foreign_key_name] = true; + } + } foreach($indexes as $index) { if ( strcasecmp($index, "PRIMARY") == 0 ) continue; if ( strcasecmp($index, "ibfk") == 0 ) continue; + if ( isset($foreign_key_indexes[$index]) ) continue; // lti_key_const_2 backs issuer_id until lti_issuer phase 3 (2026-10-01 UTC) if ( $keep_issuer_index && $index === $CFG->dbprefix."lti_key_const_2" ) continue; if ( $index === $keep_guid_index ) continue; @@ -2863,14 +2882,13 @@ // One column at a time. Count values longer than 255 characters and values // with a character outside ASCII. Alter only when both counts are zero and // the column is not already varchar(255) ascii_bin. A column already in - // that form is reported and left alone. A dirty column is reported and + // that form is left alone with no message. A dirty column is reported and // left alone so a later upgrade can alter it after the data is cleaned. $identity_note = function($message) { error_log($message); echo(htmlentities($message)."
\n"); }; $identity_ascii_columns = \Tsugi\Services\Admin\AdminService::identityAsciiColumns(); - $identity_note('Checking identity columns for varchar(255) character set ascii collation ascii_bin.'); $identity_described = array(); foreach ( $identity_ascii_columns as $identity_column ) { $identity_table = $identity_column[0]; @@ -2955,7 +2973,6 @@ continue; } if ( $identity_already ) { - $identity_note($identity_summary.' Already varchar(255) character set ascii collation ascii_bin.'); continue; } $identity_nullable = strtoupper((string) \Tsugi\Util\U::getIgnoreCase($identity_field_meta, 'Null')) === 'YES'; From 19029517d854546abbf543de34ea63eddbedd12c Mon Sep 17 00:00:00 2001 From: Charles Severance Date: Sun, 22 Nov 2026 02:25:00 -0500 Subject: [PATCH 4/4] Leave the undo script database handle untyped so PHPStan accepts its error checks. Co-authored-by: Cursor --- admin/lti-maint/undo-registration-migration.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/admin/lti-maint/undo-registration-migration.php b/admin/lti-maint/undo-registration-migration.php index 8b90a133a..7e703f13c 100644 --- a/admin/lti-maint/undo-registration-migration.php +++ b/admin/lti-maint/undo-registration-migration.php @@ -82,7 +82,6 @@ function undo_registration_out($line) } /** - * @param \Tsugi\Util\PDOX $PDOX * @param string $sql * @param array $params * @return array|null @@ -101,7 +100,6 @@ function undo_registration_row($PDOX, $sql, $params = array()) } /** - * @param \Tsugi\Util\PDOX $PDOX * @param string $sql */ function undo_registration_exec($PDOX, $sql)