Skip to content

Copy object to prevent exception in react native #960

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

Merged
merged 5 commits into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 10 additions & 2 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -1385,16 +1385,17 @@ Raven.prototype = {

for (var i = 0; i < breadcrumbs.values.length; ++i) {
crumb = breadcrumbs.values[i];
if (!crumb.hasOwnProperty('data') || !isObject(crumb.data))
if (!crumb.hasOwnProperty('data') || !isObject(crumb.data) || objectFrozen(crumb.data))
continue;

data = crumb.data;
data = objectMerge({}, crumb.data);
for (var j = 0; j < urlProps.length; ++j) {
urlProp = urlProps[j];
if (data.hasOwnProperty(urlProp)) {
data[urlProp] = truncate(data[urlProp], this._globalOptions.maxUrlLength);
}
}
breadcrumbs.values[i].data = data;
}
},

Expand Down Expand Up @@ -1779,6 +1780,13 @@ function objectMerge(obj1, obj2) {
return obj1;
}

function objectFrozen(obj) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@HazAT – would you mind adding a comment about why we do this for react native, and why it's okay if Object.isFrozen isn't called in environments where it isn't supported (maybe link to this PR?). Last thing and then I'll merge.

Copy link
Member Author

Choose a reason for hiding this comment

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

Of course makes sense 👍

if (!Object.isFrozen) {
return false;
}
return Object.isFrozen(obj);
}

function truncate(str, max) {
return !max || str.length <= max ? str : str.substr(0, max) + '\u2026';
}
Expand Down
86 changes: 85 additions & 1 deletion test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,90 @@ describe('globals', function() {
assert.equal(Raven._backoffStart, null); // clock is at 100ms
assert.equal(Raven._backoffDuration, 0);
});
it('should truncate url in breadcrumb', function() {
this.sinon.stub(Raven, 'isSetup').returns(true);
this.sinon.stub(Raven, '_makeRequest');
this.sinon.stub(Raven, '_getHttpData').returns({
url: 'http://localhost/?a=b',
headers: {'User-Agent': 'lolbrowser'}
});

Raven._globalProject = '2';
Raven._globalOptions = {
logger: 'javascript',
maxMessageLength: 100
};
Raven._globalOptions.maxUrlLength = 30;

var longUrl = new Array(50).join('a');
var obj = {method: 'POST', url: 'http://example.org/api/0/auth/' + longUrl};
Raven._breadcrumbs = [{type: 'request', timestamp: 0.1, data: obj}];

Raven._send({message: 'bar'});
assert.deepEqual(Raven._makeRequest.lastCall.args[0].data, {
project: '2',
logger: 'javascript',
platform: 'javascript',
request: {
url: 'http://localhost/?a=b',
headers: {
'User-Agent': 'lolbrowser'
}
},
event_id: 'abc123',
message: 'bar',
extra: {'session:duration': 100},
breadcrumbs: {
values: [
{ type: 'request', timestamp: 0.1, data: { method: 'POST', url: 'http://example.org/api/0/auth/…' }}
]
}
});
});

it('should skip truncating url in breadcrumb if object is frozen', function() {
this.sinon.stub(Raven, 'isSetup').returns(true);
this.sinon.stub(Raven, '_makeRequest');
this.sinon.stub(Raven, '_getHttpData').returns({
url: 'http://localhost/?a=b',
headers: {'User-Agent': 'lolbrowser'}
});

Raven._globalProject = '2';
Raven._globalOptions = {
logger: 'javascript',
maxMessageLength: 100
};
Raven._globalOptions.maxUrlLength = 35;

var longUrl = new Array(50).join('a');
var obj = {method: 'POST', url: 'http://example.org/api/0/auth/' + longUrl};
Object.freeze(obj);

Raven._breadcrumbs = [{type: 'request', timestamp: 0.1, data: obj}];

Raven._send({message: 'bar'});
assert.deepEqual(Raven._makeRequest.lastCall.args[0].data, {
project: '2',
logger: 'javascript',
platform: 'javascript',
request: {
url: 'http://localhost/?a=b',
headers: {
'User-Agent': 'lolbrowser'
}
},
event_id: 'abc123',
message: 'bar',
extra: {'session:duration': 100},
breadcrumbs: {
values: [
{ type: 'request', timestamp: 0.1, data: { method: 'POST', url: 'http://example.org/api/0/auth/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' }}
]
}
});
});

});

describe('makeRequest', function() {
Expand Down Expand Up @@ -2770,7 +2854,7 @@ describe('Raven (private methods)', function () {
this.clock.tick(0); // Raven initialized at time "0"
Raven = new _Raven();
});

afterEach(function () {
this.clock.restore();
});
Expand Down