Skip to content

Commit

Permalink
chore: optimize ak check
Browse files Browse the repository at this point in the history
  • Loading branch information
csg01123119 committed Sep 14, 2023
1 parent 5b38931 commit fe8cb63
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

node task/detect-secrets
npm run lint-staged
20 changes: 18 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"publish-to-cdn": "node publish.js",
"snyk-protect": "snyk-protect",
"lint-staged": "lint-staged",
"detect-secrets": "node task/detect-secrets",
"tsc": "npm run tsc:clean && npm run tsc:build",
"tsc:build": "tsc -b tsconfig.json tsconfig-cjs.json",
"tsc:watch": "tsc -b tsconfig.json tsconfig-cjs.json --watch",
Expand Down Expand Up @@ -120,6 +119,7 @@
"prettier": "^3.0.0",
"promise-polyfill": "^6.0.2",
"puppeteer": "19.0.0",
"readline-sync": "^1.4.10",
"semantic-release": "^21.1.1",
"should": "^11.0.0",
"sinon": "^15.2.0",
Expand Down Expand Up @@ -157,9 +157,6 @@
},
"snyk": true,
"lint-staged": {
"**/!(dist)/*": [
"npm run detect-secrets --"
],
"**/*.{js,ts}": [
"eslint --cache --fix --ext .js,.ts",
"prettier --write",
Expand Down
50 changes: 39 additions & 11 deletions task/detect-secrets.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
const process = require('process');
const fs = require('fs');
const files = process.argv.slice(2);
const reg = /['"]LT([A-Za-z0-9+/=]{14}|LT[A-Za-z0-9+/=]{22}|LT[A-Za-z0-9+/=]{28})['"]/;
files.forEach((val, index) => {
try {
const data = fs.readFileSync(val, 'utf8');
if (reg.test(data)) {
console.error("Don't push accessKeyId/accessKeySecret to repo! ------ File: " + val);
process.exit(-1);
const readline = require('readline-sync');

const { execSync } = require('child_process');

const getCommandValue = command => {
return execSync(command).toString('utf8').trim();
};

const whiteFiles = ['package-lock.json'];
// Get the list of file names to be submitted
const filenames = getCommandValue('git diff --cached --name-only')
.split('\n')
.filter(item => !!item && !whiteFiles.some(wh => wh === item));
if (filenames.length === 0) {
console.error('No files to submit');
process.exit(-1);
}

const list = [];
filenames.forEach(file => {
if (fs.existsSync(file)) {
const txt = fs.readFileSync(file).toString('utf-8');
const reg = /([0-9a-z+=]{16}|[0-9a-z+=]{24}|[0-9a-z+=]{30})/gi;
const res = txt.match(reg);
if (res) {
const whiteList = ['peerDependencies', 'hasInstallScript'];
const aks = res.filter(item => !whiteList.some(wh => wh === item));
if (aks.length > 0) {
list.push(file); // Check if the code contains AK
aks.forEach(item => console.log(item));
}
}
} catch (err) {
console.error('file error ----' + val);
process.exit(-1);
}
});
if (list.length > 0) {
console.error('Please check if AK is included in the following files\n');
list.forEach(item => console.error(item + '\n'));

const input = readline.question(`Do you want to continue submitting?(y|n)`);
if (/n/i.test(input)) process.exit(-1);
}

0 comments on commit fe8cb63

Please sign in to comment.