Skip to content

Fix JWT extractor migration loop#243

Merged
ReneWerner87 merged 2 commits into
masterfrom
codex/2025-11-30-20-50-34
Dec 1, 2025
Merged

Fix JWT extractor migration loop#243
ReneWerner87 merged 2 commits into
masterfrom
codex/2025-11-30-20-50-34

Conversation

@ReneWerner87
Copy link
Copy Markdown
Member

@ReneWerner87 ReneWerner87 commented Nov 30, 2025

Summary

  • rewrite replaceFieldImpl to process replacements without infinite loops and handle inline values safely
  • add coverage for migrating TokenLookup across inline, pointer, and Fiber v2 middleware configs

Testing

  • make lint
  • make test

Codex Task

Summary by CodeRabbit

  • Tests

    • Added extensive tests for JWT extractor migration across many scenarios (inline configs, pointer-style configs, middleware variants and legacy import paths) to ensure correct extractor generation and imports.
  • Chores

    • Improved internal migration engine: more robust, streaming field-replacement that preserves formatting, handles edge cases and comments, and yields safer fallbacks on parsing failures.

✏️ Tip: You can customize this high-level summary in your review settings.

@ReneWerner87 ReneWerner87 requested a review from a team as a code owner November 30, 2025 20:50
@ReneWerner87 ReneWerner87 requested review from efectn, gaby and sixcolors and removed request for a team November 30, 2025 20:50
@ReneWerner87 ReneWerner87 added the ☢️ Bug Something isn't working label Nov 30, 2025
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @ReneWerner87, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the underlying field replacement mechanism within the migration tool. The primary goal is to make the migration process for JWT extractor configurations more reliable and resilient by addressing potential issues like infinite loops and incorrect parsing of complex or inline values. The updated logic ensures that diverse configuration patterns are handled gracefully, leading to a more stable and accurate migration experience.

Highlights

  • Migration Logic Rewrite: The core replaceFieldImpl function, used for migrating fields, has been completely rewritten to prevent infinite loops and safely handle inline values during replacements.
  • Improved JWT Extractor Migration: The changes specifically enhance the migration of TokenLookup configurations for JWT extractors, ensuring correct handling across various scenarios including inline configurations, pointer-based configurations, and Fiber v2 middleware setups.
  • Expanded Test Coverage: New test cases have been added to validate the robustness of the JWT extractor migration for inline, pointer, and Fiber v2 middleware configurations, ensuring the TokenLookup field is correctly transformed into Extractor using extractors.FromCookie.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors replaceFieldImpl to be more robust and avoid infinite loops during migrations, which is a great improvement. It also adds valuable test coverage for the JWT extractor migration. I've identified a few areas for improvement: there are some redundant error checks in common.go, and one of the new tests in jwt_extractor_test.go appears to be for an invalid code pattern, which also seems unsupported by the current migration logic.

Comment thread cmd/internal/migrations/v3/jwt_extractor_test.go
Comment thread cmd/internal/migrations/v3/common.go Outdated
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Nov 30, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Rewrites field-replacement from regex callbacks to a streaming, position-based parser preserving prefixes/indentation/comments; adds comprehensive JWT extractor migration tests covering inline, pointer, Fiber v2, and legacy import path scenarios.

Changes

Cohort / File(s) Summary
Migration Parser Rewrite
cmd/internal/migrations/v3/common.go
Replaces regex-based replaceFieldImpl with a streaming parser that scans source by position, captures field prefixes/indentation, parses values while tracking strings and nested delimiters, extracts trailing comments via ExtractCommentAndValue, preserves formatting (prefix/newline/indent), produces TODO fallbacks on unquote failures, and assembles replacements incrementally.
JWT Extractor Migration Tests
cmd/internal/migrations/v3/jwt_extractor_test.go
Adds multiple tests for MigrateJWTExtractor validating TokenLookup→extractors.FromCookie("jwt") migrations across inline config, Fiber v2 middleware (including error flows), pointer-style config, legacy import-path variants, and skip/unrelated-package cases; asserts import handling and migration logging.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • Areas needing extra attention:
    • Position/index tracking and off-by-one risks in replaceFieldImpl.
    • Correct handling of nested delimiters and string literal edge cases.
    • Preservation of comments, trailing commas, and original whitespace/newlines.
    • Test coverage for legacy import path detection logic.

Possibly related PRs

Suggested reviewers

  • gaby
  • sixcolors
  • efectn

Poem

🐰 I nibble through code, byte by byte,
untangling braces in soft moonlight.
Tokens hop to cookie land, neat and spry,
comments kept snug as I stitch and rewrite.
Hooray — migrations done, a carrot-high delight! 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Description check ❓ Inconclusive The PR description provides a clear summary of changes and testing performed, but lacks detail about the specific problem being solved and the rationale behind the rewrite. Expand the description to explain the problem context (e.g., what infinite loop issue was occurring) and why the rewrite approach was necessary, as specified in the template.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fix JWT extractor migration loop' directly relates to the main changes in the PR, which include rewriting replaceFieldImpl to fix infinite loops and expanding test coverage for JWT extractor migration.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/2025-11-30-20-50-34

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
cmd/internal/migrations/v3/common.go (1)

207-218: Raw string literals (backticks) are not handled in string parsing.

The string detection only handles double-quoted strings. If a field value contains a raw string literal using backticks (e.g., `cookie:jwt`), the parser could incorrectly interpret commas or newlines within it as value terminators.

This may be acceptable if raw strings aren't expected in the target fields, but worth noting for robustness.

To handle all Go string types, consider extending the string detection:

 			switch ch {
 			case '"':
 				inString = true
+			case '`':
+				// Skip raw string literal
+				i++
+				for i < len(src) && src[i] != '`' {
+					i++
+				}
 			case '(', '{', '[':
 				depth++
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cc00b7f and 4c12080.

📒 Files selected for processing (2)
  • cmd/internal/migrations/v3/common.go (1 hunks)
  • cmd/internal/migrations/v3/jwt_extractor_test.go (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
cmd/internal/migrations/v3/jwt_extractor_test.go (1)
cmd/internal/migrations/v3/jwt_extractor.go (1)
  • MigrateJWTExtractor (15-95)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: Analyse
  • GitHub Check: Build (1.25.x, macos-latest)
  • GitHub Check: Build (1.25.x, macos-13)
  • GitHub Check: Build (1.25.x, windows-latest)
  • GitHub Check: Build (1.25.x, ubuntu-latest)
🔇 Additional comments (4)
cmd/internal/migrations/v3/common.go (1)

168-306: LGTM! The streaming parser correctly prevents infinite loops.

The rewritten replaceFieldImpl properly advances the pos cursor after each replacement (pos = end at line 299), and the regex search operates on src[pos:] with index adjustment. This ensures forward progress and eliminates the infinite loop issue mentioned in the PR objectives.

cmd/internal/migrations/v3/jwt_extractor_test.go (3)

97-118: LGTM!

This test covers the inline config format which is an important edge case for the field replacement logic. The assertions properly verify that TokenLookup is removed and replaced with the correct Extractor call.


150-218: LGTM!

Excellent realistic test case that validates the migration works correctly in the context of a complete Fiber v2 middleware implementation with error handling and JWT claims parsing. This provides good confidence the migration won't break real-world code.


253-281: LGTM!

Good test coverage for pointer-style configuration (&jwtware.Config{...}). The regex pattern in MigrateJWTExtractor matches jwtware.Config{...} regardless of the preceding &, so this case is correctly handled.

@ReneWerner87 ReneWerner87 merged commit f35482b into master Dec 1, 2025
14 of 15 checks passed
@ReneWerner87 ReneWerner87 deleted the codex/2025-11-30-20-50-34 branch December 1, 2025 07:29
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
cmd/internal/migrations/v3/common.go (1)

199-251: replaceFieldImpl only tracks " strings; braces/commas inside ' or ` literals may confuse nesting

In the main scan loop, inString is toggled only when ch == '"'. Any braces or commas that appear inside single‑quoted or backtick‑quoted literals will be treated as structural characters, affecting depth and possibly causing the value to extend past its true end or stop too early. Other helpers in this file (splitArgs, extractCall, extractBlock) already handle ", ', and ` uniformly, so it would be more robust to mirror that here.

Consider extending the string handling to all three quote types, e.g. by tracking the current quote byte as in splitArgs/extractCall, so delimiters inside any Go string/rune literal are ignored while scanning for the end of the field value.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4c12080 and cefa1a6.

📒 Files selected for processing (1)
  • cmd/internal/migrations/v3/common.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Build (1.25.x, macos-13)
  • GitHub Check: Build (1.25.x, macos-latest)
  • GitHub Check: Build (1.25.x, windows-latest)
🔇 Additional comments (1)
cmd/internal/migrations/v3/common.go (1)

168-297: Streaming parser in replaceFieldImpl looks sound and fixes the prior re‑matching / infinite‑loop risk

The new implementation walks src once with a monotonic pos, never mutates src in place, and correctly uses end/pos so each field: occurrence is processed at most once. Delimiter handling via depth, skipCommaSuffix, and newline/comma tracking covers nested literals and inline comments without obvious holes for the JWT/TokenLookup use cases, and the TODO path on unquote failure preserves comments and layout. Overall this is a solid upgrade over the previous regex callback approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☢️ Bug Something isn't working codex

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant