(prefix: string, extension: string = '.tmp')
| 46 | * Create a secure temporary file path |
| 47 | */ |
| 48 | export function createSecureTempPath(prefix: string, extension: string = '.tmp'): string { |
| 49 | const tempDir = process.env.TEMP_DIR ?? os.tmpdir(); |
| 50 | |
| 51 | if (!isAllowedTempDir(tempDir)) { |
| 52 | throw new Error(`Temporary directory not allowed: ${tempDir}`); |
| 53 | } |
| 54 | |
| 55 | const randomSuffix = generateSecureRandomSuffix(); |
| 56 | const filename = `${prefix}_${randomSuffix}${extension}`; |
| 57 | |
| 58 | return path.join(tempDir, filename); |
| 59 | } |
| 60 | |
| 61 | // Dangerous character ranges (excluding tab and line feed) |
| 62 | const DANGEROUS_CHARS_REGEX = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g; |
no test coverage detected