Skip to content

Commit

Permalink
added check for resources greater than 100mb (#1858)
Browse files Browse the repository at this point in the history
* added check for resources greater than 100mb

* fixed coverage
  • Loading branch information
Shivanshu-07 authored Feb 12, 2025
1 parent a76ee38 commit d416e1f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/core/src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,13 @@ async function saveResponseResource(network, request, session) {
url,
responseStatus: response?.status
};
// Checing for content length more than 100MB, to prevent websocket error which is governed by
// maxPayload option of websocket defaulted to 100MB.
// If content-length is more than our allowed 25MB, no need to process that resouce we can return log.
let contentLength = parseInt(response.headers['Content-Length']);
if (contentLength > MAX_RESOURCE_SIZE) {
return log.debug('- Skipping resource larger than 25MB', meta);
}
let resource = network.intercept.getResource(url);

if (!resource || (!resource.root && !resource.provided && disableCache)) {
Expand All @@ -484,8 +491,6 @@ async function saveResponseResource(network, request, session) {
return log.debug('- Skipping remote resource', meta);
} else if (!body.length) {
return log.debug('- Skipping empty response', meta);
} else if (body.length > MAX_RESOURCE_SIZE) {
return log.debug('- Skipping resource larger than 25MB', meta);
} else if (!ALLOWED_STATUSES.includes(response.status)) {
return log.debug(`- Skipping disallowed status [${response.status}]`, meta);
} else if (!enableJavaScript && !ALLOWED_RESOURCES.includes(request.type)) {
Expand Down
35 changes: 35 additions & 0 deletions packages/core/test/discovery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,41 @@ describe('Discovery', () => {
);
});

it('skips file greater than 100MB', async () => {
server.reply('/large.css', () => [200, 'text/css', 'A'.repeat(100_000_000)]);
percy.loglevel('debug');

await percy.snapshot({
name: 'test snapshot',
url: 'http://localhost:8000',
domSnapshot: testDOM.replace('style.css', 'large.css')
});

await percy.idle();

expect(captured[0]).toEqual([
jasmine.objectContaining({
attributes: jasmine.objectContaining({
'resource-url': jasmine.stringMatching(/^\/percy\.\d+\.log$/)
})
}),
jasmine.objectContaining({
attributes: jasmine.objectContaining({
'resource-url': 'http://localhost:8000/'
})
}),
jasmine.objectContaining({
attributes: jasmine.objectContaining({
'resource-url': 'http://localhost:8000/img.gif'
})
})
]);

expect(logger.stderr).toContain(
'[percy:core:discovery] - Skipping resource larger than 25MB'
);
});

it('does not capture duplicate root resources', async () => {
let reDOM = dedent`
<html><head></head><body>
Expand Down

0 comments on commit d416e1f

Please sign in to comment.