Skip to content

Commit 31e08c5

Browse files
Address review feedback on COOKIE_DOMAIN
- Apply Domain attribute to all three Set-Cookie sites, including the origin-response success path that was missed in the first pass. - Move the cookie builder into helpers/misc with input validation: trim whitespace and reject anything other than hostname-safe chars so a malformed env var can't inject into the Set-Cookie header. - Invalid values are logged and ignored (cookie falls back to host-only), keeping the worker resilient to operator typos.
1 parent cc357c9 commit 31e08c5

5 files changed

Lines changed: 43 additions & 27 deletions

File tree

dist/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
This folder contains the built output assets for the worker "crowdhandler-integration" generated at 2026-04-27T13:50:38.241Z.
1+
This folder contains the built output assets for the worker "crowdhandler-integration" generated at 2026-04-27T14:01:55.277Z.

dist/index.js

Lines changed: 20 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helpers/misc.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ const helpers = {
5252
}
5353
return parsedCookie
5454
},
55+
//Build a Set-Cookie value for the crowdhandler token, optionally with a
56+
//Domain attribute so the cookie can be shared across subdomains of the
57+
//configured parent. The raw value is trimmed and validated against
58+
//hostname-safe characters so a typo'd or malicious env var can't inject
59+
//into the Set-Cookie header.
60+
buildCrowdhandlerCookie: function(tokenValue, rawCookieDomain) {
61+
const parts = [`crowdhandler=${tokenValue}`, 'path=/', 'Secure']
62+
if (rawCookieDomain) {
63+
const trimmed = String(rawCookieDomain).trim()
64+
if (/^\.?[a-zA-Z0-9.-]+$/.test(trimmed)) {
65+
parts.push(`Domain=${trimmed}`)
66+
} else {
67+
console.warn(`[CH] Ignoring invalid COOKIE_DOMAIN value: ${JSON.stringify(rawCookieDomain)}`)
68+
}
69+
}
70+
return parts.join('; ')
71+
},
5572
queryStringParse: function(querystring) {
5673
const params = new URLSearchParams(querystring)
5774
let qStrObject = {}

index.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,6 @@ async function handleRequest(request, env, ctx) {
240240
whitelabel = true
241241
}
242242

243-
//Optional cookie Domain attribute. When set, the crowdhandler cookie is shared
244-
//across subdomains of the configured value (e.g. ".barbican.org.uk" makes the
245-
//cookie visible to both tickets.* and spektrix.*). Browsers reject Domain
246-
//values that aren't a parent of the request host, so the operator is
247-
//responsible for setting a valid value.
248-
const cookieDomain = env.COOKIE_DOMAIN || null
249-
const buildCrowdhandlerCookie = (tokenValue) => {
250-
const parts = [`crowdhandler=${tokenValue}`, 'path=/', 'Secure']
251-
if (cookieDomain) parts.push(`Domain=${cookieDomain}`)
252-
return parts.join('; ')
253-
}
254-
255243
if (whitelabel === true) {
256244
waitingRoomDomain = `${host}/ch`
257245
} else {
@@ -360,7 +348,7 @@ async function handleRequest(request, env, ctx) {
360348
//If this is a freshly promoted session, strip the special CrowdHandler parameters by issuing a redirect.
361349
if (freshlyPromoted) {
362350
let setCookie = {
363-
'Set-Cookie': buildCrowdhandlerCookie(token),
351+
'Set-Cookie': helpers.buildCrowdhandlerCookie(token, env.COOKIE_DOMAIN),
364352
}
365353
let redirectLocation
366354
if (queryString) {
@@ -500,7 +488,7 @@ async function handleRequest(request, env, ctx) {
500488
status: 302,
501489
headers: Object.assign(helpers.noCacheHeaders, {
502490
Location: redirectLocation,
503-
'Set-Cookie': buildCrowdhandlerCookie(responseBody.token),
491+
'Set-Cookie': helpers.buildCrowdhandlerCookie(responseBody.token, env.COOKIE_DOMAIN),
504492
}),
505493
})
506494
} else {
@@ -537,7 +525,7 @@ async function handleRequest(request, env, ctx) {
537525
if (validToken.test(responseBody.token) === true) {
538526
modifiedOriginResponse.headers.append(
539527
'set-cookie',
540-
`crowdhandler=${responseBody.token}; path=/; Secure`,
528+
helpers.buildCrowdhandlerCookie(responseBody.token, env.COOKIE_DOMAIN),
541529
)
542530
}
543531
//Set integration method cookie

0 commit comments

Comments
 (0)