Skip to content

Commit

Permalink
Fix crash when request URL is not a string
Browse files Browse the repository at this point in the history
e.g. a HTMLAnchorElement returns its 'href' from 'toString', which
allows it to be used in place of a URL string
  • Loading branch information
imjoehaines committed Nov 25, 2021
1 parent 45fbe5b commit 1e8b8e0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
14 changes: 10 additions & 4 deletions packages/plugin-network-breadcrumbs/network-breadcrumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ module.exports = (_ignoredUrls = [], win = window) => {
}

function handleXHRLoad () {
if (this[REQUEST_URL_KEY] === undefined) {
const url = this[REQUEST_URL_KEY]

if (url === undefined) {
client._logger.warn('The request URL is no longer present on this XMLHttpRequest. A breadcrumb cannot be left for this request.')
return
}

if (includes(ignoredUrls, this[REQUEST_URL_KEY].replace(/\?.*$/, ''))) {
// an XMLHttpRequest's URL can be an object as long as its 'toString'
// returns a URL, e.g. a HTMLAnchorElement
if (typeof url === 'string' && includes(ignoredUrls, url.replace(/\?.*$/, ''))) {
// don't leave a network breadcrumb from bugsnag notify calls
return
}
Expand All @@ -82,12 +86,14 @@ module.exports = (_ignoredUrls = [], win = window) => {
}

function handleXHRError () {
if (this[REQUEST_URL_KEY] === undefined) {
const url = this[REQUEST_URL_KEY]

if (url === undefined) {
client._logger.warn('The request URL is no longer present on this XMLHttpRequest. A breadcrumb cannot be left for this request.')
return
}

if (includes(ignoredUrls, this[REQUEST_URL_KEY].replace(/\?.*$/, ''))) {
if (typeof url === 'string' && includes(ignoredUrls, url.replace(/\?.*$/, ''))) {
// don't leave a network breadcrumb from bugsnag notify calls
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class XMLHttpRequest {
this.status = null
}

open (method: string, url: string) {
open (method: string, url: string | { toString: () => any }) {
}

send (fail: boolean, status: number | null = null) {
Expand Down Expand Up @@ -225,6 +225,61 @@ describe('plugin: network breadcrumbs', () => {
)
})

it('should leave a breadcrumb when the request URL is not a string', () => {
const window = { XMLHttpRequest } as unknown as Window & typeof globalThis

const logger = {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn()
}

p = plugin([], window)
const client = new Client({ apiKey: 'abcabcabcabcabcabcabc1234567890f', logger, plugins: [p] })

const request = new window.XMLHttpRequest() as unknown as XMLHttpRequest
request.open('GET', { toString: () => 'https://example.com' })
request.send(false, 200)

expect(client._breadcrumbs.length).toBe(1)
expect(client._breadcrumbs[0]).toEqual(expect.objectContaining({
type: 'request',
message: 'XMLHttpRequest succeeded',
metadata: {
status: 200,
request: 'GET https://example.com'
}
}))
})

it('should leave a breadcrumb when the request URL is not a string for a request that errors', () => {
const window = { XMLHttpRequest } as unknown as Window & typeof globalThis

const logger = {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn()
}

p = plugin([], window)
const client = new Client({ apiKey: 'abcabcabcabcabcabcabc1234567890f', logger, plugins: [p] })

const request = new window.XMLHttpRequest() as unknown as XMLHttpRequest
request.open('GET', { toString: () => 'https://example.com' })
request.send(true)

expect(client._breadcrumbs.length).toBe(1)
expect(client._breadcrumbs[0]).toEqual(expect.objectContaining({
type: 'request',
message: 'XMLHttpRequest error',
metadata: {
request: 'GET https://example.com'
}
}))
})

it('should leave a breadcrumb when a fetch() resolves', (done) => {
const window = { XMLHttpRequest, fetch } as unknown as Window & typeof globalThis

Expand Down

0 comments on commit 1e8b8e0

Please sign in to comment.