fix(codegen): validate every identifier before interpolation into DDL#83
Merged
Conversation
Closes #39. `generate_sidecar_schema` interpolated `table.name` and PK column names directly into raw SQL via `format!()`. A table named `posts'); DROP TABLE x;--` would be injected verbatim into the metadata seed INSERT. The DDL generator was a SQL-injection sink wide open to whatever the schema parser handed it. Add a `crate::codegen::ident::validate_identifier` helper: - Accepts `^[A-Za-z_][A-Za-z0-9_]*$` with a 63-character length cap (Postgres' NAMEDATALEN minus null terminator). - Returns `anyhow::Error` whose message names the offending identifier (truncated to 60 chars for legibility) and the kind label ("table name" / "column name") so users can find it in their schema input. Gate every identifier at the entry of `generate_sidecar_schema` — every `table.name` and every `column.name` — so downstream `format!()` sites are safe by construction. Return type becomes `anyhow::Result<String>`. Update call sites: - `main.rs` Generate command: `?` propagates the error to the user. - In-module tests: `.expect("test schema must validate")`. - `tests/integration_test.rs`: same. New tests in `codegen::ident::tests`: - `valid_identifiers_pass`: 7 happy-path cases. - `injection_strings_rejected`: 16 attack strings including the canonical `posts'); DROP TABLE x;--`, semicolons, quotes, comments, whitespace, non-ASCII, dots, parens. - `overlong_identifiers_rejected`: 64-char input fails. - `error_names_offending_identifier`: error message includes both the bad identifier and the kind label. `cargo clippy --all-targets -- -D warnings` clean; 42 unit tests pass (up from 38). Co-Authored-By: Claude Opus 4.7 <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.
Summary
Per V-L2-G1:
generate_sidecar_schemawas a SQL-injection sink. A table name likeposts''); DROP TABLE x;--would have been interpolated verbatim into the metadata seed INSERT.crate::codegen::ident::validate_identifieraccepting^[A-Za-z_][A-Za-z0-9_]*$with a 63-char cap. Error message names the offending identifier + kind.generate_sidecar_schema. Return becomesanyhow::Result<String>.main.rs, in-module tests, andtests/integration_test.rsto handle the Result.Closes
Test plan
cargo clippy --all-targets -- -D warningscleancargo test --lib --bins42/42 (4 new incodegen::ident::tests)