Skip to content

🐛 bug: fix regex route constraint parsing with literal >#4292

Merged
ReneWerner87 merged 3 commits into
mainfrom
fix-regex-route-constraint-truncation
May 19, 2026
Merged

🐛 bug: fix regex route constraint parsing with literal >#4292
ReneWerner87 merged 3 commits into
mainfrom
fix-regex-route-constraint-truncation

Conversation

@gaby

@gaby gaby commented May 18, 2026

Copy link
Copy Markdown
Member

Motivation

  • Fix a regression in the parameter parser where the first unescaped > inside a parameter constraint was recorded as the constraint terminator, which truncated regex constraints containing literal > and could degrade them into a no-op noConstraint allowing unintended matches.

Description

  • Update analyseParameterPart in path.go so paramConstraintEndPosition is updated for every unescaped > found, ensuring the closing delimiter of the full constraint is used rather than the first > inside constraint data.
  • Add a regression test in path_testcases_test.go for the pattern /:param<regex(^[^>]+$)> to assert a safe value matches and a value containing > is rejected.
  • Changes affect path.go and path_testcases_test.go and preserve existing parser behavior for all other cases.

Copilot AI review requested due to automatic review settings May 18, 2026 14:40
@gaby gaby requested a review from a team as a code owner May 18, 2026 14:40
@gaby gaby requested review from ReneWerner87, efectn and sixcolors May 18, 2026 14:40
@coderabbitai

coderabbitai Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b83e89d9-7840-4664-9a0e-d4b89eeb38d3

📥 Commits

Reviewing files that changed from the base of the PR and between 91ecf4c and d5bb50b.

📒 Files selected for processing (1)
  • path.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • path.go

Walkthrough

The parameter route parser in path.go no longer guards constraint end-marker detection with a position-already-set check, allowing subsequent unescaped > characters to overwrite the end position. A test case validates this behavior with a regex constraint matching values without > characters while rejecting those containing >.

Changes

Parameter Constraint End-Marker Parsing

Layer / File(s) Summary
Constraint end-marker detection logic
path.go
Remove the paramConstraintEndPosition == -1 guard condition on line 373, allowing the end-marker position to be overwritten when additional unescaped > characters are encountered during parameter type-constraint parsing.
Regex constraint test with > character
path_testcases_test.go
Add a test case on lines 626–632 that validates a regex constraint (^[^>]+$) to verify that route parameter values are correctly accepted (e.g., "safe-value") or rejected (e.g., "bad>value") based on the constraint logic.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • gofiber/fiber#3753: Refactors analyseParameterPart's constraint and end-marker detection logic, directly related to the handling of paramConstraintEndPosition and > detection.
  • gofiber/fiber#3335: Modifies analyseParameterPart's handling of parameter-constraint parsing logic, including how lengths and pattern bytes are computed.

Suggested labels

☢️ Bug, v3

Suggested reviewers

  • sixcolors
  • efectn
  • ReneWerner87

Poem

🐰 A marker once locked in its place,
Now dances to find the right trace—
When > meets > in the fray,
The parser rewrites what should stay,
And tests ensure safe values race!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change as a bug fix for regex route constraint parsing with literal > characters, directly matching the primary issue addressed in the changeset.
Description check ✅ Passed The pull request description covers the motivation and specific changes made. While it omits several template sections (benchmarks, documentation updates, changelog, migration guide, type of change checklist, and commit formatting), the core problem statement and solution are well-documented.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-regex-route-constraint-truncation

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


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.

@ReneWerner87 ReneWerner87 added this to v3 May 18, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone May 18, 2026

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

Copy link
Copy Markdown
Contributor

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 modifies the route parser in path.go to allow multiple closing characters within parameter constraints, which is necessary for regex patterns containing the > character. A corresponding test case was added to path_testcases_test.go. The review feedback identifies a potential panic scenario with malformed routes and suggests adding a check to ensure the constraint start position is set before processing the end character.

Comment thread path.go Outdated
@codecov

codecov Bot commented May 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.32%. Comparing base (dfc64f5) to head (d5bb50b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4292      +/-   ##
==========================================
+ Coverage   91.26%   91.32%   +0.06%     
==========================================
  Files         132      132              
  Lines       12933    12933              
==========================================
+ Hits        11803    11811       +8     
+ Misses        715      708       -7     
+ Partials      415      414       -1     
Flag Coverage Δ
unittests 91.32% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a route parameter constraint parsing regression where the parser treated the first unescaped > inside a <...> constraint as the terminator, truncating regex constraints that contain literal > and potentially degrading them into an unintended no-op constraint.

Changes:

  • Update analyseParameterPart to keep updating paramConstraintEndPosition for each unescaped > so the final closing delimiter is used.
  • Add a regression testcase covering a regex constraint that contains a literal > ([^>]+) and verifying correct match/reject behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
path.go Adjusts constraint end parsing to use the last unescaped > within the constraint instead of the first.
path_testcases_test.go Adds a regression testcase ensuring regex constraints with literal > are parsed and enforced correctly.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

path.go:380

  • paramConstraintEndPosition is set as soon as the first unescaped > is encountered after <, which makes the parser treat the constraint as “closed” for the parameterEndChars check. If the constraint body contains a > followed later by - or . (both are parameterEndChars), the loop can break before reaching the real closing >, leaving the constraint truncated/ignored. Consider only accepting > as the constraint terminator when it’s followed by a parameter boundary (e.g., end-of-string or a parameterEndChars rune like /, ?, -, .), so internal > inside regex data (e.g. :p<regex(^foo>bar-baz$)>) doesn’t prematurely “close” the constraint.
		for i := range search {
			if paramConstraintStartPosition == -1 && search[i] == paramConstraintStart && (i == 0 || search[i-1] != escapeChar) {
				paramConstraintStartPosition = i + 1
				continue
			}
if paramConstraintStartPosition != -1 && search[i] == paramConstraintEnd && (i == 0 || search[i-1] != escapeChar) {
				paramConstraintEndPosition = i + 1
				continue
			}
			if parameterEndChars[search[i]] {
				if (paramConstraintStartPosition == -1 && paramConstraintEndPosition == -1) ||
					(paramConstraintStartPosition != -1 && paramConstraintEndPosition != -1) {
					paramEndPosition = i

Comment thread path.go Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@path.go`:
- Line 373: The if-statement line containing paramConstraintStartPosition,
search, paramConstraintEnd and escapeChar is not gofmt-formatted; run gofmt -w
path.go (or use your IDE's format command / goimports) to reformat the file so
the if-condition line is corrected and CI will pass, ensuring the expression
spanning "if paramConstraintStartPosition != -1 && search[i] ==
paramConstraintEnd && (i == 0 || search[i-1] != escapeChar) {" is properly
formatted.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: db0d0d58-e554-4002-83ba-3f2f3cff9c7b

📥 Commits

Reviewing files that changed from the base of the PR and between 422f768 and 91ecf4c.

📒 Files selected for processing (1)
  • path.go

Comment thread path.go Outdated

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 422f768 Previous: dfc64f5 Ratio
Benchmark_Compress/Zstd (github.com/gofiber/fiber/v3/middleware/compress) - B/op 1 B/op 0 B/op +∞

This comment was automatically generated by workflow using github-action-benchmark.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@gaby gaby removed the aardvark label May 18, 2026
@ReneWerner87 ReneWerner87 merged commit 1987a06 into main May 19, 2026
21 checks passed
@ReneWerner87 ReneWerner87 deleted the fix-regex-route-constraint-truncation branch May 19, 2026 07:11
@github-project-automation github-project-automation Bot moved this to Done in v3 May 19, 2026
@ReneWerner87 ReneWerner87 removed this from the v3 milestone May 22, 2026
@ReneWerner87 ReneWerner87 added this to the v3.3.0 milestone May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants