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

fixed content length NaN #1866

Merged
merged 8 commits into from
Feb 24, 2025
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
6 changes: 5 additions & 1 deletion packages/core/src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,8 @@ async function saveResponseResource(network, request, session) {
// 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']);
let contentLength = response.headers?.[Object.keys(response.headers).find(key => key.toLowerCase() === 'content-length')];
contentLength = parseInt(contentLength);
if (contentLength > MAX_RESOURCE_SIZE) {
return log.debug('- Skipping resource larger than 25MB', meta);
}
Expand All @@ -491,6 +492,9 @@ 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) {
log.debug('- Missing headers for the requested resource.', meta);
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
111 changes: 111 additions & 0 deletions packages/core/test/discovery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,117 @@ describe('Discovery', () => {
);
});

it('checks if no header is send from server', async () => {
server.reply('/large.css', () => [200, 'text/css', 'A'.repeat(30_000_000)], { noHeaders: true });
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] - Missing headers for the requested resource.'
);
});

it('does not capture remote files with content-length NAN greater than 25MB', async () => {
server.reply('/large.css', () => [200, 'text/css', 'A'.repeat(30_000_000)], { headersOverride: { 'content-length': NaN } });
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'
);
});

function testContentLengthCasing(description, contentLengthHeader) {
it(description, async () => {
server.reply('/large.css', () => [200, 'text/css', 'A'.repeat(30_000_000)], { headersOverride: contentLengthHeader });
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'
);
});
}

describe('Content-Length casing tests', () => {
testContentLengthCasing('does not capture remote files with content-length casing', { 'content-length': '16515075' });
testContentLengthCasing('does not capture remote files with Content-Length casing', { 'Content-Length': '16515075' });
testContentLengthCasing('does not capture remote files with CONTENT-LENGTH casing', { 'CONTENT-LENGTH': '16515075' });
});

it('skips file greater than 100MB', async () => {
server.reply('/large.css', () => [200, 'text/css', 'A'.repeat(100_000_000)]);
percy.loglevel('debug');
Expand Down
13 changes: 10 additions & 3 deletions packages/core/test/helpers/server.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
// aliased to src during tests
import Server from '../../dist/server.js';

export function createTestServer({ default: defaultReply, ...replies }, port = 8000) {
export function createTestServer({ default: defaultReply, ...replies }, port = 8000, options = {}) {
let server = new Server();

// alternate route handling
let handleReply = reply => async (req, res) => {
let handleReply = (reply, options = {}) => async (req, res) => {
let [status, headers, body] = typeof reply === 'function' ? await reply(req) : reply;
if (!Buffer.isBuffer(body) && typeof body !== 'string') body = JSON.stringify(body);

if (options.noHeaders) {
return res.writeHead(status).end(body);
}
if (options.headersOverride) {
headers = { ...headers, ...options.headersOverride };
}
return res.send(status, headers, body);
};

// map replies to alternate route handlers
server.reply = (p, reply) => (replies[p] = handleReply(reply), null);
server.reply = (p, reply, options = {}) => (replies[p] = handleReply(reply, options), null);
for (let [p, reply] of Object.entries(replies)) server.reply(p, reply);
if (defaultReply) defaultReply = handleReply(defaultReply);

Expand Down