-
Notifications
You must be signed in to change notification settings - Fork 21
Remove CSRF ContextKey and streamline TokenFromContext usage #169
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 | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -27,6 +27,13 @@ func MigrateMiddlewareLocals(cmd *cobra.Command, cwd string, _, _ *semver.Versio | |||||||||||||||||||||||||||||||||||||
| for _, r := range replacements { | ||||||||||||||||||||||||||||||||||||||
| content = r.re.ReplaceAllString(content, r.repl) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| reTypeAssert := regexp.MustCompile(`([\w\.]+FromContext\([^\)]+\))\.\([^\)]+\)`) | ||||||||||||||||||||||||||||||||||||||
| content = reTypeAssert.ReplaceAllString(content, "$1") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| reComma := regexp.MustCompile(`(\w+)\s*,\s*\w+\s*:=\s*([\w\.]+FromContext\([^\)]+\))`) | ||||||||||||||||||||||||||||||||||||||
| content = reComma.ReplaceAllString(content, "$1 := $2") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+36
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. Regex is overbroad; it may break unrelated FromContext usages (e.g., grpc/peer.FromContext) Both post-processors target any Apply: - reTypeAssert := regexp.MustCompile(`([\w\.]+FromContext\([^\)]+\))\.\([^\)]+\)`)
- content = reTypeAssert.ReplaceAllString(content, "$1")
-
- reComma := regexp.MustCompile(`(\w+)\s*,\s*\w+\s*:=\s*([\w\.]+FromContext\([^\)]+\))`)
- content = reComma.ReplaceAllString(content, "$1 := $2")
+ allowed := `(?:requestid|csrf|session|basicauth|keyauth)`
+ reTypeAssert := regexp.MustCompile(fmt.Sprintf(
+ `((?:%s)\.(?:TokenFromContext|HandlerFromContext|FromContext|UsernameFromContext|PasswordFromContext)\([^)]+\))\s*\.\([^)]+\)`,
+ allowed,
+ ))
+ content = reTypeAssert.ReplaceAllString(content, "$1")
+
+ reComma := regexp.MustCompile(fmt.Sprintf(
+ `(\w+)\s*,\s*\w+\s*:=\s*((?:%s)\.(?:TokenFromContext|HandlerFromContext|FromContext|UsernameFromContext|PasswordFromContext)\([^)]+\))`,
+ allowed,
+ ))
+ content = reComma.ReplaceAllString(content, "$1 := $2")Follow-up: Add a negative test to ensure 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| return content | ||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
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.
The regex
reCommais a bit too broad. Using\w+for the second variable in a two-value assignment will match not only the blank identifier_but also any named variable (e.g.,ok). This could lead to silent bugs if the named variable was used later in the code. For example,val, ok := ...would be changed toval := ..., removing theokvariable without warning.It would be safer to only handle cases where the second variable is explicitly the blank identifier
_. This ensures you're only changing assignments where the second return value is intentionally discarded. If a named variable is used, the migration will not touch it, and the Go compiler will flag it as an error after the function signatures change, which is a much safer failure mode.