Skip to content

fix(generators): Validate table and column options as SQL identifiers - #103

Merged
igor-alexandrov merged 1 commit into
masterfrom
claude/generator-identifier-validation
Aug 19, 2026
Merged

igor-alexandrov merged 1 commit into
masterfrom
claude/generator-identifier-validation

Conversation

@igor-alexandrov

Copy link
Copy Markdown
Collaborator

Why

A security review noted that the table and index strategy generators interpolate --source-table-name, --source-columns, and --table-name verbatim into the generated migration's CREATE TABLE / CREATE TRIGGER SQL. There is no realistic attacker — the input is the developer's own rails g command line and the migration is written for review — but a name with punctuation produced broken or surprising DDL that only surfaced when the migration ran. Hostile-looking names should fail closed instead.

What

  • New shared Metka::Generators::SqlIdentifier module checks each option value against /\A[a-zA-Z_][a-zA-Z0-9_]*\z/.
  • Both generators call it at the top of generate_migration and raise Thor::Error naming the offending option — Thor prints it as a clean CLI error and nothing is generated. In the index generator the check runs before the PostgreSQL skip, so bad input is reported on either adapter.

Tests

  • Rejection tests for each option on both generators, asserting the error names the option and that no migration file is written (exit_on_failure? is false for Rails generators, so Thor reports the error on stderr rather than raising — the tests capture stderr).
  • Full suite green on SQLite locally (135 runs, 0 failures); PostgreSQL covered by CI.

🤖 Generated with Claude Code

The table and index strategy generators interpolate --source-table-name,
--source-columns and --table-name verbatim into the migration's CREATE
TABLE / CREATE TRIGGER SQL. A name with punctuation produced broken or
surprising DDL that only surfaced when the migration ran.

Both generators now check every name against a plain-identifier pattern
(letters, digits, underscores, not starting with a digit) and fail with
a clear Thor error before writing anything.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@igor-alexandrov
igor-alexandrov merged commit 56e2f27 into master Aug 19, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant