diff --git a/.coderabbit.yaml b/.coderabbit.yaml index afc8b2182..46ccd2687 100644 --- a/.coderabbit.yaml +++ b/.coderabbit.yaml @@ -91,7 +91,6 @@ reviews: tools: phpstan: enabled: true - config_file: "phpstan.neon" phpcs: enabled: false phpmd: diff --git a/.cursor/rules/ask-before-commit.mdc b/.cursor/rules/ask-before-commit.mdc new file mode 100644 index 000000000..dde308d2f --- /dev/null +++ b/.cursor/rules/ask-before-commit.mdc @@ -0,0 +1,12 @@ +--- +description: Ask before any commit or push +alwaysApply: true +--- + +# Ask before commit or push + +Do not run `git commit`, `git push`, or open a pull request at the end of other work. + +When the work is ready, stop and ask. Commit, push, or open a pull request only after an explicit approval in that same conversation. Approval sounds like "commit this", "push it", or "open the PR". Finishing the task is not approval. + +"Commit and push" and "open the PR" are approval for that request. Do not wait for a second confirmation. diff --git a/AGENTS.md b/AGENTS.md index e5354fa8f..430d5aa27 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -58,6 +58,7 @@ File-based lessons (`$CFG->lessons`, no `manifest_id`) must keep working. - **App Store test harness (no login):** anything under `mod/` and anything under `tool/` can be launched from the store without signing in. Start at `$CFG->wwwroot/tool` (redirects to the store) or go directly to `$CFG->wwwroot/store/test/{name}` (for example `/tsugi/store/test/trophy`). Use **Try It**, pick an identity (Jane Instructor or a student), and exercise the tool in the iframe. Some instructor views (Trophy is one) show a launch var dump — that is expected, not a broken page. Do not assume a site login is required for this path. ## Commit & Pull Request Guidelines +- Do not commit, push, or open a pull request when a task is finished, unless this conversation already approved it. Stop and ask when that approval is absent. Approval sounds like "commit this", "push it", or "open the PR". "Commit and push" and "open the PR" are approval for that request. Do not wait for a second confirmation. See `.cursor/rules/ask-before-commit.mdc`. - Recent commits are short, sentence-style summaries (e.g., “Add item to headers.”). Keep messages concise and action-oriented. - PRs go through GitHub and require a CLA before acceptance (see `CONTRIBUTING.md`). - If you touch vendored libraries in `vendor/`, include clear notes in the PR so maintainers can propagate changes across related repos. diff --git a/lib/include/setup.php b/lib/include/setup.php index db509d2b7..a3a1c2980 100644 --- a/lib/include/setup.php +++ b/lib/include/setup.php @@ -10,7 +10,7 @@ // upgrade checking - don't change this unless you want to trigger // database upgrade messages it should be the max of all versions in // all database.php files. -$CFG->dbversion = 202611200002; +$CFG->dbversion = 202611220002; function die_with_error_log($msg, $extra=false, $prefix="DIE:") { error_log($prefix.' '.$msg.' '.$extra); @@ -177,7 +177,7 @@ function isCli() { $LINK = false; if (!defined('TSUGI_VERSION')) { - define('TSUGI_VERSION', '26.11.7'); // ← update this occasionally + define('TSUGI_VERSION', '26.11.9'); // ← update this occasionally } if (!defined('TSUGI_MINIMUM_PHP')) { diff --git a/lib/src/Core/LTIX.php b/lib/src/Core/LTIX.php index a4cb545aa..44beadfa8 100644 --- a/lib/src/Core/LTIX.php +++ b/lib/src/Core/LTIX.php @@ -20,6 +20,7 @@ use \Tsugi\Crypt\AesOpenSSL; use \Tsugi\Services\Plus\GradeSync; use \Tsugi\Services\Plus\PlusShell; +use \Tsugi\Services\Admin\AdminService; use \Firebase\JWT\JWT; @@ -1472,6 +1473,7 @@ public static function adjustData($p, &$row, $post, $needed) { // $post['user_subject'] is the user_subject from the launch // $row['subject_key'] is the subject from the row if ( $row['user_id'] === null && isset($post['user_id']) && U::isNotEmpty($post['user_id']) ) { + self::requireNaturalKey('user_key', $post['user_id']); $sql = "INSERT INTO {$p}lti_user /*PDOX pk: user_id lk: user_sha256,key_id */ ( guid, user_key, user_sha256, displayname, email, image, locale, key_id, created_at, updated_at ) VALUES @@ -1497,24 +1499,29 @@ public static function adjustData($p, &$row, $post, $needed) { // TODO: Combine this into the previous query after user_subject migrations run - 28-May-2019 if ( U::isNotEmpty($post_user_subject) ) { - $sql = "UPDATE {$p}lti_user SET - subject_key = :subject_key, - subject_sha256 = :subject_sha256, - updated_at = NOW() - WHERE user_id = :UID"; - - $stmt = $PDOX->queryReturnError($sql, array( - ':UID' => $row['user_id'], - ':subject_key' => $post_user_subject, - ':subject_sha256' => lti_sha256($post_user_subject), - ) - ); - - if ( $stmt->success ) { - $row['subject_key'] = $post_user_subject; - $actions[] = "=== Added subject to new user id=".$row['user_id']." ".$row['user_email']; + $subject_problem = AdminService::naturalKeyProblem('subject_key', $post_user_subject); + if ( $subject_problem !== null ) { + error_log($subject_problem); } else { - error_log("Unable to update user_subject - please upgrade database email=".$row['user_email']); + $sql = "UPDATE {$p}lti_user SET + subject_key = :subject_key, + subject_sha256 = :subject_sha256, + updated_at = NOW() + WHERE user_id = :UID"; + + $stmt = $PDOX->queryReturnError($sql, array( + ':UID' => $row['user_id'], + ':subject_key' => $post_user_subject, + ':subject_sha256' => lti_sha256($post_user_subject), + ) + ); + + if ( $stmt->success ) { + $row['subject_key'] = $post_user_subject; + $actions[] = "=== Added subject to new user id=".$row['user_id']." ".$row['user_email']; + } else { + error_log("Unable to update user_subject - please upgrade database email=".$row['user_email']); + } } } } @@ -1523,6 +1530,8 @@ public static function adjustData($p, &$row, $post, $needed) { $lti11_transition_user_id = isset($post['lti11_transition_user_id']) ? $post['lti11_transition_user_id'] : null; $lti11_transition_user_id_sha256 = $lti11_transition_user_id === null ? null : lti_sha256($lti11_transition_user_id); if ( $row['user_id'] === null && U::isNotEmpty($post_user_subject) ) { + self::requireNaturalKey('subject_key', $post_user_subject); + self::requireNaturalKey('user_key', $lti11_transition_user_id); $sql = "INSERT INTO {$p}lti_user /*PDOX pk: user_id lk: subject_sha256,user_sha256,key_id */ ( guid, user_key, user_sha256, subject_key, subject_sha256, displayname, email, image, locale, key_id, created_at, updated_at ) VALUES @@ -1554,22 +1563,27 @@ public static function adjustData($p, &$row, $post, $needed) { // Note that we will forever check for errors because of duplicate key possibilities if ( $row['user_id'] > 0 && U::isNotEmpty($post_user_subject) && U::isNotEmpty($lti11_transition_user_id) && $row['user_key'] != $lti11_transition_user_id) { - $sql = "UPDATE {$p}lti_user - SET user_key = :user_key, user_sha256 = :user_sha256, updated_at = NOW() - WHERE user_id = :uid"; - $stmt = $PDOX->queryReturnError($sql, array( - ':uid' => $row['user_id'], - ':user_key' => $lti11_transition_user_id, - ':user_sha256' => $lti11_transition_user_id_sha256, - ) - ); - - if ( $stmt->success ) { - $row['user_key'] = $lti11_transition_user_id; - $actions[] = "=== Updated user_key for id=".$row['user_id']." subject=".$post_user_subject. - " transition_user_uid=".$lti11_transition_user_id; + $user_key_problem = AdminService::naturalKeyProblem('user_key', $lti11_transition_user_id); + if ( $user_key_problem !== null ) { + error_log($user_key_problem); } else { - error_log("Unable to update user_key - transition problem subject=$post_user_subject user_id=".$row['user_id']); + $sql = "UPDATE {$p}lti_user + SET user_key = :user_key, user_sha256 = :user_sha256, updated_at = NOW() + WHERE user_id = :uid"; + $stmt = $PDOX->queryReturnError($sql, array( + ':uid' => $row['user_id'], + ':user_key' => $lti11_transition_user_id, + ':user_sha256' => $lti11_transition_user_id_sha256, + ) + ); + + if ( $stmt->success ) { + $row['user_key'] = $lti11_transition_user_id; + $actions[] = "=== Updated user_key for id=".$row['user_id']." subject=".$post_user_subject. + " transition_user_uid=".$lti11_transition_user_id; + } else { + error_log("Unable to update user_key - transition problem subject=$post_user_subject user_id=".$row['user_id']); + } } } @@ -1577,20 +1591,25 @@ public static function adjustData($p, &$row, $post, $needed) { // Always check and log errors in case the transition went badly and then was reconfigured $row_subject_key = U::get($row, 'subject_key'); if ( $row['user_id'] > 0 && U::isNotEmpty($post_user_subject) && $post_user_subject != $row_subject_key ) { - $sql = "UPDATE {$p}lti_user - SET subject_key = :subject_key, subject_sha256 = :subject_sha256, updated_at = NOW() - WHERE user_id = :uid"; - $stmt = $PDOX->queryReturnError($sql, array( - ':uid' => $row['user_id'], - ':subject_key' => $post_user_subject, - ':subject_sha256' => lti_sha256($post_user_subject), - ) - ); - if ( $stmt->success ) { - $row['subject_key'] = $post_user_subject; - $actions[] = "=== Updated subject_key for id=".$row['user_id']." post_user_subject=".$post_user_subject; + $subject_problem = AdminService::naturalKeyProblem('subject_key', $post_user_subject); + if ( $subject_problem !== null ) { + error_log($subject_problem); } else { - error_log("Unable to update subject_key - transition problems post_user_subject=".$post_user_subject." user_id=".$row['user_id']); + $sql = "UPDATE {$p}lti_user + SET subject_key = :subject_key, subject_sha256 = :subject_sha256, updated_at = NOW() + WHERE user_id = :uid"; + $stmt = $PDOX->queryReturnError($sql, array( + ':uid' => $row['user_id'], + ':subject_key' => $post_user_subject, + ':subject_sha256' => lti_sha256($post_user_subject), + ) + ); + if ( $stmt->success ) { + $row['subject_key'] = $post_user_subject; + $actions[] = "=== Updated subject_key for id=".$row['user_id']." post_user_subject=".$post_user_subject; + } else { + error_log("Unable to update subject_key - transition problems post_user_subject=".$post_user_subject." user_id=".$row['user_id']); + } } } @@ -1616,6 +1635,7 @@ public static function adjustData($p, &$row, $post, $needed) { // This is for LTI 1.x only as service is not used for LTI 2.x $oldserviceid = $row['service_id']; if ( $row['service_id'] === null && $post['service'] ) { + self::requireNaturalKey('service_key', $post['service']); $sql = "INSERT INTO {$p}lti_service ( service_key, service_sha256, key_id, created_at, updated_at ) VALUES ( :service_key, :service_sha256, :key_id, NOW(), NOW() ) @@ -3066,6 +3086,22 @@ public static function noteLoggedIn($row) } } + /** + * Stop before the insert when a natural key cannot be stored. + * The column would reject it, and the launch would die as a database failure. + * + * @param string $column + * @param mixed $value + */ + private static function requireNaturalKey($column, $value) { + $problem = AdminService::naturalKeyProblem($column, $value); + if ( $problem === null ) { + return; + } + error_log($problem); + die($problem); + } + /** * populateRoster * @@ -3112,11 +3148,20 @@ public static function populateRoster($groups=false, $insert=false) { $key = $PDOX->rowDie("SELECT key_id FROM {$CFG->dbprefix}lti_key WHERE key_key = :key", array(':key' => $TSUGI_LAUNCH->context->key)); - if (! $key ) return false; + if (! $key ) { + $PDOX->setAttribute(\PDO::ATTR_ERRMODE, $errormode); + return false; + } $insertUserArray = []; $insertUserMembershipArray = []; foreach($ROSTER->data as $user) { + $user_key_problem = AdminService::naturalKeyProblem('user_key', isset($user['user_id']) ? $user['user_id'] : null); + if ( $user_key_problem !== null ) { + error_log($user_key_problem); + $PDOX->setAttribute(\PDO::ATTR_ERRMODE, $errormode); + return false; + } array_push($insertUserArray, "(UNHEX('".Guid::uuid7()."'), '". $user['user_id'] ."', '". lti_sha256($user['user_id']) ."', '". diff --git a/lib/src/Services/Admin/AdminService.php b/lib/src/Services/Admin/AdminService.php index 9e39a4d20..f03801fd2 100644 --- a/lib/src/Services/Admin/AdminService.php +++ b/lib/src/Services/Admin/AdminService.php @@ -911,6 +911,91 @@ public static function identityAsciiColumns() { ); } + /** + * Natural keys the upgrade narrows to ascii_bin. The fourth value is the varchar length. + * + * subject_key is an LTI 1.3 sub, so it matches the other identity columns at 255. + * user_key and profile_key also store googlemail: plus an email. An email can be + * 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. + * 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. + * The sixth is the unique column list. ascii_bin ignores trailing spaces, so the + * unique is on a generated VARBINARY column of the exact bytes, named column_bin. + * The seventh value is true when the varchar accepts NULL. + * NULL user_key and NULL subject_key stay allowed more than once per key. + * + * @return array,6:bool}> + */ + public static function naturalKeyAsciiColumns() { + return array( + array('lti_user', 'subject_key', 'user_id', 255, 'lti_user_const_subject_key', array('key_id', 'subject_key_bin'), true), + 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), + ); + } + + /** + * Why this value cannot be stored in a natural-key column, or null when it can. + * + * Null is allowed for user_key and subject_key. A leading or trailing space + * is rejected, and so is a non-printing character, same as an issuer or client id. + * A space in the middle is kept. The value itself is left out of the message, + * because user_key and profile_key can be an email address. + * + * @param string $column + * @param mixed $value + * @return string|null + */ + public static function naturalKeyProblem($column, $value) { + $spec = null; + foreach ( self::naturalKeyAsciiColumns() as $natural ) { + if ( $natural[1] === $column ) { + $spec = $natural; + break; + } + } + if ( $spec === null ) { + return $column.' is not a natural key column.'; + } + $width = (int) $spec[3]; + $nullable = ! empty($spec[6]); + if ( $value === null ) { + return $nullable ? null : $column.' has no value.'; + } + if ( ! is_string($value) ) { + return $column.' is not text.'; + } + $reasons = array(); + if ( preg_match('/[^\x00-\x7F]/', $value) === 1 ) { + $reasons[] = 'has a character outside ASCII'; + } + $length = strlen($value); + if ( $reasons && function_exists('mb_strlen') ) { + $counted = mb_strlen($value, 'UTF-8'); + if ( is_int($counted) ) $length = $counted; + } + if ( $length > $width ) { + $reasons[] = 'is '.$length.' characters and the limit is '.$width; + } + if ( preg_match('/^ /', $value) === 1 ) { + $reasons[] = 'has a leading space'; + } + if ( preg_match('/ $/', $value) === 1 ) { + $reasons[] = 'has a trailing space'; + } + if ( preg_match('/[\x00-\x1F\x7F]/', $value) === 1 ) { + $reasons[] = 'contains a non-printing character'; + } + if ( count($reasons) < 1 ) return null; + if ( count($reasons) === 1 ) return $column.' '.$reasons[0].'.'; + $last = array_pop($reasons); + return $column.' '.implode(', ', $reasons).' and '.$last.'.'; + } + /** * Identity columns that are not yet varchar(255) character set ascii collation ascii_bin. * A missing table or column is included. Names are table.column without the database prefix. diff --git a/lib/src/Services/Login/GoogleLoginService.php b/lib/src/Services/Login/GoogleLoginService.php index 8ecc4840d..2a9573241 100644 --- a/lib/src/Services/Login/GoogleLoginService.php +++ b/lib/src/Services/Login/GoogleLoginService.php @@ -9,6 +9,7 @@ use \Tsugi\Core\Cache; use \Tsugi\Core\User; use \Tsugi\Controllers\Login; +use Tsugi\Services\Admin\AdminService; use Tsugi\Services\Permissions\PermissionTables; /** @@ -146,6 +147,12 @@ public static function establishSiteSession($user_key, $userEmail, $displayName, global $CFG, $PDOX; $result = new GoogleLoginResult(); + $user_problem = AdminService::naturalKeyProblem('user_key', $user_key); + $profile_problem = AdminService::naturalKeyProblem('profile_key', $user_key); + if ( $user_problem !== null || $profile_problem !== null ) { + $result->error = $user_problem !== null ? $user_problem : $profile_problem; + return $result; + } if ( session_id() == "" ) { session_start(); } diff --git a/lib/src/Services/Lti/database.php b/lib/src/Services/Lti/database.php index 388e5dbb5..0cf650ecc 100644 --- a/lib/src/Services/Lti/database.php +++ b/lib/src/Services/Lti/database.php @@ -170,9 +170,16 @@ "create table {$CFG->dbprefix}lti_user ( user_id INTEGER NOT NULL AUTO_INCREMENT, user_sha256 CHAR(64) NULL, - user_key TEXT NULL, + -- LTI 1.1 user_id, the 1.1 transition id, or googlemail: plus an email. + -- 512 covers a 254-character address. The hash stays until a later unique index. + user_key VARCHAR(512) CHARACTER SET ascii COLLATE ascii_bin NULL, + -- ascii_bin ignores trailing spaces. This is the exact bytes, for the unique index. + -- MariaDB 10.5 rejects NULL or NOT NULL after STORED. Nullability follows the expression. + user_key_bin VARBINARY(512) GENERATED ALWAYS AS (CAST(user_key AS BINARY)) STORED, subject_sha256 CHAR(64) NULL, - subject_key TEXT NULL, + -- LTI 1.3 sub. + subject_key VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NULL, + subject_key_bin VARBINARY(255) GENERATED ALWAYS AS (CAST(subject_key AS BINARY)) STORED, deleted TINYINT(1) NOT NULL DEFAULT 0, key_id INTEGER NOT NULL, @@ -219,6 +226,10 @@ CONSTRAINT `{$CFG->dbprefix}lti_user_const_1` UNIQUE(key_id, user_sha256), CONSTRAINT `{$CFG->dbprefix}lti_user_const_2` UNIQUE(key_id, subject_sha256), + -- Parallel to the sha256 uniques, on the exact bytes. NULL user_key and + -- NULL subject_key still allow more than one row on a key. + CONSTRAINT `{$CFG->dbprefix}lti_user_const_user_key` UNIQUE(key_id, user_key_bin), + CONSTRAINT `{$CFG->dbprefix}lti_user_const_subject_key` UNIQUE(key_id, subject_key_bin), CONSTRAINT `{$CFG->dbprefix}lti_user_const_guid` UNIQUE (`guid`), INDEX `{$CFG->dbprefix}lti_user_indx_email` (email(255)), INDEX `{$CFG->dbprefix}lti_user_indx_profile` (profile_id), @@ -226,7 +237,7 @@ -- UNIQUE(key_id, user_sha256) walks every user on that key. INDEX `{$CFG->dbprefix}lti_user_indx_key_email` (key_id, email(255), updated_at), CONSTRAINT `{$CFG->dbprefix}lti_user_const_pk` PRIMARY KEY (user_id) -) ENGINE = InnoDB DEFAULT CHARSET=utf8"), +) ENGINE = InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC"), /* If MySQL had CHECK constraints - these would be nice in lti_user - for now we will just need to be careful in code. @@ -706,7 +717,11 @@ "create table {$CFG->dbprefix}lti_service ( service_id INTEGER NOT NULL AUTO_INCREMENT, service_sha256 CHAR(64) NOT NULL, - service_key TEXT NOT NULL, + -- lis_outcome_service_url. 1024 ascii needs a DYNAMIC table: a COMPACT + -- index holds 767 bytes, and this column plus key_id does not fit. + service_key VARCHAR(1024) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + -- ascii_bin ignores trailing spaces. This is the exact bytes, for the unique index. + service_key_bin VARBINARY(1024) GENERATED ALWAYS AS (CAST(service_key AS BINARY)) STORED, deleted TINYINT(1) NOT NULL DEFAULT 0, key_id INTEGER NOT NULL, @@ -725,8 +740,9 @@ ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `{$CFG->dbprefix}lti_service_const_1` UNIQUE(key_id, service_sha256), + CONSTRAINT `{$CFG->dbprefix}lti_service_const_key` UNIQUE(key_id, service_key_bin), CONSTRAINT `{$CFG->dbprefix}lti_service_const_pk` PRIMARY KEY (service_id) -) ENGINE = InnoDB DEFAULT CHARSET=utf8"), +) ENGINE = InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC"), // service_id/sourcedid are for LTI 1.x // result_url is for LTI 2.x @@ -3092,9 +3108,370 @@ } // End of the November 2026 inbound registration detour. + // user_key, subject_key, profile_key, and service_key. 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. + // A Compact, Redundant, or Compressed table is rebuilt. A failed rebuild + // leaves the column. While these columns are rolling out, a table that is + // already DYNAMIC and a column that is already ascii_bin are reported too. + // Drop those already-done lines once every database has been converted. + // After the column is ascii_bin, a generated VARBINARY column holds the + // exact bytes. ascii_bin ignores trailing spaces, so the unique constraint + // is on that generated column, parallel to the sha256 unique. An older + // unique on the varchar itself is replaced. The sha256 unique stays. + // NULL user_key and NULL subject_key still allow more than one row on a + // key, same as the hash unique. A column that is already ascii_bin skips + // the length and ASCII scan. + $natural_row_ready = array(); + $natural_format = function($physical) use ($PDOX) { + $rows = $PDOX->allRows( + "SELECT ENGINE AS engine_name, ROW_FORMAT AS row_format, CREATE_OPTIONS AS create_options + FROM information_schema.TABLES + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = :table_name", + array(':table_name' => $physical), + false + ); + if ( ! is_array($rows) || ! isset($rows[0]) ) { + return array('engine' => '', 'format' => '', 'options' => ''); + } + return array( + 'engine' => strtolower(trim((string) \Tsugi\Util\U::getIgnoreCase($rows[0], 'engine_name'))), + 'format' => strtolower(trim((string) \Tsugi\Util\U::getIgnoreCase($rows[0], 'row_format'))), + 'options' => strtolower(trim((string) \Tsugi\Util\U::getIgnoreCase($rows[0], 'create_options'))), + ); + }; + $natural_ensure_dynamic = function($physical) use ($PDOX, $identity_note, $natural_format, &$natural_row_ready) { + if ( array_key_exists($physical, $natural_row_ready) ) { + return $natural_row_ready[$physical]; + } + $shape = $natural_format($physical); + if ( $shape['engine'] !== '' && $shape['engine'] !== 'innodb' ) { + $identity_note('Table '.$physical.' engine is '.$shape['engine'].'. Columns left unchanged.'); + $natural_row_ready[$physical] = false; + return false; + } + if ( $shape['format'] === 'dynamic' ) { + $identity_note('Table '.$physical.' row format is DYNAMIC.'); + $natural_row_ready[$physical] = true; + return true; + } + $shown = $shape['format'] === '' ? 'unknown' : $shape['format']; + $row_sql = "ALTER TABLE {$physical} ROW_FORMAT=DYNAMIC"; + if ( $shape['format'] === 'compressed' || strpos($shape['options'], 'key_block_size') !== false ) { + $row_sql .= ', KEY_BLOCK_SIZE=0'; + } + $identity_note('Table '.$physical.' row format is '.$shown.'. Altering to DYNAMIC.'); + $identity_note('Upgrading: '.$row_sql); + $row_alter = $PDOX->queryReturnError($row_sql, false, false); + if ( ! $row_alter->success ) { + $identity_note('Table '.$physical.' row format alter failed: '.$row_alter->errorImplode.'. Columns left unchanged.'); + $natural_row_ready[$physical] = false; + return false; + } + $after = $natural_format($physical); + if ( $after['format'] !== 'dynamic' ) { + $still = $after['format'] === '' ? 'unknown' : $after['format']; + $identity_note('Table '.$physical.' row format is still '.$still.' after ALTER. Columns left unchanged.'); + $natural_row_ready[$physical] = false; + return false; + } + $identity_note('Table '.$physical.' row format is DYNAMIC.'); + $natural_row_ready[$physical] = true; + return true; + }; + // Non-strict mode can truncate a value during MODIFY and still report success. + $natural_modify_strict = function($sql) use ($PDOX, $identity_note) { + $mode_rows = $PDOX->allRows("SELECT @@SESSION.sql_mode AS sql_mode", false, false); + if ( ! is_array($mode_rows) || ! isset($mode_rows[0]) ) { + $identity_note('Could not read sql_mode. Column left unchanged.'); + return false; + } + $prior = (string) \Tsugi\Util\U::get($mode_rows[0], 'sql_mode'); + if ( preg_match('/^[A-Za-z0-9_,]*$/', $prior) !== 1 ) { + $identity_note('sql_mode is not a mode list. Column left unchanged.'); + return false; + } + $next = $prior === '' ? 'STRICT_ALL_TABLES' : $prior.',STRICT_ALL_TABLES'; + $set_mode = $PDOX->queryReturnError("SET SESSION sql_mode = '".$next."'", false, false); + if ( ! $set_mode->success ) { + $identity_note('Could not set strict sql_mode: '.$set_mode->errorImplode.'. Column left unchanged.'); + return false; + } + $identity_note('Upgrading: '.$sql); + $alter = $PDOX->queryReturnError($sql, false, false); + $restore = $PDOX->queryReturnError("SET SESSION sql_mode = '".$prior."'", false, false); + if ( ! $restore->success ) { + $identity_note('Could not restore sql_mode: '.$restore->errorImplode.'.'); + } + return $alter; + }; + $natural_index_columns = function($index_name, $physical) use ($PDOX) { + $detail = $PDOX->indexDetail($physical); + if ( ! is_array($detail) ) return array(); + $ranked = array(); + foreach ( $detail as $index_row ) { + if ( (string) \Tsugi\Util\U::get($index_row, 'Key_name') !== $index_name ) continue; + $seq = (int) \Tsugi\Util\U::get($index_row, 'Seq_in_index'); + $ranked[$seq] = strtolower((string) \Tsugi\Util\U::get($index_row, 'Column_name')); + } + if ( count($ranked) === 0 ) return array(); + ksort($ranked); + return array_values($ranked); + }; + $natural_described = array(); + foreach ( \Tsugi\Services\Admin\AdminService::naturalKeyAsciiColumns() as $natural_column ) { + $natural_table = $natural_column[0]; + $natural_field = $natural_column[1]; + $natural_id = $natural_column[2]; + $natural_width = (int) $natural_column[3]; + $natural_label = 'Identity column '.$natural_table.'.'.$natural_field; + $natural_ident = '/^[A-Za-z0-9_]+$/'; + if ( preg_match($natural_ident, $natural_table) !== 1 + || preg_match($natural_ident, $natural_field) !== 1 + || preg_match($natural_ident, $natural_id) !== 1 + || $natural_width < 1 || $natural_width > 3072 ) { + $identity_note($natural_label.': column description is not usable. Left unchanged.'); + continue; + } + $natural_physical = $CFG->dbprefix.$natural_table; + if ( ! array_key_exists($natural_physical, $natural_described) ) { + $natural_meta = $PDOX->describe($natural_physical); + $natural_described[$natural_physical] = is_array($natural_meta) ? $natural_meta : false; + } + if ( $natural_described[$natural_physical] === false ) { + $identity_note($natural_label.': table '.$natural_table.' is absent. Left unchanged.'); + continue; + } + $natural_field_meta = null; + foreach ( $natural_described[$natural_physical] as $natural_field_row ) { + if ( \Tsugi\Util\U::getIgnoreCase($natural_field_row, 'Field') === $natural_field ) { + $natural_field_meta = $natural_field_row; + break; + } + } + if ( ! is_array($natural_field_meta) ) { + $identity_note($natural_label.': column is absent. Left unchanged.'); + continue; + } + $natural_schema_rows = $PDOX->allRows( + "SELECT DATA_TYPE AS data_type, + CHARACTER_MAXIMUM_LENGTH AS char_length, + CHARACTER_SET_NAME AS charset_name, + COLLATION_NAME AS collation_name + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = :table_name + AND COLUMN_NAME = :column_name", + array( + ':table_name' => $natural_physical, + ':column_name' => $natural_field, + ), + false + ); + if ( ! is_array($natural_schema_rows) || ! isset($natural_schema_rows[0]) ) { + $identity_note($natural_label.': could not read the column type. Left unchanged.'); + continue; + } + $natural_schema = $natural_schema_rows[0]; + $natural_already = strtolower((string) \Tsugi\Util\U::getIgnoreCase($natural_schema, 'data_type')) === 'varchar' + && (int) \Tsugi\Util\U::getIgnoreCase($natural_schema, 'char_length') === $natural_width + && strtolower((string) \Tsugi\Util\U::getIgnoreCase($natural_schema, 'charset_name')) === 'ascii' + && strtolower((string) \Tsugi\Util\U::getIgnoreCase($natural_schema, 'collation_name')) === 'ascii_bin'; + $natural_nullable = strtoupper((string) \Tsugi\Util\U::getIgnoreCase($natural_field_meta, 'Null')) === 'YES'; + $natural_null_sql = $natural_nullable ? 'NULL' : 'NOT NULL'; + if ( $natural_already ) { + $identity_note($natural_label.': already varchar('.$natural_width.') character set ascii collation ascii_bin. Length and ASCII scan skipped.'); + } else { + $natural_count_sql = "SELECT COALESCE(SUM(CASE WHEN CHAR_LENGTH({$natural_field}) > {$natural_width} THEN 1 ELSE 0 END), 0) AS too_long, + COALESCE(SUM(CASE WHEN {$natural_field} IS NOT NULL AND CHAR_LENGTH({$natural_field}) <> LENGTH({$natural_field}) THEN 1 ELSE 0 END), 0) AS non_ascii + FROM {$natural_physical}"; + $natural_counts = $PDOX->allRows($natural_count_sql, false, false); + if ( ! is_array($natural_counts) || ! isset($natural_counts[0]) ) { + $identity_note($natural_label.': could not count values. Left unchanged.'); + continue; + } + $natural_too_long = (int) \Tsugi\Util\U::get($natural_counts[0], 'too_long', 0); + $natural_non_ascii = (int) \Tsugi\Util\U::get($natural_counts[0], 'non_ascii', 0); + $natural_long_word = $natural_too_long === 1 ? 'value' : 'values'; + $natural_ascii_word = $natural_non_ascii === 1 ? 'value' : 'values'; + $natural_summary = $natural_label.': '.$natural_too_long.' '.$natural_long_word.' longer than '.$natural_width.' characters, '.$natural_non_ascii.' '.$natural_ascii_word.' outside ASCII.'; + if ( $natural_too_long > 0 || $natural_non_ascii > 0 ) { + $natural_example_sql = "SELECT {$natural_id} AS row_id, + CHAR_LENGTH({$natural_field}) AS value_length, + LENGTH({$natural_field}) AS value_bytes + FROM {$natural_physical} + WHERE CHAR_LENGTH({$natural_field}) > {$natural_width} + OR ({$natural_field} IS NOT NULL AND CHAR_LENGTH({$natural_field}) <> LENGTH({$natural_field})) + ORDER BY CHAR_LENGTH({$natural_field}) DESC, {$natural_id} + LIMIT 5"; + $natural_examples = $PDOX->allRows($natural_example_sql, false, false); + $natural_example_bits = array(); + if ( is_array($natural_examples) ) { + foreach ( $natural_examples as $natural_example ) { + $natural_example_bits[] = $natural_id.'='.\Tsugi\Util\U::get($natural_example, 'row_id') + .' length='.\Tsugi\Util\U::get($natural_example, 'value_length') + .' bytes='.\Tsugi\Util\U::get($natural_example, 'value_bytes'); + } + } + $natural_example_text = count($natural_example_bits) > 0 ? ' Examples: '.implode('; ', $natural_example_bits).'.' : ''; + $natural_shape = $natural_format($natural_physical); + $natural_shown = $natural_shape['format'] === '' ? 'unknown' : $natural_shape['format']; + $identity_note($natural_summary.$natural_example_text.' Left unchanged. Row format is '.$natural_shown.'.'); + continue; + } + } + if ( ! $natural_ensure_dynamic($natural_physical) ) { + $identity_note($natural_label.': left unchanged.'); + continue; + } + if ( ! $natural_already ) { + $natural_alter_sql = "ALTER TABLE {$natural_physical} MODIFY {$natural_field} VARCHAR({$natural_width}) CHARACTER SET ascii COLLATE ascii_bin {$natural_null_sql}"; + $identity_note($natural_summary.' Altering to varchar('.$natural_width.') character set ascii collation ascii_bin.'); + $natural_alter = $natural_modify_strict($natural_alter_sql); + if ( $natural_alter === false || ! $natural_alter->success ) { + $natural_alter_error = is_object($natural_alter) ? $natural_alter->errorImplode : 'strict mode was not set'; + $identity_note($natural_label.': alter failed: '.$natural_alter_error.'. Left unchanged.'); + continue; + } + $identity_note($natural_label.': altered to varchar('.$natural_width.') character set ascii collation ascii_bin.'); + } + $natural_bin = $natural_field.'_bin'; + if ( preg_match($natural_ident, $natural_bin) !== 1 ) { + $identity_note($natural_label.': exact-byte column name is not usable. Unique index not added.'); + continue; + } + if ( ! $PDOX->columnExists($natural_bin, $natural_physical) ) { + // MariaDB 10.5 rejects NULL or NOT NULL after STORED. + $natural_bin_sql = "ALTER TABLE {$natural_physical} ADD COLUMN {$natural_bin} VARBINARY({$natural_width}) GENERATED ALWAYS AS (CAST({$natural_field} AS BINARY)) STORED"; + $identity_note($natural_label.': adding exact-byte column '.$natural_bin.'.'); + $identity_note('Upgrading: '.$natural_bin_sql); + $natural_bin_alter = $PDOX->queryReturnError($natural_bin_sql, false, false); + if ( ! $natural_bin_alter->success ) { + $identity_note($natural_label.': exact-byte column alter failed: '.$natural_bin_alter->errorImplode.'. Unique index not added.'); + continue; + } + $identity_note($natural_label.': added exact-byte column '.$natural_bin.'.'); + } else { + $natural_bin_meta = $PDOX->describeColumn($natural_bin, $natural_physical); + $natural_bin_extra = strtolower((string) \Tsugi\Util\U::getIgnoreCase($natural_bin_meta, 'Extra')); + if ( strpos($natural_bin_extra, 'generated') === false ) { + $identity_note($natural_label.': '.$natural_bin.' exists and is not a generated column. Unique index not added.'); + continue; + } + $identity_note($natural_label.': exact-byte column '.$natural_bin.' is already present.'); + } + + $natural_unique_name = isset($natural_column[4]) ? (string) $natural_column[4] : ''; + $natural_unique_columns = isset($natural_column[5]) && is_array($natural_column[5]) ? $natural_column[5] : array(); + $natural_unique_ok = preg_match($natural_ident, $natural_unique_name) === 1 && count($natural_unique_columns) > 0; + if ( $natural_unique_ok ) { + foreach ( $natural_unique_columns as $natural_unique_column ) { + if ( preg_match($natural_ident, (string) $natural_unique_column) !== 1 ) { + $natural_unique_ok = false; + break; + } + } + } + if ( ! $natural_unique_ok ) { + $identity_note($natural_label.': unique description is not usable. Unique index not added.'); + continue; + } + $natural_unique_index = $CFG->dbprefix.$natural_unique_name; + $natural_unique_list = implode(', ', $natural_unique_columns); + $natural_unique_want = array(); + foreach ( $natural_unique_columns as $natural_unique_column ) { + $natural_unique_want[] = strtolower((string) $natural_unique_column); + } + $natural_unique_have = $natural_index_columns($natural_unique_index, $natural_physical); + $natural_unique_ready = $natural_unique_have === $natural_unique_want; + if ( $natural_unique_ready ) { + $identity_note($natural_label.': unique ('.$natural_unique_list.') already present.'); + continue; + } + $natural_unique_replace = count($natural_unique_have) > 0; + $natural_unique_where = array(); + foreach ( $natural_unique_columns as $natural_unique_column ) { + $natural_unique_where[] = $natural_unique_column.' IS NOT NULL'; + } + $natural_unique_where_sql = implode(' AND ', $natural_unique_where); + $natural_group_rows = $PDOX->allRows( + "SELECT COUNT(*) AS duplicate_groups FROM ( + SELECT 1 AS present + FROM {$natural_physical} + WHERE {$natural_unique_where_sql} + GROUP BY {$natural_unique_list} + HAVING COUNT(*) > 1 + ) AS duplicate_rows", + false, + false + ); + if ( ! is_array($natural_group_rows) || ! isset($natural_group_rows[0]) ) { + $identity_note($natural_label.': could not count duplicate values for unique ('.$natural_unique_list.'). Unique index not added.'); + continue; + } + $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_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 { + $natural_example_select .= ', MIN(key_id) AS key_id, MAX(key_id) AS key_id_max'; + } + $natural_dup_examples = $PDOX->allRows( + "SELECT {$natural_example_select} + FROM {$natural_physical} + WHERE {$natural_unique_where_sql} + GROUP BY {$natural_unique_list} + HAVING n > 1 + ORDER BY n DESC, example_id + LIMIT 5", + false, + false + ); + $natural_dup_bits = array(); + if ( is_array($natural_dup_examples) ) { + foreach ( $natural_dup_examples as $natural_dup_example ) { + $natural_dup_bit = $natural_id.'='.\Tsugi\Util\U::get($natural_dup_example, 'example_id') + .' count='.\Tsugi\Util\U::get($natural_dup_example, 'n'); + $natural_dup_key = \Tsugi\Util\U::get($natural_dup_example, 'key_id'); + $natural_dup_key_max = \Tsugi\Util\U::get($natural_dup_example, 'key_id_max'); + if ( $natural_dup_key !== null && $natural_dup_key !== false && $natural_dup_key !== '' ) { + $natural_dup_bit .= ' key_id='.$natural_dup_key; + if ( $natural_dup_key_max !== null && $natural_dup_key_max !== false && (string) $natural_dup_key_max !== (string) $natural_dup_key ) { + $natural_dup_bit .= '..'.$natural_dup_key_max; + } + } + $natural_dup_bits[] = $natural_dup_bit; + } + } + $natural_dup_text = count($natural_dup_bits) > 0 ? ' Examples: '.implode('; ', $natural_dup_bits).'.' : ''; + $identity_note($natural_label.': '.$natural_groups.' duplicate '.$natural_group_word.' for unique ('.$natural_unique_list.').'.$natural_dup_text.' Unique index not added.'); + continue; + } + if ( $natural_unique_replace ) { + $natural_unique_sql = "ALTER TABLE {$natural_physical} DROP INDEX `{$natural_unique_index}`, ADD CONSTRAINT `{$natural_unique_index}` UNIQUE ({$natural_unique_list})"; + $identity_note($natural_label.': unique '.$natural_unique_index.' is on ('.implode(', ', $natural_unique_have).'). 0 duplicate groups for unique ('.$natural_unique_list.'). Replacing it.'); + } else { + $natural_unique_sql = "ALTER TABLE {$natural_physical} ADD CONSTRAINT `{$natural_unique_index}` UNIQUE ({$natural_unique_list})"; + $identity_note($natural_label.': 0 duplicate groups for unique ('.$natural_unique_list.'). Adding constraint '.$natural_unique_index.'.'); + } + $identity_note('Upgrading: '.$natural_unique_sql); + $natural_unique_alter = $PDOX->queryReturnError($natural_unique_sql, false, false); + if ( ! $natural_unique_alter->success ) { + $identity_note($natural_label.': unique ('.$natural_unique_list.') alter failed: '.$natural_unique_alter->errorImplode.'. Unique index not added.'); + continue; + } + $natural_unique_verb = $natural_unique_replace ? 'replaced' : 'added'; + $identity_note($natural_label.': '.$natural_unique_verb.' unique ('.$natural_unique_list.').'); + } + // When you increase this number in any database.php file, // make sure to update the global value in setup.php - return 202611200002; + return 202611220002; }; // Don't forget the semicolon on anonymous functions :) diff --git a/lib/src/Services/Plus/RosterSync.php b/lib/src/Services/Plus/RosterSync.php index 4b6c1dc00..1c40e1d67 100644 --- a/lib/src/Services/Plus/RosterSync.php +++ b/lib/src/Services/Plus/RosterSync.php @@ -2,6 +2,7 @@ namespace Tsugi\Services\Plus; +use Tsugi\Services\Admin\AdminService; use Tsugi\Core\Guid; use Tsugi\Core\Keyset; use Tsugi\Core\LTIX; @@ -649,6 +650,10 @@ private static function withDeploymentClaim($fn) { * @param mixed $member */ private static function upsertUser($keyId, $subject, $member) { + $problem = AdminService::naturalKeyProblem('subject_key', $subject); + if ( $problem !== null ) { + throw new \RuntimeException($problem); + } global $CFG; $PDOX = LTIX::getConnection(); $p = $CFG->dbprefix; diff --git a/lib/src/Services/Profile/database.php b/lib/src/Services/Profile/database.php index f5420da74..d0f177fd8 100644 --- a/lib/src/Services/Profile/database.php +++ b/lib/src/Services/Profile/database.php @@ -13,7 +13,11 @@ "create table {$CFG->dbprefix}profile ( profile_id INTEGER NOT NULL AUTO_INCREMENT, profile_sha256 CHAR(64) NOT NULL UNIQUE, - profile_key TEXT NOT NULL, + -- Same value as lti_user.user_key. googlemail: plus an email can pass 255. + profile_key VARCHAR(512) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + -- ascii_bin ignores trailing spaces. This is the exact bytes, for the unique index. + -- MariaDB 10.5 rejects NULL or NOT NULL after STORED. + profile_key_bin VARBINARY(512) GENERATED ALWAYS AS (CAST(profile_key AS BINARY)) STORED, deleted TINYINT(1) NOT NULL DEFAULT 0, key_id INTEGER NOT NULL, @@ -43,8 +47,11 @@ INDEX `{$CFG->dbprefix}profile_indx_email` (email(255)), INDEX `{$CFG->dbprefix}profile_indx_map` (`map`), + -- Parallel to the unique on profile_sha256, on the exact bytes. One value across every key. + CONSTRAINT `{$CFG->dbprefix}profile_const_key` UNIQUE(profile_key_bin), + CONSTRAINT `{$CFG->dbprefix}profile_const_pk` PRIMARY KEY (profile_id) -) ENGINE = InnoDB DEFAULT CHARSET=utf8") +) ENGINE = InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC") ); $DATABASE_UPGRADE = function($oldversion) { diff --git a/lib/src/Services/StudentView/StudentView.php b/lib/src/Services/StudentView/StudentView.php index d3cf71018..61a4968c9 100644 --- a/lib/src/Services/StudentView/StudentView.php +++ b/lib/src/Services/StudentView/StudentView.php @@ -2,6 +2,7 @@ namespace Tsugi\Services\StudentView; +use Tsugi\Services\Admin\AdminService; use Tsugi\Controllers\Courses; use Tsugi\Controllers\Tool; use Tsugi\Core\Cache; @@ -570,6 +571,11 @@ private static function findUser($contextId, $slug) { * @return array{user_id:int,key_id:int,user_sha256:string,user_key:string}|null */ private static function insertUser($keyId, $userKey, $name, $email) { + $problem = AdminService::naturalKeyProblem('user_key', $userKey); + if ( $problem !== null ) { + error_log($problem); + return null; + } global $CFG, $PDOX; $PDOX = LTIX::getConnection(); $sha = U::lti_sha256($userKey); diff --git a/lib/tests/Services/Admin/AdminServiceNaturalKeyTest.php b/lib/tests/Services/Admin/AdminServiceNaturalKeyTest.php new file mode 100644 index 000000000..f54d66fa4 --- /dev/null +++ b/lib/tests/Services/Admin/AdminServiceNaturalKeyTest.php @@ -0,0 +1,75 @@ +assertNull(AdminService::naturalKeyProblem('subject_key', 'abc')); + $this->assertNull(AdminService::naturalKeyProblem('subject_key', null)); + $this->assertNull(AdminService::naturalKeyProblem('subject_key', 'ab c')); + $this->assertNull(AdminService::naturalKeyProblem('user_key', null)); + $this->assertNull(AdminService::naturalKeyProblem('service_key', str_repeat('a', 1024))); + $this->assertNull(AdminService::naturalKeyProblem('profile_key', 'googlemail:ada@example.edu')); + } + + 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)); + } + + public function testACharacterOutsideAsciiIsRefused() { + $this->assertSame( + 'user_key has a character outside ASCII.', + AdminService::naturalKeyProblem('user_key', "caf\u{e9}") + ); + } + + public function testAValuePastTheLimitIsRefused() { + $this->assertSame( + 'subject_key is 256 characters and the limit is 255.', + AdminService::naturalKeyProblem('subject_key', str_repeat('a', 256)) + ); + $this->assertNull(AdminService::naturalKeyProblem('subject_key', str_repeat('a', 255))); + $this->assertSame( + 'user_key is 513 characters and the limit is 512.', + AdminService::naturalKeyProblem('user_key', str_repeat('b', 513)) + ); + $this->assertSame( + 'service_key is 1025 characters and the limit is 1024.', + AdminService::naturalKeyProblem('service_key', str_repeat('c', 1025)) + ); + } + + public function testANonStringIsRefused() { + $this->assertSame('user_key is not text.', AdminService::naturalKeyProblem('user_key', 12)); + } + + public function testLeadingAndTrailingSpacesAreRefused() { + $this->assertSame('subject_key has a leading space.', AdminService::naturalKeyProblem('subject_key', ' abc')); + $this->assertSame('user_key has a trailing space.', AdminService::naturalKeyProblem('user_key', 'googlemail:ada@example.edu ')); + $this->assertSame( + 'service_key has a leading space and has a trailing space.', + AdminService::naturalKeyProblem('service_key', ' https://lms.example/grades ') + ); + } + + public function testNonPrintingCharactersAreRefused() { + $this->assertSame( + 'subject_key contains a non-printing character.', + AdminService::naturalKeyProblem('subject_key', "user-1\n") + ); + $this->assertSame( + 'profile_key contains a non-printing character.', + AdminService::naturalKeyProblem('profile_key', "googlemail:ada@example.edu\t") + ); + $this->assertSame( + 'service_key contains a non-printing character.', + AdminService::naturalKeyProblem('service_key', "https://lms.example/\x07grades") + ); + $this->assertSame( + 'user_key has a trailing space and contains a non-printing character.', + AdminService::naturalKeyProblem('user_key', "googlemail:ada@example.edu \n") + ); + } +}