diff --git a/packages/core/src/chat.ts b/packages/core/src/chat.ts index b0877fb6c..39c6cee5a 100644 --- a/packages/core/src/chat.ts +++ b/packages/core/src/chat.ts @@ -73,6 +73,7 @@ import { serializeLogProb, topLogprobsToMarkdown, } from "./logprob" +import { hideSecrets } from "./secrets" export function toChatCompletionUserMessage( expanded: string, @@ -967,11 +968,11 @@ export function appendUserMessage( ) { if (!content) return const last = messages.at(-1) as ChatCompletionUserMessageParam - if (last?.role === "user") last.content += "\n" + content + if (last?.role === "user") last.content += "\n" + hideSecrets(content) else messages.push({ role: "user", - content, + content: hideSecrets(content), } as ChatCompletionUserMessageParam) } @@ -981,11 +982,11 @@ export function appendAssistantMessage( ) { if (!content) return const last = messages.at(-1) as ChatCompletionAssistantMessageParam - if (last?.role === "assistant") last.content += "\n" + content + if (last?.role === "assistant") last.content += "\n" + hideSecrets(content) else messages.push({ role: "assistant", - content, + content: hideSecrets(content), } satisfies ChatCompletionAssistantMessageParam) } @@ -1003,7 +1004,7 @@ export function appendSystemMessage( messages.unshift(last) } if (last.content) last.content += SYSTEM_FENCE - last.content += content + last.content += hideSecrets(content) } export function addToolDefinitionsMessage( diff --git a/packages/core/src/secrets.ts b/packages/core/src/secrets.ts new file mode 100644 index 000000000..14230d998 --- /dev/null +++ b/packages/core/src/secrets.ts @@ -0,0 +1,58 @@ +import { TraceOptions } from "./trace" +import { logVerbose } from "./util" + +const secretPatterns: Record = { + "AWS Access Key": /AKIA[0-9A-Z]{16}/g, + "GitHub Token": /ghp_[0-9a-zA-Z]{36}/g, + "Slack Token": /xox[baprs]-[0-9a-zA-Z]{10,48}/g, + "Google API Key": /AIza[0-9A-Za-z-_]{35}/g, + "Azure Key": /[0-9a-zA-Z/+]{88}/g, + "Stripe API Key": /sk_live_[0-9a-zA-Z]{24}/g, + "Google AI Key": /AIza[0-9A-Za-z-_]{35}/g, + "OpenAI Key": /sk-[0-9a-zA-Z]{32}/g, + "Twilio API Key": /SK[0-9a-fA-F]{32}/g, + "SendGrid API Key": /SG\.[0-9A-Za-z\-_]{22}\.[0-9A-Za-z\-_]{43}/g, + "Facebook Access Token": /EAACEdEose0cBA[0-9A-Za-z]+/g, + "Twitter Access Token": /[1-9][0-9]+-[0-9a-zA-Z]{40}/g, + "Twitter Secret Key": /[0-9a-zA-Z]{40}/g, + "GitLab Personal Access Token": /glpat-[0-9a-zA-Z\-_]{20}/g, + "DigitalOcean Token": /[0-9a-fA-F]{64}/g, + "Mailgun API Key": /key-[0-9a-zA-Z]{32}/g, + "Dropbox Access Token": /sl.[0-9a-zA-Z\-_]{43}/g, + "Shopify Access Token": /shpat_[0-9a-fA-F]{32}/g, + "GitHub Personal Access Token": /ghp_[0-9a-zA-Z]{36}/g, + "Generic API Key": /(?