Skip to content

Commit

Permalink
fix: handle malformed URIs in prerequests (#28522)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschile authored Dec 14, 2023
1 parent 7bcb074 commit c9062f1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 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 @@ -94,6 +94,16 @@ class QueueMap<T> {
}
}

const 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
}
}

// This class' purpose is to match up incoming "requests" (requests from the browser received by the http proxy)
// with "pre-requests" (events received by our browser extension indicating that the browser is about to make a request).
// Because these come from different sources, they can be out of sync, arriving in either order.
Expand Down Expand Up @@ -148,7 +158,7 @@ export class PreRequests {

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

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

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

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

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

if (pendingPreRequest) {
Expand Down
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

4 comments on commit c9062f1

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on c9062f1 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.6.2/linux-arm64/develop-c9062f145ee00e7c6ee98c8c8949849dbf525e6d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on c9062f1 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.6.2/linux-x64/develop-c9062f145ee00e7c6ee98c8c8949849dbf525e6d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on c9062f1 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.6.2/darwin-x64/develop-c9062f145ee00e7c6ee98c8c8949849dbf525e6d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on c9062f1 Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.6.2/win32-x64/develop-c9062f145ee00e7c6ee98c8c8949849dbf525e6d/cypress.tgz

Please sign in to comment.