2026-06-12 18:24:07 +03:00
|
|
|
import { requestExistingFileAccess } from '@/lib/desktop';
|
|
|
|
|
import { isFilePathWithinDirectory, normalizeFilePath } from '@/lib/path-utils';
|
2026-08-22 19:26:20 +02:00
|
|
|
import { getRuntimeKey } from '@/lib/runtime-switch';
|
2026-06-12 18:24:07 +03:00
|
|
|
|
|
|
|
|
type OutsideFileGrantEntry = {
|
|
|
|
|
outsideFileGrant: string;
|
|
|
|
|
expiresAt: number;
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-22 19:26:20 +02:00
|
|
|
const GRANT_RENEWAL_BUFFER_MS = 5_000;
|
|
|
|
|
const grantsByCacheKey = new Map<string, OutsideFileGrantEntry>();
|
|
|
|
|
const pendingGrantsByCacheKey = new Map<string, Promise<string | undefined>>();
|
|
|
|
|
|
|
|
|
|
const grantCacheKey = (path: string, runtimeKey = getRuntimeKey()): string => `${runtimeKey}\0${path}`;
|
2026-06-12 18:24:07 +03:00
|
|
|
|
|
|
|
|
export const getOutsideFileGrant = (path: string): string | undefined => {
|
|
|
|
|
const normalizedPath = normalizeFilePath(path);
|
|
|
|
|
if (!normalizedPath) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-22 19:26:20 +02:00
|
|
|
const cacheKey = grantCacheKey(normalizedPath);
|
|
|
|
|
const entry = grantsByCacheKey.get(cacheKey);
|
2026-06-12 18:24:07 +03:00
|
|
|
if (!entry) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entry.expiresAt <= Date.now()) {
|
2026-08-22 19:26:20 +02:00
|
|
|
grantsByCacheKey.delete(cacheKey);
|
2026-06-12 18:24:07 +03:00
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entry.outsideFileGrant;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-26 19:27:53 +03:00
|
|
|
const rememberOutsideFileGrant = (
|
2026-06-12 18:24:07 +03:00
|
|
|
path: string,
|
|
|
|
|
outsideFileGrant: string,
|
2026-08-22 19:26:20 +02:00
|
|
|
expiresAt: number,
|
|
|
|
|
runtimeKey: string,
|
2026-06-12 18:24:07 +03:00
|
|
|
): void => {
|
|
|
|
|
const normalizedPath = normalizeFilePath(path);
|
|
|
|
|
if (!normalizedPath || !outsideFileGrant) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-22 19:26:20 +02:00
|
|
|
grantsByCacheKey.set(grantCacheKey(normalizedPath, runtimeKey), {
|
2026-06-12 18:24:07 +03:00
|
|
|
outsideFileGrant,
|
2026-08-22 19:26:20 +02:00
|
|
|
expiresAt: expiresAt - GRANT_RENEWAL_BUFFER_MS,
|
2026-06-12 18:24:07 +03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const ensureOutsideFileGrantForDesktop = async (
|
|
|
|
|
path: string,
|
|
|
|
|
workspaceRoot: string,
|
|
|
|
|
): Promise<string | undefined> => {
|
|
|
|
|
const normalizedPath = normalizeFilePath(path);
|
|
|
|
|
if (!normalizedPath || !workspaceRoot || isFilePathWithinDirectory(normalizedPath, workspaceRoot)) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-22 19:26:20 +02:00
|
|
|
const runtimeKey = getRuntimeKey();
|
|
|
|
|
if (runtimeKey !== 'local') {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
const cacheKey = grantCacheKey(normalizedPath, runtimeKey);
|
2026-06-12 18:24:07 +03:00
|
|
|
const existing = getOutsideFileGrant(normalizedPath);
|
|
|
|
|
if (existing) {
|
|
|
|
|
return existing;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-22 19:26:20 +02:00
|
|
|
const pending = pendingGrantsByCacheKey.get(cacheKey);
|
|
|
|
|
if (pending) {
|
|
|
|
|
return pending;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const request = requestExistingFileAccess(normalizedPath).then((result) => {
|
|
|
|
|
if (!result.success || getRuntimeKey() !== runtimeKey) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { path: grantedPath, outsideFileGrant, expiresAt } = result;
|
|
|
|
|
if (expiresAt <= Date.now() + GRANT_RENEWAL_BUFFER_MS) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
rememberOutsideFileGrant(grantedPath, outsideFileGrant, expiresAt, runtimeKey);
|
|
|
|
|
if (normalizeFilePath(grantedPath) !== normalizedPath) {
|
|
|
|
|
rememberOutsideFileGrant(normalizedPath, outsideFileGrant, expiresAt, runtimeKey);
|
|
|
|
|
}
|
|
|
|
|
return outsideFileGrant;
|
|
|
|
|
});
|
|
|
|
|
pendingGrantsByCacheKey.set(cacheKey, request);
|
|
|
|
|
try {
|
|
|
|
|
return await request;
|
|
|
|
|
} finally {
|
|
|
|
|
pendingGrantsByCacheKey.delete(cacheKey);
|
2026-06-12 18:24:07 +03:00
|
|
|
}
|
2026-08-22 19:26:20 +02:00
|
|
|
};
|
2026-06-12 18:24:07 +03:00
|
|
|
|
2026-08-22 19:26:20 +02:00
|
|
|
export const resolveOutsideFileReadOptions = async (
|
|
|
|
|
path: string,
|
|
|
|
|
workspaceRoot: string,
|
|
|
|
|
enabled: boolean,
|
|
|
|
|
): Promise<{ allowOutsideWorkspace: boolean; outsideFileGrant?: string }> => {
|
|
|
|
|
const allowOutsideWorkspace = enabled
|
|
|
|
|
&& Boolean(workspaceRoot)
|
|
|
|
|
&& !isFilePathWithinDirectory(path, workspaceRoot);
|
|
|
|
|
if (!allowOutsideWorkspace) {
|
|
|
|
|
return { allowOutsideWorkspace: false };
|
2026-06-12 18:24:07 +03:00
|
|
|
}
|
2026-08-22 19:26:20 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
allowOutsideWorkspace: true,
|
|
|
|
|
outsideFileGrant: await ensureOutsideFileGrantForDesktop(path, workspaceRoot),
|
|
|
|
|
};
|
2026-06-12 18:24:07 +03:00
|
|
|
};
|