Skip to content

Commit

Permalink
💥[RUMF-1152] sanitize resource method names (#2288)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan authored Jun 7, 2023
1 parent c928b60 commit fad84dd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/browser/fetchObservable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ describe('fetch proxy', () => {
fetchStub(new Request(FAKE_URL, { method: 'PUT' }), { method: 'POST' }).resolveWith({ status: 500 })
fetchStub(new Request(FAKE_URL), { method: 'POST' }).resolveWith({ status: 500 })
fetchStub(FAKE_URL, { method: 'POST' }).resolveWith({ status: 500 })
fetchStub(FAKE_URL, { method: 'post' }).resolveWith({ status: 500 })
fetchStub(null as any).resolveWith({ status: 500 })
fetchStub({ method: 'POST' } as any).resolveWith({ status: 500 })

Expand All @@ -123,8 +124,9 @@ describe('fetch proxy', () => {
expect(requests[3].method).toEqual('POST')
expect(requests[4].method).toEqual('POST')
expect(requests[5].method).toEqual('POST')
expect(requests[6].method).toEqual('GET')
expect(requests[6].method).toEqual('POST')
expect(requests[7].method).toEqual('GET')
expect(requests[8].method).toEqual('GET')
done()
})
})
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/browser/fetchObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ function createFetchObservable() {
}

function beforeSend(observable: Observable<FetchContext>, input: unknown, init?: RequestInit) {
const method = (init && init.method) || (input instanceof Request && input.method) || 'GET'
const methodFromParams = (init && init.method) || (input instanceof Request && input.method)
const method = methodFromParams ? methodFromParams.toUpperCase() : 'GET'
const url = input instanceof Request ? input.url : normalizeUrl(String(input))
const startClocks = clocksNow()

Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/browser/xhrObservable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ describe('xhr observable', () => {
})
})

it('should sanitize request method', (done) => {
withXhr({
setup(xhr) {
xhr.open('get', '/ok')
xhr.send()
xhr.complete(200, 'ok')
},
onComplete() {
const request = requests[0]
expect(request.method).toBe('GET')
done()
},
})
})

it('should track client error', (done) => {
withXhr({
setup(xhr) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/browser/xhrObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function createXhrObservable() {
function openXhr(this: XMLHttpRequest, method: string, url: string | URL | undefined | null) {
xhrContexts.set(this, {
state: 'open',
method,
method: method.toUpperCase(),
url: normalizeUrl(String(url)),
})
}
Expand Down

0 comments on commit fad84dd

Please sign in to comment.