Repository navigation
fix(dapper)!: validate and quote schema and table names - #49
Merged
Merged
Conversation
Validate DapperDataProtectionConfig.SchemaName and TableName at startup (options validation with ValidateOnStart) with a per-provider validator: SQL Server accepts letters, digits and underscores (schema <= 128, table <= 112 characters); PostgreSQL accepts the same in lowercase only (<= 63). Quote every identifier, pass names as parameters in the SQL Server existence checks, and create the schema in InitializeDb when it is missing. Closes #22 BREAKING CHANGE: names that do not match the new rules now fail at startup with an OptionsValidationException. SQL Server names containing characters other than letters, digits and underscores must be renamed. PostgreSQL names must be lowercase: replace a mixed-case name with its lowercase form, which is the name of the table created by earlier versions.
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
SchemaNameandTableNameare validated by an internal per-providerIValidateOptions<DapperDataProtectionConfig>registered withValidateOnStart(). Without a host, validation runs whenUseDapperDataProtection()reads the options. TheOptionsValidationExceptionnames the option, the value and the rule.^[A-Za-z_][A-Za-z0-9_]*, schema <= 128, table <= 112 characters (so thatIX_{TableName}_FriendlyNamefits in 128).^[a-z_][a-z0-9_]*, <= 63 characters.[...]/"...", closing quote escaped), including constraint and index names, so reserved words work. SQL Server passes the names as parameters in the existence checks,QUOTENAMEand the duplicate-index error message.InitializeDb()creates the schema when it is missing, inside the initialization lock added in fix: serialize InitializeDb across concurrent instances #47. Existence is checked first (SCHEMA_ID/pg_namespace), because PostgreSQL'sCREATE SCHEMA IF NOT EXISTSrequires the database CREATE privilege even when the schema exists.Design note: PostgreSQL names must be lowercase
The issue proposed documenting that quoting makes PostgreSQL names case-sensitive. That is not enough. Earlier versions did not quote names, so
DpKeyscreated the tabledpkeys. With quoting, the same configuration would silently target a new, empty"DpKeys"table, and the existing keys would be ignored. Rejecting mixed-case names turns that into a startup error with a clear fix: use the lowercase form, which is the name of the existing table. Lowercase quoted and unquoted names are the same, so existing installations keep working.Tests
IStartupValidatorandUseDapperDataProtection().order); end-to-end with a missing custom schema and a reserved-word table name for both providers.Closes #22
BREAKING CHANGE: names that do not match the new rules now fail at startup with an
OptionsValidationException. SQL Server names containing characters other than letters, digits and underscores must be renamed. PostgreSQL names must be lowercase: replace a mixed-case name with its lowercase form, which is the name of the table created by earlier versions.