Skip to content

Commit 77f298f

Browse files
Peter Bengtssonheiskrrofergjmarlenadocubot
authored
301 redirects should not set-cookie (#23360)
* 301 redirects should not set-cookie Part of #1316 * Update release-notes.js * No public facing doc changes (#23355) Co-authored-by: jmarlena <6732600+jmarlena@users.noreply.github.com> * New translation batch for pt (#23362) * Add crowdin translations * Run script/i18n/homogenize-frontmatter.js * Run script/i18n/fix-translation-errors.js * Run script/i18n/lint-translation-files.js --check parsing * Run script/i18n/lint-translation-files.js --check rendering * run script/i18n/reset-known-broken-translation-files.js * Check in pt CSV report * 24h cache all internal redirects without language prefix (#23354) Part of #1271 * Redirects for root path should not set-cookie in all supported languages (#23377) * test: add case when unsupported language is specified in redirect * test: assert root redirects to appropriate language preference if specified * refactor: remove redundant unit test * refactor: re-order units starting with affirmative case * refactor: remove single-use variable from unit * fix: re-include previous unit with udpated description Co-authored-by: Octomerger Bot <63058869+Octomerger@users.noreply.github.com> Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com> Co-authored-by: Rogan Ferguson <40493721+roferg@users.noreply.github.com> Co-authored-by: jmarlena <6732600+jmarlena@users.noreply.github.com> Co-authored-by: docubot <67483024+docubot@users.noreply.github.com> Co-authored-by: Francis <15894826+francisfuzz@users.noreply.github.com> Co-authored-by: Octomerger Bot <63058869+Octomerger@users.noreply.github.com>
1 parent 0142b04 commit 77f298f

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

middleware/redirects/external.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const externalSites = readJsonFile('./lib/redirects/external-sites.json')
44
// blanket redirects to external websites
55
export default function externalRedirects(req, res, next) {
66
if (req.path in externalSites) {
7+
// Undo the cookie setting that CSRF sets.
8+
res.removeHeader('set-cookie')
79
return res.redirect(301, externalSites[req.path])
810
} else {
911
return next()

middleware/redirects/handle-redirects.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ export default function handleRedirects(req, res, next) {
2323
language = req.context.userLanguage
2424
}
2525

26+
// Undo the cookie setting that CSRF sets.
27+
res.removeHeader('set-cookie')
28+
2629
noCacheControl(res)
30+
2731
return res.redirect(302, `/${language}`)
2832
}
2933

@@ -67,6 +71,9 @@ export default function handleRedirects(req, res, next) {
6771
return next()
6872
}
6973

74+
// Undo the cookie setting that CSRF sets.
75+
res.removeHeader('set-cookie')
76+
7077
// do the redirect if the from-URL already had a language in it
7178
if (pathLanguagePrefixed(req.path)) {
7279
cacheControl(res)

tests/rendering/server.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import CspParse from 'csp-parse'
77
import { productMap } from '../../lib/all-products.js'
88
import { SURROGATE_ENUMS } from '../../middleware/set-fastly-surrogate-key.js'
99
import { jest } from '@jest/globals'
10+
import { languageKeys } from '../../lib/languages.js'
1011

1112
const AZURE_STORAGE_URL = 'githubdocs.azureedge.net'
1213
const activeProducts = Object.values(productMap).filter(
@@ -610,6 +611,7 @@ describe('server', () => {
610611
test('redirects old articles to their English URL', async () => {
611612
const res = await get('/articles/deleting-a-team')
612613
expect(res.statusCode).toBe(302)
614+
expect(res.headers['set-cookie']).toBeUndefined()
613615
// no cache control because a language prefix had to be injected
614616
expect(res.headers['cache-control']).toBeUndefined()
615617
})
@@ -621,17 +623,48 @@ describe('server', () => {
621623
)
622624
})
623625

624-
test('redirects / to /en', async () => {
626+
test('redirects / to /en when no language preference is specified', async () => {
625627
const res = await get('/')
626628
expect(res.statusCode).toBe(302)
627629
expect(res.headers.location).toBe('/en')
628630
expect(res.headers['cache-control']).toBe('private, no-store')
631+
expect(res.headers['set-cookie']).toBeUndefined()
632+
})
633+
634+
test('redirects / to appropriate language preference if specified', async () => {
635+
await Promise.all(
636+
languageKeys.map(async (languageKey) => {
637+
const res = await get('/', {
638+
headers: {
639+
'accept-language': `${languageKey}`,
640+
},
641+
})
642+
expect(res.statusCode).toBe(302)
643+
expect(res.headers.location).toBe(`/${languageKey}`)
644+
expect(res.headers['cache-control']).toBe('private, no-store')
645+
expect(res.headers['set-cookie']).toBeUndefined()
646+
})
647+
)
648+
})
649+
650+
test('redirects / to /en when unsupported language preference is specified', async () => {
651+
const res = await get('/', {
652+
headers: {
653+
// Tagalog: https://www.loc.gov/standards/iso639-2/php/langcodes_name.php?iso_639_1=tl
654+
'accept-language': 'tl',
655+
},
656+
})
657+
expect(res.statusCode).toBe(302)
658+
expect(res.headers.location).toBe('/en')
659+
expect(res.headers['cache-control']).toBe('private, no-store')
660+
expect(res.headers['set-cookie']).toBeUndefined()
629661
})
630662

631663
test('adds English prefix to old article URLs', async () => {
632664
const res = await get('/articles/deleting-a-team')
633665
expect(res.statusCode).toBe(302)
634666
expect(res.headers.location.startsWith('/en/')).toBe(true)
667+
expect(res.headers['set-cookie']).toBeUndefined()
635668
expect(res.headers['cache-control']).toBeUndefined()
636669
})
637670

@@ -646,6 +679,7 @@ describe('server', () => {
646679
const res = await get('/desktop-classic')
647680
expect(res.statusCode).toBe(301)
648681
expect(res.headers.location).toBe('https://desktop.github.com')
682+
expect(res.headers['set-cookie']).toBeUndefined()
649683
expect(res.headers['cache-control']).toBeUndefined()
650684
})
651685

0 commit comments

Comments
 (0)