Skip to content

Commit 27b9bc2

Browse files
authored
fix(den): return proxy-safe base URL for desktop handoff (anomalyco#990)
1 parent 32a51b2 commit 27b9bc2

2 files changed

Lines changed: 114 additions & 11 deletions

File tree

‎services/den-v2/src/http/desktop-auth.ts‎

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,62 @@ const exchangeGrantSchema = z.object({
1919
grant: z.string().trim().min(12).max(128),
2020
})
2121

22+
function readSingleHeader(value: unknown): string | null {
23+
if (typeof value !== "string") {
24+
return null
25+
}
26+
const first = value.split(",")[0]?.trim() ?? ""
27+
return first || null
28+
}
29+
30+
function isWebAppHost(hostname: string): boolean {
31+
const normalized = hostname.trim().toLowerCase()
32+
return normalized === "app.openworklabs.com"
33+
|| normalized === "app.openwork.software"
34+
|| normalized.startsWith("app.")
35+
}
36+
37+
function withDenProxyPath(origin: string): string {
38+
const url = new URL(origin)
39+
const pathname = url.pathname.replace(/\/+$/, "")
40+
if (pathname.toLowerCase().endsWith("/api/den")) {
41+
return url.toString().replace(/\/+$/, "")
42+
}
43+
url.pathname = `${pathname}/api/den`.replace(/\/+/g, "/")
44+
return url.toString().replace(/\/+$/, "")
45+
}
46+
47+
function resolveDesktopDenBaseUrl(req: express.Request): string {
48+
const originHeader = readSingleHeader(req.headers.origin)
49+
if (originHeader) {
50+
try {
51+
const originUrl = new URL(originHeader)
52+
if ((originUrl.protocol === "https:" || originUrl.protocol === "http:") && isWebAppHost(originUrl.hostname)) {
53+
return withDenProxyPath(originUrl.origin)
54+
}
55+
} catch {}
56+
}
57+
58+
const forwardedProto = readSingleHeader(req.headers["x-forwarded-proto"])
59+
const forwardedHost = readSingleHeader(req.headers["x-forwarded-host"])
60+
const host = readSingleHeader(req.headers.host)
61+
const protocol = forwardedProto ?? req.protocol ?? "https"
62+
const targetHost = forwardedHost ?? host
63+
if (!targetHost) {
64+
return "https://app.openworklabs.com/api/den"
65+
}
66+
67+
const origin = `${protocol}://${targetHost}`
68+
try {
69+
const url = new URL(origin)
70+
if (isWebAppHost(url.hostname)) {
71+
return withDenProxyPath(url.origin)
72+
}
73+
} catch {}
74+
75+
return origin
76+
}
77+
2278
function buildOpenworkDeepLink(input: {
2379
scheme?: string | null
2480
grant: string
@@ -59,13 +115,7 @@ desktopAuthRouter.post(
59115
consumed_at: null,
60116
})
61117

62-
const forwardedProto =
63-
typeof req.headers["x-forwarded-proto"] === "string"
64-
? req.headers["x-forwarded-proto"]
65-
: null
66-
const host = typeof req.headers.host === "string" ? req.headers.host : null
67-
const protocol = forwardedProto ?? req.protocol ?? "https"
68-
const denBaseUrl = host ? `${protocol}://${host}` : "https://app.openworklabs.com"
118+
const denBaseUrl = resolveDesktopDenBaseUrl(req)
69119

70120
res.json({
71121
grant,

‎services/den/src/http/desktop-auth.ts‎

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,62 @@ const exchangeGrantSchema = z.object({
1919
grant: z.string().trim().min(12).max(128),
2020
})
2121

22+
function readSingleHeader(value: unknown): string | null {
23+
if (typeof value !== "string") {
24+
return null
25+
}
26+
const first = value.split(",")[0]?.trim() ?? ""
27+
return first || null
28+
}
29+
30+
function isWebAppHost(hostname: string): boolean {
31+
const normalized = hostname.trim().toLowerCase()
32+
return normalized === "app.openworklabs.com"
33+
|| normalized === "app.openwork.software"
34+
|| normalized.startsWith("app.")
35+
}
36+
37+
function withDenProxyPath(origin: string): string {
38+
const url = new URL(origin)
39+
const pathname = url.pathname.replace(/\/+$/, "")
40+
if (pathname.toLowerCase().endsWith("/api/den")) {
41+
return url.toString().replace(/\/+$/, "")
42+
}
43+
url.pathname = `${pathname}/api/den`.replace(/\/+/g, "/")
44+
return url.toString().replace(/\/+$/, "")
45+
}
46+
47+
function resolveDesktopDenBaseUrl(req: express.Request): string {
48+
const originHeader = readSingleHeader(req.headers.origin)
49+
if (originHeader) {
50+
try {
51+
const originUrl = new URL(originHeader)
52+
if ((originUrl.protocol === "https:" || originUrl.protocol === "http:") && isWebAppHost(originUrl.hostname)) {
53+
return withDenProxyPath(originUrl.origin)
54+
}
55+
} catch {}
56+
}
57+
58+
const forwardedProto = readSingleHeader(req.headers["x-forwarded-proto"])
59+
const forwardedHost = readSingleHeader(req.headers["x-forwarded-host"])
60+
const host = readSingleHeader(req.headers.host)
61+
const protocol = forwardedProto ?? req.protocol ?? "https"
62+
const targetHost = forwardedHost ?? host
63+
if (!targetHost) {
64+
return "https://app.openworklabs.com/api/den"
65+
}
66+
67+
const origin = `${protocol}://${targetHost}`
68+
try {
69+
const url = new URL(origin)
70+
if (isWebAppHost(url.hostname)) {
71+
return withDenProxyPath(url.origin)
72+
}
73+
} catch {}
74+
75+
return origin
76+
}
77+
2278
function buildOpenworkDeepLink(input: { scheme?: string | null; grant: string; denBaseUrl: string }) {
2379
const requestedScheme = input.scheme?.trim() || "openwork"
2480
const scheme = /^[a-z][a-z0-9+.-]*$/i.test(requestedScheme) ? requestedScheme : "openwork"
@@ -51,10 +107,7 @@ desktopAuthRouter.post("/desktop-handoff", asyncRoute(async (req, res) => {
51107
consumed_at: null,
52108
})
53109

54-
const forwardedProto = typeof req.headers["x-forwarded-proto"] === "string" ? req.headers["x-forwarded-proto"] : null
55-
const host = typeof req.headers.host === "string" ? req.headers.host : null
56-
const protocol = forwardedProto ?? req.protocol ?? "https"
57-
const denBaseUrl = host ? `${protocol}://${host}` : "https://app.openworklabs.com"
110+
const denBaseUrl = resolveDesktopDenBaseUrl(req)
58111
res.json({
59112
grant,
60113
expiresAt: expiresAt.toISOString(),

0 commit comments

Comments
 (0)