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

Verify types field existence #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 3 additions & 7 deletions __tests__/e2e/security-e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ describe.only('Types bundler security', () => {
};
(fetch as MockedFetch).setMockedNpmPackages(mockPackages);

const result = await bundle('[email protected]', '/tmp/bundle.d.ts', { wrapWithModuleDeclare: true });

expect(result).toBeUndefined();
await expect(bundle('[email protected]', '/tmp/bundle.d.ts', { wrapWithModuleDeclare: true })).rejects.toMatchObject({ message: 'Unable to process ../../../../../../../../../../etc/passwd as it is outside of the project root - ../../../../../../../etc/passwd' });
});

it('should not import files outside the project root via absolute path', async () => {
Expand All @@ -24,9 +23,7 @@ describe.only('Types bundler security', () => {
};
(fetch as MockedFetch).setMockedNpmPackages(mockPackages);

const result = await bundle('[email protected]', '/tmp/bundle.d.ts', { wrapWithModuleDeclare: true });

expect(result).toBeUndefined();
await expect(bundle('[email protected]', '/tmp/bundle.d.ts', { wrapWithModuleDeclare: true })).rejects.toMatchObject({ message: 'Unable to find package.json while searching from /etc/passwd upwards' });
});

it('should not reference types outside the project root', async () => {
Expand All @@ -38,8 +35,7 @@ describe.only('Types bundler security', () => {
})
});

const result = await bundle('[email protected]', '/tmp/bundle.d.ts', { wrapWithModuleDeclare: true });
await expect(bundle('[email protected]', '/tmp/bundle.d.ts', { wrapWithModuleDeclare: true })).rejects.toMatchObject({ message: 'Unable to process /etc/passwd as it is outside of the project root - ../../../../../../../etc/passwd' });

expect(result).toBeUndefined();
});
});
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export async function bundle(packageIdentifier: string, outputFilePath: string,
const pkgJsonData: any = await saveFileFromPackage(tempDirectory, packageName, packageVersion, 'package.json');
const pkgJson = JSON.parse(pkgJsonData);
const pkgPath = tempDirectory;

if (!pkgJson.types) {
throw new Error(`Currently bundler only supports packages with "types" field.`)
}

const inputOptions = {
input: path.join(pkgPath, pkgJson.types),
Expand All @@ -50,6 +54,6 @@ export async function bundle(packageIdentifier: string, outputFilePath: string,
await cache.clearCache();
return resultCode;
} catch(ex) {
console.error(ex);
throw ex;
}
}
Loading