Skip to content

Commit

Permalink
be more conservative with spotting q= in URL (github#21945)
Browse files Browse the repository at this point in the history
* be more conservative with spotting q= in URL

Fixes github/docs-engineering#951

* refactor check

* Update tests/routing/redirects.js

Co-authored-by: Rachael Sewell <[email protected]>

Co-authored-by: Rachael Sewell <[email protected]>
  • Loading branch information
peterbe and rachmari authored Oct 18, 2021
1 parent 1f71206 commit c712da8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions middleware/redirects/handle-redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ export default function handleRedirects(req, res, next) {
let redirect = req.path
let queryParams = req._parsedUrl.query

// update old-style query params (#9467)
// have to do this now because searchPath replacement changes the path as well as the query params
if (queryParams) {
queryParams = '?' + queryParams.replace('q=', 'query=')
// update old-style query params (#9467)
if ('q' in req.query) {
queryParams = queryParams.replace('q=', 'query=')
}
queryParams = '?' + queryParams
redirect = (redirect + queryParams).replace(patterns.searchPath, '$1')
}

Expand Down
9 changes: 9 additions & 0 deletions tests/routing/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ describe('redirects', () => {
expect(res.headers.location).toBe(expected)
})

test('have faq= not converted to query=', async () => {
// Don't confuse `?faq=` for `?q=` just because they both start with `q=`
// Docs internal #21945
const res = await get('/en/enterprise/admin?faq=pulls')
expect(res.statusCode).toBe(301)
const expected = `/en/enterprise-server@${enterpriseServerReleases.latest}/admin?faq=pulls`
expect(res.headers.location).toBe(expected)
})

test('work with redirected search paths', async () => {
const res = await get('/en/enterprise/admin/search?utf8=%E2%9C%93&query=pulls')
expect(res.statusCode).toBe(301)
Expand Down

0 comments on commit c712da8

Please sign in to comment.