fix(dbconn): stop interpreting user DDL as an escape format string#1037
Draft
morgo wants to merge 1 commit into
Draft
fix(dbconn): stop interpreting user DDL as an escape format string#1037morgo wants to merge 1 commit into
morgo wants to merge 1 commit into
Conversation
Callers built the sqlescape format string by concatenating raw user SQL onto a trusted prefix (e.g. "ALTER TABLE %n ALGORITHM=INSTANT, " + stmt.Alter). sqlescape interprets %n/%?/%% anywhere in the text, including inside string literals of the user's DDL: - An alter containing %n or %? in a literal (e.g. COMMENT '100%new') failed escaping with "missing arguments", which PANICKED the process on the ForceExec path (MustEscapeSQL), the default path. - %% in a literal (e.g. DEFAULT '50%% off') was silently collapsed to a single %, so spirit executed different DDL than the user wrote. Fix: user SQL is now data, never format. Add dbconn.ExecRaw and dbconn.ForceExecRaw which execute a fully-built statement with no format interpretation, and route all statements embedding user SQL through them. Trusted prefixes are escaped separately and the user clause appended verbatim. ForceExec is now a thin wrapper that escapes (error-returning, no panic) BEFORE the kill timer is armed, removing the panic-inside-timer-window hazard; kill-timer semantics unchanged. Call sites fixed: - migration/change.go alterNewTable (ALGORITHM=COPY attempt + retry) - migration/change.go attemptInstantDDL (ForceExec + Exec variants) - migration/change.go attemptInplaceDDL (ForceExec + Exec variants) - migration/runner.go non-ALTER statement execution (raw stmt.Statement was the format string) All other Exec/ForceExec/EscapeSQL call sites use compile-time constant format strings and are unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Several call sites build the
sqlescapeformat string by concatenating raw user SQL onto a trusted prefix, e.g."ALTER TABLE %n ALGORITHM=INSTANT, " + c.stmt.Alter.sqlescape.EscapeSQLinterprets%n/%?/%%anywhere in the text — including inside string literals of the user's DDL:--altercontaining%nor%?in a literal (e.g.COMMENT '100%new',CHECK (name LIKE 'a%?')) fails escaping withmissing arguments. On the default force-kill path this goes throughsqlescape.MustEscapeSQL, which panics the process — with the kill timer already armed.%%in a literal (e.g.DEFAULT '50%% off') is silently collapsed to a single%, so spirit executes different DDL than the user wrote ('50% off'instead of'50%% off').Non-ALTER statements (
CREATE TABLE/DROP TABLE/RENAME TABLE) were also executed by passing the raw user statement as the format string with zero args.Fix
User SQL is now always data, never a format string:
dbconn.ExecRaw(ctx, db, stmt)anddbconn.ForceExecRaw(ctx, db, tables, dbConfig, logger, stmt)execute a fully-built statement with no format interpretation.ForceExecis now a thin wrapper overForceExecRawthat escapes with the error-returningEscapeSQL(no panic) before the kill timer is armed — this also removes the previous panic-inside-timer-window hazard. Kill-timer/retry semantics are unchanged.sqlescape.MustEscapeSQL("ALTER TABLE %n ALGORITHM=INSTANT, ", tableName)) and append the user clause verbatim, executing via the raw variants.Call sites audited and fixed (all places where a non-constant format string reached
Exec/ForceExec):pkg/migration/change.goalterNewTable—ALGORITHM=COPYattempt and the plain retrypkg/migration/change.goattemptInstantDDL—ForceExecandExecvariantspkg/migration/change.goattemptInplaceDDL—ForceExecandExecvariantspkg/migration/runner.go— non-ALTER single-statement execution (stmt.Statementwas the format string)All other
Exec/ForceExec/EscapeSQLcall sites were audited and use compile-time constant format strings (incl.checkpoint.go's"CREATE TABLE %n " + tableDDL, which concatenates two package constants); they are unchanged.pkg/move/pkg/datasyncalready execute fetched DDL via plainExecContext.Testing
New regression tests (verified to fail without the fix — the instant-path test reproduces the process panic
missing arguments, need 2-th arg, but only got 1 args):pkg/migrationTestPercentSignsInDDLLiterals:COMMENT '100%new, a%?b'— no panic, comment lands exactly as written inSHOW CREATE TABLE(exercises theForceExecRawpath,SkipForceKill=false)DEFAULT '50%% off' COMMENT 'literal 50%%'— the same clause is run directly with a plain client on a sibling table and the resulting column definitions must be byte-identical (previously spirit stored'50% off')ADD INDEXforces copy) withDEFAULT 'copy a%?b'— exercisesalterNewTableCREATE TABLE ... DEFAULT '50%% off' COMMENT '100%new'via--statementpkg/dbconnTestExecRaw(literals reach the server verbatim;Execon the same text still errors),TestForceExecRaw(no format interpretation and the MDL-blocker force-kill still works),TestForceExecBadFormatString(bad format now returns an error before the kill timer is armed, instead of panicking)Runs against MySQL 8.0.45:
./pkg/dbconn/...suite: pass./pkg/migrationtests covering the touched paths (TestPercentSignsInDDLLiterals,TestForNonInstantBurn,TestIndexVisibility,TestStatementWorkflowStillInstant,TestTrailingSemicolon,TestAlterExtendVarcharE2E,TestMigrationWithSQLCommentsInStatement,TestStmtWorkflow,TestCreateIndexIsRewritten,TestRenameInMySQL80,TestSchemaNameIncluded, change.go tests): passgo build,go vet,gofmt,golangci-lint: clean🤖 Generated with Claude Code