Skip to content

Commit 21271cb

Browse files
committed
remove 'SameSite=none' together with 'secure'. Fixes #14
1 parent 6376ae8 commit 21271cb

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ function proxyRequest (route, mw, lws) {
139139
/* On insecure connections, remove `secure` attribute from remote cookies */
140140
const setCookies = remoteRes.headers['set-cookie']
141141
if (!ctx.req.socket.encrypted && !lws.config.rewriteKeepSecureAttr && setCookies && setCookies.length) {
142-
const cookies = setCookies.map(c => util.removeCookieAttribute(c, 'secure'))
142+
const cookies = setCookies.map(c => {
143+
let result = util.removeCookieAttribute(c, 'secure')
144+
if (/samesite=none/.test(result)) {
145+
result = util.removeCookieAttribute(result, 'samesite=none')
146+
}
147+
return result
148+
})
143149
remoteRes.headers['set-cookie'] = cookies
144150
}
145151

test/remote.js

+68
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,39 @@ tom.test('GET HTTPS, secure cookie attribute set - remove it', async function ()
273273
}
274274
}, { timeout: 120000 })
275275

276+
tom.test('GET HTTPS, `secure` and `SameSite=none` attributes set - remove them both', async function () {
277+
class SecureCookie {
278+
middleware (config, lws) {
279+
return function (ctx, next) {
280+
const secure = true
281+
ctx.cookies.set('test', 'one', { secure, sameSite: 'none' })
282+
ctx.body = 'test'
283+
}
284+
}
285+
}
286+
const remotePort = 10000 + this.index
287+
const remoteLws = await Lws.create({
288+
port: remotePort,
289+
https: true,
290+
stack: [SecureCookie]
291+
})
292+
293+
const port = 8100 + this.index
294+
const lws = await Lws.create({
295+
port,
296+
stack: [Rewrite, Static],
297+
rewrite: { from: '/', to: `https://localhost:${remotePort}/` }
298+
})
299+
try {
300+
const response = await fetch(`http://localhost:${port}/`)
301+
a.strictEqual(response.status, 200)
302+
a.strictEqual(response.headers.get('set-cookie'), 'test=one; path=/; httponly')
303+
} finally {
304+
lws.server.close()
305+
remoteLws.server.close()
306+
}
307+
}, { timeout: 120000 })
308+
276309
tom.test('GET HTTPS, --rewrite.keep-secure-attr', async function () {
277310
class SecureCookie {
278311
middleware (config, lws) {
@@ -342,4 +375,39 @@ tom.test('GET HTTPS, --rewrite.keep-secure-attr, multiple cookies', async functi
342375
}
343376
}, { timeout: 120000 })
344377

378+
tom.test('GET HTTPS, --rewrite.keep-secure-attr keeps sameSite value too, multiple cookies', async function () {
379+
class SecureCookie {
380+
middleware (config, lws) {
381+
return function (ctx, next) {
382+
const secure = true
383+
ctx.cookies.set('test', 'one', { secure, sameSite: 'none' })
384+
ctx.cookies.set('test2', 'two', { secure, sameSite: 'none' })
385+
ctx.body = 'test'
386+
}
387+
}
388+
}
389+
const remotePort = 10000 + this.index
390+
const remoteLws = await Lws.create({
391+
port: remotePort,
392+
https: true,
393+
stack: [SecureCookie]
394+
})
395+
396+
const port = 8100 + this.index
397+
const lws = await Lws.create({
398+
port,
399+
stack: [Rewrite, Static],
400+
rewrite: { from: '/', to: `https://localhost:${remotePort}/` },
401+
rewriteKeepSecureAttr: true
402+
})
403+
try {
404+
const response = await fetch(`http://localhost:${port}/`)
405+
a.strictEqual(response.status, 200)
406+
a.strictEqual(response.headers.get('set-cookie'), 'test=one; path=/; samesite=none; secure; httponly, test2=two; path=/; samesite=none; secure; httponly')
407+
} finally {
408+
lws.server.close()
409+
remoteLws.server.close()
410+
}
411+
}, { timeout: 120000 })
412+
345413
export default tom

0 commit comments

Comments
 (0)