-
Notifications
You must be signed in to change notification settings - Fork 210
docs: add security-hardened proxy.ts example and README section #230
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
base: main
Are you sure you want to change the base?
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 | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||||
| import { NextRequest, NextResponse } from 'next/server'; | ||||||||
|
|
||||||||
| /** | ||||||||
| * Security-hardened proxy example for vinext on Cloudflare Workers. | ||||||||
| * | ||||||||
| * This proxy adds: | ||||||||
| * 1. Security response headers (OWASP recommended) | ||||||||
| * 2. Double-encoded path traversal protection | ||||||||
| * | ||||||||
| * See: https://owasp.org/www-project-secure-headers/ | ||||||||
| */ | ||||||||
| export default function proxy(request: NextRequest) { | ||||||||
|
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. Minor: vinext supports both
Suggested change
Actually, |
||||||||
| // Block double-encoded path traversal attempts. | ||||||||
| // %252f = double-encoded '/', %2e%2e = encoded '..', %5c = encoded '\' | ||||||||
|
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 comment says
Suggested change
|
||||||||
| // These can bypass route matching when the server decodes at different stages. | ||||||||
| const rawUrl = request.url; | ||||||||
| if (/%25[0-9a-fA-F]{2}/.test(rawUrl) || /%5[cC]/.test(rawUrl)) { | ||||||||
|
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. This regex tests against the full serialized Also, by the time this code runs, the URL has already been parsed and re-serialized through a Suggestion: either remove this check (vinext handles path traversal built-in via
Suggested change
|
||||||||
| return new NextResponse('Bad Request', { status: 400 }); | ||||||||
| } | ||||||||
|
|
||||||||
| const response = NextResponse.next(); | ||||||||
|
|
||||||||
| // Prevent MIME-type sniffing | ||||||||
| response.headers.set('X-Content-Type-Options', 'nosniff'); | ||||||||
|
|
||||||||
| // Prevent clickjacking — use 'SAMEORIGIN' if you embed your own pages in iframes | ||||||||
| response.headers.set('X-Frame-Options', 'DENY'); | ||||||||
|
|
||||||||
| // Control referrer information sent to other origins | ||||||||
| response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin'); | ||||||||
|
|
||||||||
| // Restrict browser features — customize based on your app's needs | ||||||||
| response.headers.set( | ||||||||
| 'Permissions-Policy', | ||||||||
| 'camera=(), microphone=(), geolocation=()', | ||||||||
| ); | ||||||||
|
|
||||||||
| return response; | ||||||||
| } | ||||||||
|
|
||||||||
| export const config = { | ||||||||
| matcher: [ | ||||||||
| // Match all paths except static assets and vinext internals. | ||||||||
| // This explicit matcher ensures /api/* routes are also covered. | ||||||||
| '/((?!_vinext|_next/static|favicon\\.ico).*)', | ||||||||
| ], | ||||||||
| }; | ||||||||
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.
Nit: the phrase "we recommend" is a strong endorsement that implies this is necessary for security. Since the README sentence right before it says vinext already handles URL normalization, path traversal prevention, and internal header stripping automatically, users might wonder why they need to add a proxy.ts at all.
Consider softening to something like "you may want to add" and clarifying that the built-in protections handle the critical stuff, while these headers are best-practice additions: