@secretlint/secretlint-rule-privatekey
Version:
A secretlint rule for PrivateKey.
51 lines • 1.8 kB
JavaScript
import { matchPatterns } from "@textlint/regexp-string-matcher";
export const messages = {
PrivateKey: {
en: (props) => `found private key: ${props.KEY}`,
ja: (props) => `秘密鍵: ${props.KEY} がみつかりました`,
},
};
function reportIfFoundRawPrivateKey({ source, options, context, t, }) {
// Based on https://docs.cribl.io/docs/regexesyml
const PRIVATE_KEY_PATTERN = /-----BEGIN\s?((?:DSA|RSA|EC|PGP|OPENSSH|[A-Z]{2,16})?\s?PRIVATE KEY(\sBLOCK)?)-----[\s\S]{1,10000}?-----END\s?\1-----/gm;
const results = source.content.matchAll(PRIVATE_KEY_PATTERN);
for (const result of results) {
const index = result.index || 0;
const match = result[0] || "";
const range = [index, index + match.length];
const allowedResults = matchPatterns(match, options.allows);
if (allowedResults.length > 0) {
continue;
}
context.report({
message: t("PrivateKey", {
KEY: match,
}),
range,
});
}
}
export const creator = {
messages,
meta: {
id: "@secretlint/secretlint-rule-privatekey",
recommended: true,
type: "scanner",
supportedContentTypes: ["text"],
docs: {
url: "https://github.com/secretlint/secretlint/blob/master/packages/%40secretlint/secretlint-rule-privatekey/README.md",
},
},
create(context, options) {
const t = context.createTranslator(messages);
const normalizedOptions = {
allows: options.allows || [],
};
return {
file(source) {
reportIfFoundRawPrivateKey({ source, options: normalizedOptions, context, t });
},
};
},
};
//# sourceMappingURL=index.js.map