Skip to content

Commit 80fdafe

Browse files
committed
Merge remote-tracking branch 'origin/main' into benibenj/handsome-stoat
2 parents 5f6ed82 + 18221d1 commit 80fdafe

File tree

10 files changed

+78
-15
lines changed

10 files changed

+78
-15
lines changed

src/package.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2130,9 +2130,10 @@ export async function scanFilesForSecrets(files: IFile[], fileExclusion: FileExc
21302130
}
21312131

21322132
const onDiskFiles: ILocalFile[] = files.filter(file => !isInMemoryFile(file)) as ILocalFile[];
2133+
const onDiskNoneNodeModulesFiles = onDiskFiles.filter(file => !file.localPath.includes('node_modules'));
21332134
const inMemoryFiles: IInMemoryFile[] = files.filter(file => isInMemoryFile(file)) as IInMemoryFile[];
21342135

2135-
const onDiskResult = await lintFiles(onDiskFiles.map(file => file.localPath), scanForSecrets, scanDotEnv);
2136+
const onDiskResult = await lintFiles(onDiskNoneNodeModulesFiles.map(file => file.localPath), scanForSecrets, scanDotEnv);
21362137
const inMemoryResults = await Promise.all(
21372138
inMemoryFiles.map(file => lintText(typeof file.contents === 'string' ? file.contents : file.contents.toString('utf8'), file.path, scanForSecrets, scanDotEnv))
21382139
);

src/secretLint.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import chalk from "chalk";
22
import { Convert, Location, Region, Result, Level } from "./typings/secret-lint-types";
3+
import { log } from "./util";
34

45
interface SecretLintEngineResult {
56
ok: boolean;
@@ -28,6 +29,15 @@ const secretsScanningRules = [
2829
"/^(?![\\s\\S]*-----BEGIN .*PRIVATE KEY-----[A-Za-z0-9+/=\\r\\n]{50,}-----END .*PRIVATE KEY-----)[\\s\\S]*$/"
2930
]
3031
}
32+
}, {
33+
id: "@secretlint/secretlint-rule-npm",
34+
options: {
35+
allows: [
36+
// An npm token has the prefix npm_ followed by 36 Base62 characters (30 random + 6-character checksum), totaling 40 characters.
37+
// https://github.com/microsoft/vscode-vsce/issues/1153
38+
"/^(?!(?:npm_[0-9A-Za-z]{36})$).+$/"
39+
]
40+
}
3141
}
3242
]
3343
}
@@ -71,9 +81,16 @@ export async function lintFiles(
7181
): Promise<SecretLintResult> {
7282
const engine = await getEngine(scanSecrets, scanDotEnv);
7383

74-
const engineResult = await engine.executeOnFiles({
75-
filePathList: filePaths
76-
});
84+
let engineResult;
85+
try {
86+
engineResult = await engine.executeOnFiles({
87+
filePathList: filePaths
88+
});
89+
} catch (error) {
90+
log.error('Error occurred while scanning secrets (files):', error);
91+
process.exit(1);
92+
}
93+
7794
return parseResult(engineResult);
7895
}
7996

@@ -85,10 +102,16 @@ export async function lintText(
85102
): Promise<SecretLintResult> {
86103
const engine = await getEngine(scanSecrets, scanDotEnv);
87104

88-
const engineResult = await engine.executeOnContent({
89-
content,
90-
filePath: fileName
91-
});
105+
let engineResult;
106+
try {
107+
engineResult = await engine.executeOnContent({
108+
content,
109+
filePath: fileName
110+
});
111+
} catch (error) {
112+
log.error('Error occurred while scanning secrets (content):', error);
113+
process.exit(1);
114+
}
92115
return parseResult(engineResult);
93116
}
94117

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**secret**
2+
**noSecret**
23
!noSecret1.ts
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**secret**
2+
**noSecret**
23
!noSecret2.ts
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// https://github.com/microsoft/vscode-vsce/issues/1153
2+
function npm_i_save_dev_types_Slashjest_or_npm_i_(){}
3+
function npm_i_save_dev_types_Slash_1_if_it_exists(){}
4+
function Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery_and_then_add_jquery_to_the_types_field_in_your_tsconfig(){}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**secret**
2+
**noSecret**
3+
!noSecret3.ts
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**secret**
2+
**noSecret**
23
!secret1.ts

src/test/fixtures/secrets/secret2.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const k = `npm_Ab3kZy0X9QpLmN4tUvW7aBcDeFgHiJkLmNoPqRsTu`
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**secret**
2+
**noSecret**
3+
!secret2.ts

src/test/package.test.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ async function processExitExpected(fn: () => Promise<any>, errorMessage: string)
165165
}
166166
}
167167

168+
async function processExitNotExpected(fn: () => Promise<any>, errorMessage: string): Promise<void> {
169+
const originalExit = process.exit;
170+
try {
171+
process.exit = (() => {
172+
throw new Error(errorMessage);
173+
}) as any;
174+
175+
await fn();
176+
} finally {
177+
process.exit = originalExit;
178+
}
179+
}
180+
168181
describe('collect', function () {
169182
this.timeout(60000);
170183

@@ -386,42 +399,54 @@ describe('collect', function () {
386399

387400
it('should not package .env file', async function () {
388401
const cwd = fixture('env');
389-
await processExitExpected(() => pack({ cwd, packagePath: getVisxOutputPath() }), 'Expected package to throw: .env file should not be packaged');
402+
await processExitExpected(async () => await pack({ cwd, packagePath: getVisxOutputPath() }), 'Expected package to throw: .env file should not be packaged');
390403
});
391404

392405
it('allow packaging .env file with --allow-package-env-file', async function () {
393406
const cwd = fixture('env');
394-
await pack({ cwd, allowPackageEnvFile: true, packagePath: getVisxOutputPath() });
407+
await processExitNotExpected(async () => await pack({ cwd, allowPackageEnvFile: true, packagePath: getVisxOutputPath() }), 'Should not have exited');
395408
});
396409

397410
it('should not package file which has a private key', async function () {
398411
const cwd = fixture('secrets');
399412
const ignoreFile = path.join(cwd, 'secret1Ignore');
400-
await processExitExpected(() => pack({ cwd, packagePath: getVisxOutputPath(), ignoreFile }), 'Expected package to throw: file which has a private key should not be packaged');
413+
await processExitExpected(async () => await pack({ cwd, packagePath: getVisxOutputPath(), ignoreFile }), 'Expected package to throw: file which has a private key should not be packaged');
401414
});
402415

403416
it('allow packaging file which has a private key with --allow-package-secrets', async function () {
404417
const cwd = fixture('secrets');
405418
const ignoreFile = path.join(cwd, 'secret1Ignore');
406-
await pack({ cwd, allowPackageSecrets: ['privatekey'], packagePath: getVisxOutputPath(), ignoreFile });
419+
await processExitNotExpected(async () => await pack({ cwd, allowPackageSecrets: ['privatekey'], packagePath: getVisxOutputPath(), ignoreFile }), 'Should not have exited');
407420
});
408421

409422
it('allow packaging file which has a private key with --allow-package-all-secrets', async function () {
410423
const cwd = fixture('secrets');
411424
const ignoreFile = path.join(cwd, 'secret1Ignore');
412-
await pack({ cwd, allowPackageAllSecrets: true, packagePath: getVisxOutputPath(), ignoreFile });
425+
await processExitNotExpected(async () => await pack({ cwd, allowPackageAllSecrets: true, packagePath: getVisxOutputPath(), ignoreFile }), 'Should not have exited');
413426
});
414427

415428
it('private key false positive 1', async function () {
416429
const cwd = fixture('secrets');
417430
const ignoreFile = path.join(cwd, 'noSecret1Ignore');
418-
await pack({ cwd, allowPackageAllSecrets: true, packagePath: getVisxOutputPath(), ignoreFile });
431+
await processExitNotExpected(async () => await pack({ cwd, packagePath: getVisxOutputPath(), ignoreFile }), 'Should not have exited');
419432
});
420433

421434
it('private key false positive 2', async function () {
422435
const cwd = fixture('secrets');
423436
const ignoreFile = path.join(cwd, 'noSecret2Ignore');
424-
await pack({ cwd, allowPackageAllSecrets: true, packagePath: getVisxOutputPath(), ignoreFile });
437+
await processExitNotExpected(async () => await pack({ cwd, packagePath: getVisxOutputPath(), ignoreFile }), 'Should not have exited');
438+
});
439+
440+
it('should not package npm token', async function () {
441+
const cwd = fixture('secrets');
442+
const ignoreFile = path.join(cwd, 'secret2Ignore');
443+
await processExitExpected(async () => await pack({ cwd, packagePath: getVisxOutputPath(), ignoreFile }), 'Expected package to throw: should not package npm token');
444+
});
445+
446+
it('npm token false positive 1', async function () {
447+
const cwd = fixture('secrets');
448+
const ignoreFile = path.join(cwd, 'noSecret3Ignore');
449+
await processExitNotExpected(async () => await pack({ cwd, packagePath: getVisxOutputPath(), ignoreFile }), 'Should not have exited');
425450
});
426451
});
427452

0 commit comments

Comments
 (0)