-
Notifications
You must be signed in to change notification settings - Fork 1
fix: REVOKE-vs-GRANT bug + escape ' in CREATE LOGIN passwords #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -386,6 +386,14 @@ const ( | |
| func (c *Client) CreateLogin(ctx context.Context, loginType LoginType, username, password string) error { | ||
| l := ctxzap.Extract(ctx) | ||
|
|
||
| // Username is interpolated into bracket-quoted SQL Server identifiers | ||
| // below. Reject characters that would let the value break out of the | ||
| // brackets (this is the same guard the rest of this package uses on | ||
| // identifier-interpolated inputs). | ||
| if strings.ContainsAny(username, "[]\"';") { | ||
| return fmt.Errorf("invalid characters in username") | ||
| } | ||
|
|
||
| var query string | ||
| switch loginType { | ||
| case LoginTypeWindows: | ||
|
|
@@ -399,7 +407,12 @@ func (c *Client) CreateLogin(ctx context.Context, loginType LoginType, username, | |
| // For SQL Server authentication, only username and password are used | ||
| loginName := fmt.Sprintf("[%s]", username) | ||
| l.Debug("creating SQL login", zap.String("login", loginName)) | ||
| query = fmt.Sprintf("CREATE LOGIN %s WITH PASSWORD = '%s';", loginName, password) | ||
| // SQL Server string literals escape ' by doubling it. Without this, | ||
| // a password containing ' breaks out of the literal -- a SQL | ||
| // injection vector and at minimum a syntax error for legitimate | ||
| // users whose passwords contain '. | ||
| escapedPassword := strings.ReplaceAll(password, "'", "''") | ||
| query = fmt.Sprintf("CREATE LOGIN %s WITH PASSWORD = '%s';", loginName, escapedPassword) | ||
|
Comment on lines
408
to
+415
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Suggestion: The password escaping is correct, but |
||
| case LoginTypeAzureAD, LoginTypeEntraID: | ||
| // Azure AD and Entra ID use external provider | ||
| loginName := fmt.Sprintf("[%s]", username) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Suggestion: The sibling
GrantPermissionOnDatabase(line ~126) still passes inert positional argspermission, db, usertoExecContext— the same pattern cleaned up here. Consider removing them there too for consistency.