-
Notifications
You must be signed in to change notification settings - Fork 21
migrate: map custom middleware context keys #176
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 |
|---|---|---|
|
|
@@ -12,29 +12,54 @@ import ( | |
|
|
||
| func MigrateMiddlewareLocals(cmd *cobra.Command, cwd string, _, _ *semver.Version) error { | ||
| changed, err := internal.ChangeFileContent(cwd, func(content string) string { | ||
| replacements := []struct { | ||
| re *regexp.Regexp | ||
| repl string | ||
| ctxMap := map[string]string{ | ||
| "requestid": "requestid.FromContext(%s)", | ||
| "csrf": "csrf.TokenFromContext(%s)", | ||
| "csrf_handler": "csrf.HandlerFromContext(%s)", | ||
| "session": "session.FromContext(%s)", | ||
| "username": "basicauth.UsernameFromContext(%s)", | ||
| "password": "basicauth.PasswordFromContext(%s)", | ||
| "token": "keyauth.TokenFromContext(%s)", | ||
| } | ||
|
|
||
| extractors := []struct { | ||
| pkg string | ||
| field string | ||
| replFmt string | ||
| }{ | ||
| {regexp.MustCompile(`(\w+)\.Locals\("requestid"\)`), `requestid.FromContext($1)`}, | ||
| {regexp.MustCompile(`(\w+)\.Locals\("csrf"\)`), `csrf.TokenFromContext($1)`}, | ||
| {regexp.MustCompile(`(\w+)\.Locals\("csrf_handler"\)`), `csrf.HandlerFromContext($1)`}, | ||
| {regexp.MustCompile(`(\w+)\.Locals\("session"\)`), `session.FromContext($1)`}, | ||
| {regexp.MustCompile(`(\w+)\.Locals\("username"\)`), `basicauth.UsernameFromContext($1)`}, | ||
| {regexp.MustCompile(`(\w+)\.Locals\("password"\)`), `basicauth.PasswordFromContext($1)`}, | ||
| {regexp.MustCompile(`(\w+)\.Locals\("token"\)`), `keyauth.TokenFromContext($1)`}, | ||
| {"csrf", "ContextKey", "csrf.TokenFromContext(%s)"}, | ||
| {"keyauth", "ContextKey", "keyauth.TokenFromContext(%s)"}, | ||
| {"session", "ContextKey", "session.FromContext(%s)"}, | ||
| {"basicauth", "ContextUsername", "basicauth.UsernameFromContext(%s)"}, | ||
| {"basicauth", "ContextPassword", "basicauth.PasswordFromContext(%s)"}, | ||
| } | ||
| for _, r := range replacements { | ||
| content = r.re.ReplaceAllString(content, r.repl) | ||
|
|
||
| for _, e := range extractors { | ||
| re := regexp.MustCompile(e.pkg + `\.Config{[^}]*` + e.field + `:\s*"([^"]+)"`) | ||
|
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. The regular expression used here is not robust enough for parsing Go source code. The For example, the regex would fail on this valid code: _ = csrf.New(csrf.Config{
SuccessHandler: func(c *fiber.Ctx) error {
return c.Next()
},
ContextKey: "my_csrf_token",
})Using regular expressions to parse source code is error-prone. A much more reliable approach would be to use the |
||
| matches := re.FindAllStringSubmatch(content, -1) | ||
| for _, m := range matches { | ||
| ctxMap[m[1]] = e.replFmt | ||
| } | ||
| } | ||
|
|
||
| reLocals := regexp.MustCompile(`(\w+)\.Locals\("([^"]+)"\)`) | ||
| content = reLocals.ReplaceAllStringFunc(content, func(s string) string { | ||
| sub := reLocals.FindStringSubmatch(s) | ||
| ctx := sub[1] | ||
| key := sub[2] | ||
| if fmtStr, ok := ctxMap[key]; ok { | ||
| return fmt.Sprintf(fmtStr, ctx) | ||
| } | ||
| return s | ||
| }) | ||
|
|
||
| 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") | ||
|
|
||
| reCtxKey := regexp.MustCompile(`\s*ContextKey:\s*[^,}\n]+,?`) | ||
| reCtxKey := regexp.MustCompile(`\s*Context(?:Username|Password|Key):\s*[^,}\n]+,?`) | ||
| content = reCtxKey.ReplaceAllString(content, "") | ||
|
|
||
| return content | ||
|
|
||
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.
🛠️ Refactor suggestion
Default "token" mapping can collide with extractor-derived mappings
Seeding ctxMap with
"token" -> keyauth.TokenFromContext(%s)creates a global mapping that may be overwritten (or overwrite) extractor-derived mappings when multiple middlewares reuse "token" (e.g., both keyauth and csrf set ContextKey to "token"). Today, precedence is purely order-of-extractor, which can silently misroute replacements across the file.Recommend: remove the eager default for "token" and add it as a fallback only if no extractor populated that key. This preserves the common keyauth case while avoiding accidental override when another middleware purposely uses "token".
Apply:
📝 Committable suggestion
🤖 Prompt for AI Agents