Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ export function buildCacheKey(
.replace(/\//g, '_');

// Prefix with ticker when available for human-readable filenames (optional)
const ticker = typeof params.ticker === 'string' ? params.ticker.toUpperCase() : null;
// Sanitize ticker to prevent path traversal: allow only alphanumeric and hyphens
const ticker = typeof params.ticker === 'string'
? params.ticker.replace(/[^a-zA-Z0-9-]/g, '').toUpperCase()
: null;
const prefix = ticker ? `${ticker}_` : '';

return `${cleanEndpoint}/${prefix}${hash}.json`;
Expand Down
18 changes: 13 additions & 5 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { readFileSync, writeFileSync } from 'fs';
import { config } from 'dotenv';

// Load .env on module import
Expand Down Expand Up @@ -39,7 +39,7 @@ export function checkApiKeyExists(apiKeyName: string): boolean {
}

// Also check .env file directly
if (existsSync('.env')) {
try {
const envContent = readFileSync('.env', 'utf-8');
const lines = envContent.split('\n');
for (const line of lines) {
Expand All @@ -54,6 +54,8 @@ export function checkApiKeyExists(apiKeyName: string): boolean {
}
}
}
} catch (e) {
// .env doesn't exist, ignore
}

return false;
Expand All @@ -64,8 +66,14 @@ export function saveApiKeyToEnv(apiKeyName: string, apiKeyValue: string): boolea
let lines: string[] = [];
let keyUpdated = false;

if (existsSync('.env')) {
const existingContent = readFileSync('.env', 'utf-8');
let existingContent = '';
try {
existingContent = readFileSync('.env', 'utf-8');
} catch (e) {
// .env doesn't exist or is not readable, which is fine
}

if (existingContent) {
const existingLines = existingContent.split('\n');

for (const line of existingLines) {
Expand Down Expand Up @@ -111,4 +119,4 @@ export function saveApiKeyForProvider(providerId: string, apiKey: string): boole
const apiKeyName = getApiKeyNameForProvider(providerId);
if (!apiKeyName) return false;
return saveApiKeyToEnv(apiKeyName, apiKey);
}
}