Skip to content

Commit c9062f1

Browse files
authored
fix: handle malformed URIs in prerequests (#28522)
1 parent 7bcb074 commit c9062f1

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"json"
2020
],
2121
"editor.codeActionsOnSave": {
22-
"source.fixAll.eslint": true
22+
"source.fixAll.eslint": "explicit"
2323
},
2424
"typescript.tsdk": "node_modules/typescript/lib",
2525

cli/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ _Released 12/19/2023 (PENDING)_
55

66
**Bugfixes:**
77

8+
- 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).
89
- 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).
910

1011
## 13.6.1

packages/proxy/lib/http/util/prerequests.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ class QueueMap<T> {
9494
}
9595
}
9696

97+
const tryDecodeURI = (url: string) => {
98+
// decodeURI can throw if the url is malformed
99+
// in this case, we just return the original url
100+
try {
101+
return decodeURI(url)
102+
} catch (e) {
103+
return url
104+
}
105+
}
106+
97107
// This class' purpose is to match up incoming "requests" (requests from the browser received by the http proxy)
98108
// with "pre-requests" (events received by our browser extension indicating that the browser is about to make a request).
99109
// Because these come from different sources, they can be out of sync, arriving in either order.
@@ -148,7 +158,7 @@ export class PreRequests {
148158

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

154164
if (pendingRequest) {
@@ -193,7 +203,7 @@ export class PreRequests {
193203
}
194204

195205
addPendingUrlWithoutPreRequest (url: string) {
196-
const key = `GET-${decodeURI(url)}`
206+
const key = `GET-${tryDecodeURI(url)}`
197207
const pendingRequest = this.pendingRequests.shift(key)
198208

199209
if (pendingRequest) {
@@ -236,7 +246,7 @@ export class PreRequests {
236246
const proxyRequestReceivedTimestamp = performance.now() + performance.timeOrigin
237247

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

242252
if (pendingPreRequest) {

packages/server/test/integration/http_requests_spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,35 @@ describe('Routes', () => {
12301230
expect(res.body).to.include('hello from bar!')
12311231
})
12321232
})
1233+
1234+
it('handles malformed URIs', function () {
1235+
this.timeout(1500)
1236+
1237+
nock(this.server.remoteStates.current().origin)
1238+
.get('/?foo=%A4')
1239+
.reply(200, 'hello from bar!', {
1240+
'Content-Type': 'text/html',
1241+
})
1242+
1243+
const requestPromise = this.rp({
1244+
url: 'http://www.github.com/?foo=%A4',
1245+
headers: {
1246+
'Accept-Encoding': 'identity',
1247+
},
1248+
})
1249+
1250+
this.networkProxy.addPendingBrowserPreRequest({
1251+
requestId: '1',
1252+
method: 'GET',
1253+
url: 'http://www.github.com/?foo=%A4',
1254+
})
1255+
1256+
return requestPromise.then((res) => {
1257+
expect(res.statusCode).to.eq(200)
1258+
1259+
expect(res.body).to.include('hello from bar!')
1260+
})
1261+
})
12331262
})
12341263

12351264
context('gzip', () => {

0 commit comments

Comments
 (0)