Skip to content

Commit efb42ba

Browse files
committed
fix(security): improve API key detection patterns to prevent false positives
- Change from substring to pattern-based matching for sensitive keys - Prevents ANTHROPIC_MAX_TOKENS from being incorrectly censored - Synchronize backend and UI detection logic for consistency
1 parent 639eec7 commit efb42ba

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/web-server/routes.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,17 @@ function maskApiKeys(settings: Settings): Settings {
478478
if (!settings.env) return settings;
479479

480480
const masked = { ...settings, env: { ...settings.env } };
481-
const sensitiveKeys = ['ANTHROPIC_AUTH_TOKEN', 'API_KEY', 'AUTH_TOKEN'];
481+
// Pattern-based matching for sensitive keys
482+
const sensitivePatterns = [
483+
/^ANTHROPIC_AUTH_TOKEN$/, // Exact match for Anthropic auth token
484+
/_API_KEY$/, // Keys ending with _API_KEY
485+
/_AUTH_TOKEN$/, // Keys ending with _AUTH_TOKEN
486+
/^API_KEY$/, // Exact match for API_KEY
487+
/^AUTH_TOKEN$/, // Exact match for AUTH_TOKEN
488+
];
482489

483490
for (const key of Object.keys(masked.env)) {
484-
if (sensitiveKeys.some((sensitive) => key.includes(sensitive))) {
491+
if (sensitivePatterns.some((pattern) => pattern.test(key))) {
485492
const value = masked.env[key];
486493
if (value && value.length > 8) {
487494
masked.env[key] =

ui/src/components/settings-dialog.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,17 @@ function SettingsDialogContent({
145145
};
146146

147147
const isSensitiveKey = (key: string): boolean => {
148-
return key.includes('TOKEN') || key.includes('KEY') || key.includes('SECRET');
148+
// Pattern-based matching for sensitive keys (same as backend)
149+
const sensitivePatterns = [
150+
/^ANTHROPIC_AUTH_TOKEN$/, // Exact match for Anthropic auth token
151+
/_API_KEY$/, // Keys ending with _API_KEY
152+
/_AUTH_TOKEN$/, // Keys ending with _AUTH_TOKEN
153+
/^API_KEY$/, // Exact match for API_KEY
154+
/^AUTH_TOKEN$/, // Exact match for AUTH_TOKEN
155+
/_SECRET$/, // Keys ending with _SECRET
156+
/^SECRET$/, // Exact match for SECRET
157+
];
158+
return sensitivePatterns.some((pattern) => pattern.test(key));
149159
};
150160

151161
return (

0 commit comments

Comments
 (0)