UNPKG

@secretlint/secretlint-rule-sendgrid

Version:
81 lines (76 loc) 2.41 kB
import { SecretLintRuleContext, SecretLintRuleCreator, SecretLintRuleMessageTranslate, SecretLintSourceCode, } from "@secretlint/types"; import { matchPatterns } from "@textlint/regexp-string-matcher"; export const messages = { SENDGRID_KEY: { en: (props: { KEY: string }) => `found Sendgrid api key: ${props.KEY}`, ja: (props: { KEY: string }) => `Sendgrid APIキーが見つかりました: ${props.KEY}`, }, }; export type Options = { /** * Define allow pattern written by RegReg-like strings * See https://github.com/textlint/regexp-string-matcher#regexp-like-string **/ allows?: string[]; }; function reportIfFoundKey({ source, options, context, t, }: { source: SecretLintSourceCode; options: Required<Options>; context: SecretLintRuleContext; t: SecretLintRuleMessageTranslate<typeof messages>; }) { const SENDGRID_KEY_PATTERN = /(?<![A-Za-z])SG\.\w{1,128}\.\w{1,128}([-_]?)\w{1,128}/g; const results = source.content.matchAll(SENDGRID_KEY_PATTERN); for (const result of results) { const index = result.index || 0; const match = result[0] || ""; if (match.length !== 69) { // send grid api key is 69 characters long continue; } const range = [index, index + match.length] as const; const allowedResults = matchPatterns(match, options.allows); if (allowedResults.length > 0) { continue; } context.report({ message: t("SENDGRID_KEY", { KEY: match, }), range, }); } } export const creator: SecretLintRuleCreator<Options> = { messages, meta: { id: "@secretlint/secretlint-rule-sendgrid", recommended: true, type: "scanner", supportedContentTypes: ["text"], docs: { url: "https://github.com/secretlint/secretlint/blob/master/packages/%40secretlint/secretlint-rule-sendgrid/README.md", }, }, create(context, options) { const t = context.createTranslator(messages); const normalizedOptions = { allows: options.allows || [], }; return { file(source: SecretLintSourceCode) { reportIfFoundKey({ source, options: normalizedOptions, context, t }); }, }; }, };