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

try test setup #5

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions dummy-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
df57089febbacf7ba0bc227dafbffa9fc08a93fdc68e1e42411a14efcf23656e
36 changes: 1 addition & 35 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,4 @@
#!/usr/bin/env node

import { checkIfPrivKey } from './utils/crypto';
import {
getAllFiles,
getIgnoreListFromGitignore,
getTokensFromFile,
Token,
} from './utils/files';
import chalk from 'chalk';

const run = () => {
const ignoreList = getIgnoreListFromGitignore();
const files = getAllFiles('./', [...ignoreList]);
const tokens: Token[] = [];
files.forEach((file) => {
tokens.push(...getTokensFromFile(file));
});
const vulnFiles: string[] = [];
tokens.forEach((token) => {
if (checkIfPrivKey(token.content)) {
vulnFiles.push(token.fileName);
}
});
if (vulnFiles.length > 0) {
console.log(
chalk.red(
`🚨 Found ${vulnFiles.length} instance(s) of private keys. Aborting commit.`
)
);
vulnFiles.forEach((file) => console.log(chalk(`=> ${file}`)));
process.exit(1);
} else {
console.log(chalk.green('✅ No private keys found'));
}
};
import { run } from './utils';

run();
15 changes: 15 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
testTimeout: 30000,
testEnvironment: 'jest-environment-node',
preset: 'ts-jest/presets/default-esm',
transform: {},
globals: {
'ts-jest': {
useESM: true,
},
},
moduleNameMapper: {
'^(.*)\\.js$': '$1',
},
};
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "ncc build ./index.ts -w -o dist/",
"prerelease": "rimraf ./dist/",
"release": "ncc build ./index.ts -o ./dist/ --minify --no-cache --no-source-map-register",
"prepublish": "yarn release"
"prepublish": "yarn release",
"test": "node --experimental-vm-modules node_modules/.bin/jest"
},
"files": [
"dist"
Expand All @@ -24,9 +25,13 @@
},
"homepage": "https://github.com/Dhaiwat10/priv-key-precommit#readme",
"devDependencies": {
"@types/jest": "^27.5.0",
"@types/node": "^17.0.31",
"@vercel/ncc": "^0.33.4",
"jest": "^28.1.0",
"jest-environment-node": "^28.1.0",
"rimraf": "^3.0.2",
"ts-jest": "^28.0.2",
"typescript": "^4.6.4"
},
"dependencies": {
Expand Down
9 changes: 1 addition & 8 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
- [x] get a list of all the files in the current directory
- [x] skip files inside of .gitignore and other known stuff like node_modules
- [x] loop through all the files
- [ ] read the file and tokenize the contents
- [ ] check for a private key
- [ ] keep a running count of the number of private keys found and where they are
- [ ] print out the whereabouts of all the private keys found (DONT print the keys themselves), plus throw an error if there are any to make sure husky fails
# privy-key-precommit

- [ ] write test cases
7 changes: 7 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
jest.useFakeTimers();

import { run } from '../utils';

test('test', () => {
expect(run());
});
34 changes: 34 additions & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import chalk from 'chalk';
import { checkIfPrivKey } from './crypto';
import {
getAllFiles,
getIgnoreListFromGitignore,
getTokensFromFile,
Token,
} from './files';

export const run = () => {
const ignoreList = getIgnoreListFromGitignore();
const files = getAllFiles('./', [...ignoreList]);
const tokens: Token[] = [];
files.forEach((file) => {
tokens.push(...getTokensFromFile(file));
});
const vulnFiles: string[] = [];
tokens.forEach((token) => {
if (checkIfPrivKey(token.content)) {
vulnFiles.push(token.fileName);
}
});
if (vulnFiles.length > 0) {
console.log(
chalk.red(
`🚨 Found ${vulnFiles.length} instance(s) of private keys. Aborting commit.`
)
);
vulnFiles.forEach((file) => console.log(chalk(`=> ${file}`)));
process.exit(1);
} else {
console.log(chalk.green('✅ No private keys found'));
}
};
Loading