Skip to content

Commit

Permalink
Skip lock file parsing if it's too large
Browse files Browse the repository at this point in the history
  • Loading branch information
codykaup committed Jan 10, 2025
1 parent 661975a commit 211f3b8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
7 changes: 6 additions & 1 deletion node-src/lib/findChangedDependencies.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { statSync as unMockedStatSync } from 'fs';
import { buildDepTreeFromFiles } from 'snyk-nodejs-lockfile-parser';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, Mock, vi } from 'vitest';

import { Context } from '..';
import * as git from '../git/git';
Expand All @@ -9,6 +10,10 @@ import TestLogger from './testLogger';
vi.mock('snyk-nodejs-lockfile-parser');
vi.mock('yarn-or-npm');
vi.mock('../git/git');
vi.mock('fs');

const statSync = unMockedStatSync as Mock;
statSync.mockReturnValue({ size: 1 });

const getRepositoryRoot = vi.mocked(git.getRepositoryRoot);
const checkoutFile = vi.mocked(git.checkoutFile);
Expand Down
22 changes: 20 additions & 2 deletions node-src/lib/getDependencies.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { statSync as unMockedStatSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { describe, expect, it } from 'vitest';
import { describe, expect, it, Mock, vi } from 'vitest';

import packageJson from '../__mocks__/dependencyChanges/plain-package.json';
import { checkoutFile } from '../git/git';
import { getDependencies } from './getDependencies';
import { getDependencies, MAX_LOCK_FILE_SIZE } from './getDependencies';
import TestLogger from './testLogger';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const ctx = { log: new TestLogger() } as any;

vi.mock('fs');

const statSync = unMockedStatSync as Mock;
statSync.mockReturnValue({ size: 1 });

describe('getDependencies', () => {
it('should return a set of dependencies', async () => {
const dependencies = await getDependencies(ctx, {
Expand Down Expand Up @@ -99,4 +105,16 @@ describe('getDependencies', () => {
])
);
});

it('should bail if the lock file is too large to parse', async () => {
statSync.mockReturnValue({ size: MAX_LOCK_FILE_SIZE + 1000 });

await expect(() =>
getDependencies(ctx, {
rootPath: path.join(__dirname, '../__mocks__/dependencyChanges'),
manifestPath: 'plain-package.json',
lockfilePath: 'plain-yarn.lock',
})
).rejects.toThrowError();
});
});
12 changes: 12 additions & 0 deletions node-src/lib/getDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { statSync } from 'fs';
import path from 'path';
import { buildDepTreeFromFiles, PkgTree } from 'snyk-nodejs-lockfile-parser';

import { Context } from '../types';

export const MAX_LOCK_FILE_SIZE = 10_485_760; // 10 MB

export const getDependencies = async (
ctx: Context,
{
Expand All @@ -19,6 +23,14 @@ export const getDependencies = async (
strictOutOfSync?: boolean;
}
) => {
// We can run into OOM errors if the lock file is too large. Therefore, we bail early and skip
// lock file parsing because some TurboSnap is better than no TurboSnap.
const stats = statSync(path.resolve(rootPath, lockfilePath));
if (stats.size > MAX_LOCK_FILE_SIZE) {
ctx.log.warn({ lockfilePath }, 'Lock file too large to parse, skipping');
throw new Error('Lock file too large to parse');
}

try {
const headTree = await buildDepTreeFromFiles(
rootPath,
Expand Down

0 comments on commit 211f3b8

Please sign in to comment.