Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone the fetch response before reading #868

Merged
merged 1 commit into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/browser/telemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,9 @@ Instrumenter.prototype.instrumentNetwork = function() {
}
var body = null;
if (self.autoInstrument.networkResponseBody) {
if (typeof resp.text === 'function') { // Response.text() is not implemented on multiple platforms
body = resp.text(); //returns a Promise
if (typeof resp.text === 'function') { // Response.text() is not implemented on some platforms
// The response must be cloned to prevent reading (and locking) the original stream.
body = resp.clone().text(); //returns a Promise
}
}
if (headers || body) {
Expand Down
17 changes: 14 additions & 3 deletions test/browser.rollbar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1145,8 +1145,16 @@ describe('options.autoInstrument', function() {
server.requests.length = 0;

window.fetchStub = sinon.stub(window, 'fetch');

var readableStream = new ReadableStream({
start(controller) {
controller.enqueue(JSON.stringify({name: 'foo', password: '123456'}));
controller.close();
}
});

window.fetch.returns(Promise.resolve(new Response(
JSON.stringify({name: 'foo', password: '123456'}),
readableStream,
{ status: 200, statusText: 'OK', headers: { 'content-type': 'application/json', 'password': '123456' }}
)));

Expand All @@ -1173,8 +1181,8 @@ describe('options.autoInstrument', function() {
body: JSON.stringify({name: 'bar', secret: 'xhr post'})
};
var fetchRequest = new Request('https://example.com/xhr-test');
window,fetch(fetchRequest, fetchInit)
.then(function(_response) {
window.fetch(fetchRequest, fetchInit)
.then(function(response) {
try {
rollbar.log('test'); // generate a payload to inspect
server.respond();
Expand All @@ -1198,6 +1206,9 @@ describe('options.autoInstrument', function() {
// Verify response headers capture and case-insensitive scrubbing
expect(body.data.body.telemetry[0].body.response.headers).to.eql({'content-type': 'application/json', password: '********'});

// Assert that the original stream reader hasn't been read.
expect(response.bodyUsed).to.eql(false);

rollbar.configure({ autoInstrument: false });
window.fetch.restore();
done();
Expand Down