Skip to content

Commit

Permalink
Before XHR URLs are whitelisted, strip query params and hashes (#7742)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifer-shehane authored Jun 19, 2020
1 parent a2d2c8d commit 869bcec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/driver/cypress/integration/commands/xhr_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2296,6 +2296,34 @@ describe('src/cy/commands/xhr', () => {
expect(resp).to.eq('{ \'bar\' }\n')
})
})

// https://github.com/cypress-io/cypress/issues/7280
it('ignores query params when whitelisting routes', () => {
cy.server()
cy.route(/url-with-query-param/, { foo: 'bar' }).as('getQueryParam')
cy.window().then((win) => {
win.$.get('/url-with-query-param?resource=foo.js')

return null
})

cy.wait('@getQueryParam').its('response.body')
.should('deep.equal', { foo: 'bar' })
})

// https://github.com/cypress-io/cypress/issues/7280
it('ignores hashes when whitelisting routes', () => {
cy.server()
cy.route(/url-with-hash/, { foo: 'bar' }).as('getHash')
cy.window().then((win) => {
win.$.get('/url-with-hash#foo.js')

return null
})

cy.wait('@getHash').its('response.body')
.should('deep.equal', { foo: 'bar' })
})
})

describe('route setup', () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/driver/src/cypress/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,16 @@ const warnOnForce404Default = (obj) => {
}

const whitelist = (xhr) => {
const url = new URL(xhr.url)

// https://github.com/cypress-io/cypress/issues/7280
// we want to strip the xhr's URL of any hash and query params before
// checking the REGEX for matching file extensions
url.search = ''
url.hash = ''

// whitelist if we're GET + looks like we're fetching regular resources
return xhr.method === 'GET' && regularResourcesRe.test(xhr.url)
return xhr.method === 'GET' && regularResourcesRe.test(url.href)
}

const serverDefaults = {
Expand Down

4 comments on commit 869bcec

@cypress-bot

This comment was marked as off-topic.

@cypress-bot

This comment was marked as off-topic.

@cypress-bot

This comment was marked as off-topic.

@cypress-bot

This comment was marked as off-topic.

Please sign in to comment.