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

fix: limit request body #27

Merged
merged 2 commits into from Jul 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/comment-templates.ts
@@ -1,6 +1,9 @@
import { Result } from 'eslint-remote-tester/dist/exports-for-compare-action';
import { requirePeerDependency } from './peer-dependencies';

// Github API limits body length to 65536. Let's leave some paddings just to be sure.
const RESULTS_MAX_LENGH = 62000;

const filterUniqueTruthy = <T>(item: T, index: number, array: T[]) =>
item != null && array.indexOf(item) === index;

Expand All @@ -11,7 +14,7 @@ const formatRule = (rule: string | null) => '\n- `' + rule + '`';
* Template for building github issue comment when action run into error
*/
export const ERROR_TEMPLATE = (error: Error): string =>
`Something went wrong.
`Something went wrong. This is likely an internal error of \`eslint-remote-tester-run-action\`.

<details>
<summary>Click to expand</summary>
Expand Down Expand Up @@ -50,12 +53,12 @@ ${limitReached ?
Reached maximum result count ${maxResultCount}.
Showing ${limitedResults.length}/${results.length}
` : ''}
Rules:${rules.map(formatRule).join('')}
Rules:${rules.filter(Boolean).map(formatRule).join('')}

<details>
<summary>Click to expand</summary>

${limitedResults.map(template).join('\n')}
${limitedResults.map(template).join('\n').slice(0, RESULTS_MAX_LENGH)}
</details>
`;
};
15 changes: 15 additions & 0 deletions test/__mocks__/GithubAPI.mock.ts
Expand Up @@ -8,6 +8,13 @@ export const onComment = jest.fn();
export const onIssueCreated = jest.fn();
export const expectedIssueNumber = 999;

const ERROR_BODY_MAX_LENGTH = {
resource: 'Issue',
code: 'custom',
field: 'body',
message: 'body is too long (maximum is 65536 characters)',
} as const;

export default setupServer(
rest.get(`${API_URL}/search/issues`, (req, res, ctx) => {
const q = req.url.searchParams.get('q');
Expand Down Expand Up @@ -52,6 +59,10 @@ export default setupServer(
const { owner, repo, issueNumber } = req.params;
const body = req.body;

if (body?.length > 65536) {
return res(ctx.json(ERROR_BODY_MAX_LENGTH), ctx.status(400));
}

// Void endpoint tested via additional assertion
onComment({ owner, repo, issueNumber, body });

Expand All @@ -62,6 +73,10 @@ export default setupServer(
const { owner, repo, title } = req.params;
const body = req.body;

if (body?.length > 65536) {
return res(ctx.json(ERROR_BODY_MAX_LENGTH), ctx.status(400));
}

// Void endpoint tested via additional assertion
onIssueCreated({ owner, repo, title, body });

Expand Down
17 changes: 16 additions & 1 deletion test/comment-templates.test.ts
Expand Up @@ -178,14 +178,29 @@ describe('COMMENT_TEMPLATE', () => {
"
`);
});

test('length of results are limited below 65K characters', () => {
const results = [
generateResult(1),
generateResult(2),
generateResult(3),
].map(result => ({
...result,
error: 'Extremely long stacktrace'.repeat(10000),
}));

const comment = COMMENT_TEMPLATE(results, 3, 50);

expect(comment.length).toBeLessThan(65000);
});
});

describe('ERROR_TEMPLATE', () => {
test('error stack is shown', () => {
const comment = ERROR_TEMPLATE(mockError);

expect(comment).toMatchInlineSnapshot(`
"Something went wrong.
"Something went wrong. This is likely an internal error of \`eslint-remote-tester-run-action\`.

<details>
<summary>Click to expand</summary>
Expand Down