Skip to content

Commit

Permalink
fix: handle malformed URIs in prerequests
Browse files Browse the repository at this point in the history
  • Loading branch information
mschile committed Dec 13, 2023
1 parent b71ce44 commit 0a62d01
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 356 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"json"
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"typescript.tsdk": "node_modules/typescript/lib",

Expand Down
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ _Released 12/19/2023 (PENDING)_

**Bugfixes:**

- Fixed a regression in [`13.6.1`](https://docs.cypress.io/guides/references/changelog/13.6.1) where a malformed URI would crash Cypress. Fixes [#28521](https://github.com/cypress-io/cypress/issues/28521).
- Fixed a regression in [`12.4.0`](https://docs.cypress.io/guides/references/changelog/12.4.0) where erroneous `<br>` tags were displaying in error messages in the Command Log making them less readable. Fixes [#28452](https://github.com/cypress-io/cypress/issues/28452).

## 13.6.1
Expand Down
16 changes: 13 additions & 3 deletions packages/proxy/lib/http/util/prerequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class PreRequests {

addPending (browserPreRequest: BrowserPreRequest) {
metrics.browserPreRequestsReceived++
const key = `${browserPreRequest.method}-${decodeURI(browserPreRequest.url)}`
const key = `${browserPreRequest.method}-${this.tryDecodeURI(browserPreRequest.url)}`
const pendingRequest = this.pendingRequests.shift(key)

if (pendingRequest) {
Expand Down Expand Up @@ -193,7 +193,7 @@ export class PreRequests {
}

addPendingUrlWithoutPreRequest (url: string) {
const key = `GET-${decodeURI(url)}`
const key = `GET-${this.tryDecodeURI(url)}`
const pendingRequest = this.pendingRequests.shift(key)

if (pendingRequest) {
Expand Down Expand Up @@ -236,7 +236,7 @@ export class PreRequests {
const proxyRequestReceivedTimestamp = performance.now() + performance.timeOrigin

metrics.proxyRequestsReceived++
const key = `${req.method}-${decodeURI(req.proxiedUrl)}`
const key = `${req.method}-${this.tryDecodeURI(req.proxiedUrl)}`
const pendingPreRequest = this.pendingPreRequests.shift(key)

if (pendingPreRequest) {
Expand Down Expand Up @@ -320,4 +320,14 @@ export class PreRequests {
this.pendingRequests = new QueueMap<PendingRequest>()
this.pendingUrlsWithoutPreRequests = new QueueMap<PendingUrlWithoutPreRequest>()
}

private tryDecodeURI (url: string) {
// decodeURI can throw if the url is malformed
// in this case, we just return the original url
try {
return decodeURI(url)
} catch (e) {
return url
}
}
}
29 changes: 29 additions & 0 deletions packages/server/test/integration/http_requests_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,35 @@ describe('Routes', () => {
expect(res.body).to.include('hello from bar!')
})
})

it('handles malformed URIs', function () {
this.timeout(1500)

nock(this.server.remoteStates.current().origin)
.get('/?foo=%A4')
.reply(200, 'hello from bar!', {
'Content-Type': 'text/html',
})

const requestPromise = this.rp({
url: 'http://www.github.com/?foo=%A4',
headers: {
'Accept-Encoding': 'identity',
},
})

this.networkProxy.addPendingBrowserPreRequest({
requestId: '1',
method: 'GET',
url: 'http://www.github.com/?foo=%A4',
})

return requestPromise.then((res) => {
expect(res.statusCode).to.eq(200)

expect(res.body).to.include('hello from bar!')
})
})
})

context('gzip', () => {
Expand Down
Loading

0 comments on commit 0a62d01

Please sign in to comment.