Skip to content

Add statically analyzed Go route and proxy configuration - #101551

Open
timneutkens wants to merge 109 commits into
canaryfrom
codex/proxy-go-config
Open

timneutkens wants to merge 109 commits into
canaryfrom
codex/proxy-go-config

Conversation

@timneutkens

@timneutkens timneutkens commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What?

Add static configuration for experimental Go routes and proxies. Stacked on #101874.

// route.go: no SDK import needed.
const MaxDuration = 30
// proxy.go
import "next/go/proxy"

var Config = proxy.Config{
    Matcher: []proxy.Matcher{
        {Source: "/account/:path*", Has: []proxy.Condition{{Type: "header", Key: "x-auth"}}},
    },
}

Why?

Go handlers need duration and matching controls without executing application code during discovery. Static proxy.Config is separate from per-request options such as proxy.WithRequestHeaders. Invalid proxy configuration must not let requests bypass the proxy or terminate the development server.

How?

Parse entry-file literal declarations and publish validated settings through native descriptors, manifests, routing, and adapter output. Recognize the exact next/go/proxy import with default or named aliases; reject computed configuration and invalid duration types.

When a native proxy fails compilation or static Config analysis, install catch-all/error state without reading a stale descriptor. Requests, including native Go routes, fail until successful recovery. The filesystem watcher preserves the newest native matchers at its commit point rather than overwriting failure or recovery state with an earlier snapshot. Stale Edge manifests cannot overwrite native proxy state.

The Go loader opts into throwing on invalid expanded matchers instead of calling process.exit; existing JavaScript callers keep their current behavior. The SDK remains one module with config types beside its proxy helpers.

Verification

  • All 64 targeted tests passed in an isolated Jest/SWC source runner: 37 SDK setup, 19 native-runtime, and 8 matcher/config tests.
  • Deterministic actual-source hook/watcher checks passed initial failure, restrictive-to-invalid-to-valid edits, stale descriptors/manifests, both watcher interleavings, JS replacement, and proxy-removal controls. Removing the watcher fix made both interleavings fail again.
  • The 4,090-character matcher regression validates the literal but rejects its expanded pattern without exiting, then recovers with valid configuration. Existing callers still take their original exit path.
  • Native e2e coverage was extended for compilation, static-analysis, and expanded-matcher failures and recovery, with bounded retries and explicit timeouts.
  • Independent re-review found the watcher race resolved.
  • Not run locally: full Next.js builds/typechecking and native e2e suites; framework dependencies/build outputs are not bootstrapped.

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: f082284
View workflow run

@timneutkens
timneutkens force-pushed the codex/proxy-go-config branch from 8ac86ad to b37fd4e Compare November 11, 2026 13:51
vercel[bot]
vercel Bot previously approved these changes Nov 11, 2026

@vercel vercel 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.

Vercel Agent reviewed b37fd4e and found no issues.

@timneutkens
timneutkens force-pushed the codex/proxy-go-config branch from b37fd4e to a4d00a7 Compare November 11, 2026 14:28
vercel[bot]
vercel Bot previously approved these changes Nov 11, 2026

@vercel vercel 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.

Vercel Agent reviewed a4d00a7 and found no issues.

@timneutkens
timneutkens force-pushed the codex/proxy-go-config branch from a4d00a7 to ce6cb53 Compare November 11, 2026 15:04
vercel[bot]
vercel Bot previously approved these changes Nov 11, 2026

@vercel vercel 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.

Vercel Agent reviewed ce6cb53 and found no issues.

@timneutkens
timneutkens force-pushed the codex/proxy-go-config branch from ce6cb53 to 5f85fe1 Compare November 11, 2026 15:25
vercel[bot]
vercel Bot previously approved these changes Nov 11, 2026

@vercel vercel 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.

Vercel Agent reviewed 5f85fe1 and found no issues.

@timneutkens
timneutkens force-pushed the codex/proxy-go-config branch from 5f85fe1 to 84c1fce Compare November 11, 2026 15:27
vercel[bot]
vercel Bot previously approved these changes Nov 11, 2026

@vercel vercel 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.

Vercel Agent reviewed 84c1fce and found no issues.

@timneutkens
timneutkens force-pushed the codex/proxy-go-config branch from 84c1fce to fbec3b9 Compare November 11, 2026 15:33
vercel[bot]
vercel Bot previously approved these changes Nov 11, 2026

@vercel vercel 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.

Vercel Agent reviewed fbec3b9 and found no issues.

@timneutkens
timneutkens force-pushed the codex/proxy-go-config branch from fbec3b9 to 015ab18 Compare November 11, 2026 16:53
@timneutkens

Copy link
Copy Markdown
Contributor Author

Addressed both questions in the latest review summary in 120eea4:

  • Sibling Proxy functions: rejecting these was unintended. Handler discovery now scans all compiled files for both routes and proxies; static configuration remains entrypoint-only. The existing src proxy fixture now declares Proxy in proxy-handler.go, covering this through its dev/start and standalone cases. Updated the docs accordingly.
  • Duplicate compile diagnostics: the hook now retains the compiler's existing error/fatal issue without adding or logging a generic duplicate. It still blocks requests and installs catch-all matchers while compilation fails. A fallback issue remains when the compiler supplied no error, and descriptor/matcher validation still reports Invalid proxy.go Config.

Focused verification passed: the actual Go parser/SDK/generated proxy wrapper served continuations, rewrites, and header overrides with the sibling handler; an actual-source hook check reproduced two overlay issues before the fix and one afterward, including fallback/config errors, fail-closed routing, and recovery. Rust formatting, Go formatting, Prettier, and ESLint passed.

Full Next.js typechecking is blocked by missing dependencies in this checkout. Full native e2e tests were not run without a rebuilt native binding.

Base automatically changed from codex/go-sdk to canary November 26, 2026 12:56
@timneutkens
timneutkens force-pushed the codex/proxy-go-config branch from 4a0bd9e to 3cf4151 Compare November 26, 2026 13:01
Comment thread crates/next-api/src/route.rs Resolved
vercel[bot]
vercel Bot previously requested changes Nov 26, 2026
@vercel
vercel Bot dismissed their stale review November 26, 2026 16:50

Vercel Agent Verified Review reviewed a newer commit and no longer requests changes: dismissing its change request on an earlier commit.

@vercel
vercel Bot requested a review from bgw November 26, 2026 16:50
Comment on lines +44 to +45
// Go proxies can protect either runtime. A condition cannot safely
// select a resource when Go and JavaScript would see different values.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this correctly error even when deployed with the Vercel adapter?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🤖 Confirmed: adapter runtimes bypass this Node guard. ea0c26c now fails adapter builds for Go proxy Has/Missing header or cookie conditions until adapters can enforce the same checks. Coverage verifies all four combinations fail and source-only adapter matchers still build. Header/cookie conditions remain supported by next dev and self-hosted next start; the restriction is documented.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems like a footgun/security problem nevertheless?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🤖 To clarify the enforcement: this is a hard build failure before onBuildComplete emits adapter output whenever proxy.go declares header or cookie Has/Missing conditions. Such a configuration cannot be deployed through adapterPath; all four combinations are tested. Source-only adapter matchers remain available. Self-hosted requests use the 400 guard. Can you point out the remaining scenario you are concerned about? Leaving this discussion open for that clarification.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🤖 Restored header/cookie Has and Missing support in bf06ec6. The generated adapter listener now validates the relevant headers in both the Go proxy and every Go route, so skipping the proxy cannot bypass validation.

The adapter regression uses the ordinary JavaScript matcher without the Node Go-header guard. It verifies that the destination Go route rejects ambiguous duplicate and Connection-nominated headers while the proxy invocation count stays unchanged. All four condition combinations work; repeated Cookie fields and unrelated repeated headers remain supported.

Unit, dev/start config, security, Go route, type, and clippy checks pass. Leaving this open for re-review.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🤖 Follow-up in f082284 keeps cookie Has/Missing supported and emits additional matchers that send repeated condition-cookie names through validation, including before JavaScript destinations. The Go entrypoint rejects these requests before calling the application's Proxy function.

Added regressions for both Has and Missing using the emitted JavaScript handlers. Local Prettier, ESLint, and Rust formatting checks pass; the updated runtime tests still need CI verification. Leaving this open for re-review.

Comment thread crates/next-api/src/route.rs Resolved
serverFields.goProxyConfigError = message
matchers = [{ regexp: '^/.*$', originalSource: '/:path*' }]
}
serverFields.middleware = {

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.

Note

Unverified · Major — Dev render server keeps stale proxy.go matchers after a Config edit

loadNativeMiddleware puts the new proxy.go matchers in serverFields.middleware and the router's fsChecker, but never calls propagateServerField('middleware', ...). The render server's DevServer.middleware is set only when the watcher propagates, at setup-dev-bundler.ts:1352. A proxy.go edit fires the watcher quickly, but Turbopack has to finish the Go compile first, so the watcher usually propagates the old matchers. When the Turbopack compile finishes last, the router starts invoking the proxy for a newly matched path (for example after changing /account/:path* to /admin/:path*). The render server then re-checks the path against the old matchers in handleCatchallMiddlewareRequest, takes handleFinished(), and the route is served without running proxy.go. This lasts until some unrelated file change triggers the watcher again. The PR's own 'updates the proxy matcher after a literal edit' e2e test (edit to Source: "/", expect / to be matched) hits this ordering.

Needs your judgment: the review traced this through the code but couldn’t run anything to prove it.

This branch has not been deployed

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

Labels

created-by: Turbopack team PRs by the Turbopack team. Documentation Related to Next.js' official documentation. tests Turbopack Related to Turbopack with Next.js. type: next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants