Skip to content

Commit c2eacd7

Browse files
authored
fix(console): secure server action redirects (#45374)
1 parent c5ef753 commit c2eacd7

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function sanitizeServerActionRequest(request: Request) {
2+
const requestUrl = new URL(request.url)
3+
if (requestUrl.pathname !== "/_server") return request
4+
5+
const referer = request.headers.get("referer")
6+
if (referer && URL.canParse(referer) && new URL(referer).origin === requestUrl.origin) return request
7+
8+
const sanitized = new Request(request)
9+
sanitized.headers.set("referer", requestUrl.origin)
10+
return sanitized
11+
}

‎packages/console/app/src/middleware.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { createMiddleware } from "@solidjs/start/middleware"
22
import { LOCALE_HEADER, cookie, fromPathname, strip } from "~/lib/language"
33
import { normalizeReferralCode, referralCookie } from "~/lib/referral-invite"
4+
import { sanitizeServerActionRequest } from "~/lib/server-action"
45

56
export default createMiddleware({
67
onRequest(event) {
8+
event.request = sanitizeServerActionRequest(event.request)
9+
710
const url = new URL(event.request.url)
811
const locale = fromPathname(url.pathname)
912
if (locale) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { sanitizeServerActionRequest } from "../src/lib/server-action"
3+
4+
describe("server action referer", () => {
5+
test("preserves same-origin return locations", () => {
6+
const request = new Request("https://dev.opencode.ai/_server?id=action", {
7+
headers: { referer: "https://dev.opencode.ai/auth?next=%2Fconsole" },
8+
})
9+
10+
expect(sanitizeServerActionRequest(request)).toBe(request)
11+
})
12+
13+
test("replaces unsafe return locations with the request origin", () => {
14+
const referers = ["https://evil.example/phishing-login", "not a url", undefined]
15+
16+
expect(
17+
referers.map((referer) =>
18+
sanitizeServerActionRequest(
19+
new Request("https://dev.opencode.ai/_server?id=action", {
20+
headers: referer === undefined ? undefined : { referer },
21+
}),
22+
).headers.get("referer"),
23+
),
24+
).toEqual(["https://dev.opencode.ai", "https://dev.opencode.ai", "https://dev.opencode.ai"])
25+
})
26+
27+
test("does not change other routes", () => {
28+
const request = new Request("https://dev.opencode.ai/auth", {
29+
headers: { referer: "https://evil.example/phishing-login" },
30+
})
31+
32+
expect(sanitizeServerActionRequest(request)).toBe(request)
33+
})
34+
})

0 commit comments

Comments
 (0)