fix: harden evidence secret file handoff
This commit is contained in:
@@ -60,18 +60,18 @@ function isInside(path: string, root: string): boolean {
|
||||
return pathRelative !== "" && !pathRelative.startsWith("..") && !isAbsolute(pathRelative);
|
||||
}
|
||||
|
||||
function isSafeSecretFile(path: string, secretRoots: readonly string[]): boolean {
|
||||
if (!isAbsolute(path)) return false;
|
||||
function safeSecretFilePath(path: string, secretRoots: readonly string[]): string | undefined {
|
||||
if (!isAbsolute(path)) return undefined;
|
||||
|
||||
try {
|
||||
const resolvedPath = realpathSync(path);
|
||||
const resolvedRoots = secretRoots.map((root) => realpathSync(root));
|
||||
if (!resolvedRoots.some((root) => isInside(resolvedPath, root))) return false;
|
||||
if (!statSync(resolvedPath).isFile()) return false;
|
||||
if (!resolvedRoots.some((root) => isInside(resolvedPath, root))) return undefined;
|
||||
if (!statSync(resolvedPath).isFile()) return undefined;
|
||||
accessSync(resolvedPath, constants.R_OK);
|
||||
return true;
|
||||
return resolvedPath;
|
||||
} catch {
|
||||
return false;
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,11 +132,12 @@ export function resolveBinding(
|
||||
|
||||
const value = env[variable.name];
|
||||
const present = value !== undefined && value.trim() !== "";
|
||||
const safe = !variable.secret || (present && isSafeSecretFile(value, secretRoots));
|
||||
const safePath = variable.secret && present ? safeSecretFilePath(value, secretRoots) : undefined;
|
||||
const safe = !variable.secret || safePath !== undefined;
|
||||
if ((required.has(variable.suffix) && !present) || (present && !safe)) {
|
||||
missing.push(variable.name);
|
||||
}
|
||||
if (present && safe) values[variable.name] = value;
|
||||
if (present && safe) values[variable.name] = variable.secret ? safePath! : value;
|
||||
}
|
||||
|
||||
return { transport: selectedTransport, values, missing };
|
||||
@@ -166,11 +167,11 @@ export function resolveEvidenceBinding(
|
||||
for (const variable of variables) {
|
||||
const value = env[variable.name];
|
||||
const present = value !== undefined && value.trim() !== "";
|
||||
const safe = present && isSafeSecretFile(value, secretRoots);
|
||||
if ((required.has(variable.suffix) && !present) || (present && !safe)) {
|
||||
const safePath = present ? safeSecretFilePath(value, secretRoots) : undefined;
|
||||
if ((required.has(variable.suffix) && !present) || (present && safePath === undefined)) {
|
||||
missing.push(variable.name);
|
||||
}
|
||||
if (safe) values[variable.name] = value;
|
||||
if (safePath !== undefined) values[variable.name] = safePath;
|
||||
}
|
||||
return { values, missing };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user