Skip to content

Make sure to escape user strings (#2433)#2434

Merged
mromaszewicz merged 1 commit into
oapi-codegen:release/v2.7.xfrom
mromaszewicz:release/v2.7.x
Jul 6, 2026
Merged

Make sure to escape user strings (#2433)#2434
mromaszewicz merged 1 commit into
oapi-codegen:release/v2.7.xfrom
mromaszewicz:release/v2.7.x

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Spec-derived paths and string enum values were interpolated into generated Go source without escaping, so a value containing a quote could produce malformed output. Route paths in the server registration templates and string enum constants are now emitted through strconv.Quote (the existing toGoString helper) so they are always valid, properly-escaped Go string literals. Generated output is unchanged for normal specs.

Spec-derived paths and string enum values were interpolated into generated
Go source without escaping, so a value containing a quote could produce
malformed output. Route paths in the server registration templates and
string enum constants are now emitted through strconv.Quote (the existing
toGoString helper) so they are always valid, properly-escaped Go string
literals. Generated output is unchanged for normal specs.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner July 6, 2026 01:24
@greptile-apps

greptile-apps Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a code-injection bug where spec-derived path strings and string enum values were interpolated raw into Go source, allowing a value containing a double-quote to produce malformed generated code. All seven router-backend templates and constants.tmpl are updated to pipe the relevant values through the existing toGoString helper (strconv.Quote), which adds the surrounding double-quotes and escapes any special characters.

  • All seven router backends (chi, echo, echo/v5, fiber, gin, gorilla, iris, stdhttp) receive the identical fix for route path interpolation, satisfying backend parity.
  • constants.tmpl replaces the raw ValueWrapper sandwich with a conditional toGoString call for string enums, correctly handling values that contain quotes or backslashes.
  • No strict-server templates require changes; they delegate routing to the backend templates and contain no path-registration strings.

Confidence Score: 4/5

Safe to merge — the template changes are mechanically correct, generated output is byte-for-byte identical for normal (no-special-char) specs, and all backends are updated consistently.

The fix is straightforward and low-risk, but there is no new regression test in internal/test/issues/ that exercises the boundary case (a string enum value or route path containing a double-quote). The project’s CI guard (make generate) cannot detect a future re-introduction of the unescaped interpolation without such a fixture.

constants.tmpl — the enum-escaping logic is the more novel part of the change and most benefits from a targeted test fixture.

Important Files Changed

Filename Overview
pkg/codegen/templates/constants.tmpl String enum values now use toGoString when ValueWrapper is set; logic is correct but no regression test exists for enum values containing quote characters.
pkg/codegen/templates/chi/chi-handler.tmpl Route path now passed through toGoString (strconv.Quote) instead of bare interpolation inside quotes — correct fix.
pkg/codegen/templates/echo/echo-register.tmpl Path interpolation fixed with toGoString; analogous to the chi change.
pkg/codegen/templates/echo/v5/echo-register.tmpl Echo v5 template updated identically to Echo v4 — correct parity.
pkg/codegen/templates/fiber/fiber-handler.tmpl Path interpolation fixed with toGoString; analogous to the other backend fixes.
pkg/codegen/templates/gin/gin-register.tmpl Path interpolation fixed with toGoString; analogous to the other backend fixes.
pkg/codegen/templates/gorilla/gorilla-register.tmpl Path interpolation fixed with toGoString; analogous to the other backend fixes.
pkg/codegen/templates/iris/iris-handler.tmpl Path interpolation fixed with toGoString; analogous to the other backend fixes.
pkg/codegen/templates/stdhttp/std-http-handler.tmpl Path interpolation fixed with toGoString; generated output is identical for paths without special characters because strconv.Quote produces the same quoted literal.
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
pkg/codegen/templates/constants.tmpl:12
**Missing regression test for the injection fix**

The project convention (as codified in the review rules and via `internal/test/issues/`) is that bug fixes include a regression test keyed to the issue number. This PR has no new entry under `internal/test/issues/` covering a string enum value that contains a quote (e.g. `fo"o`) or a route path with a backslash or quote. Without such a fixture, CI's `make generate` guard cannot detect a future regression that re-introduces the unescaped interpolation, and there is no `_test.go` exercising the boundary condition.

Reviews (1): Last reviewed commit: "Make sure to escape user strings (#2433)" | Re-trigger Greptile

const (
{{range $name, $value := $Enum.GetValues}}
{{$name}} {{$Enum.TypeName}} = {{$Enum.ValueWrapper}}{{$value}}{{$Enum.ValueWrapper -}}
{{$name}} {{$Enum.TypeName}} = {{if $Enum.ValueWrapper}}{{$value | toGoString}}{{else}}{{$value}}{{end -}}

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.

P2 Missing regression test for the injection fix

The project convention (as codified in the review rules and via internal/test/issues/) is that bug fixes include a regression test keyed to the issue number. This PR has no new entry under internal/test/issues/ covering a string enum value that contains a quote (e.g. fo"o) or a route path with a backslash or quote. Without such a fixture, CI's make generate guard cannot detect a future regression that re-introduces the unescaped interpolation, and there is no _test.go exercising the boundary condition.

Prompt To Fix With AI
This is a comment left during a code review.
Path: pkg/codegen/templates/constants.tmpl
Line: 12

Comment:
**Missing regression test for the injection fix**

The project convention (as codified in the review rules and via `internal/test/issues/`) is that bug fixes include a regression test keyed to the issue number. This PR has no new entry under `internal/test/issues/` covering a string enum value that contains a quote (e.g. `fo"o`) or a route path with a backslash or quote. Without such a fixture, CI's `make generate` guard cannot detect a future regression that re-introduces the unescaped interpolation, and there is no `_test.go` exercising the boundary condition.

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@mromaszewicz
mromaszewicz merged commit 3c4a475 into oapi-codegen:release/v2.7.x Jul 6, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant