Files
openchamber/packages/web/server/lib/opencode/project-directory-runtime.js
T
FanFan4204andBohdan Triapitsyn efd621b087 fix: handle non-ISO-8859-1 characters in fetch headers and Content-Disposition (#1673)
* fix: handle non-ISO-8859-1 characters in fetch headers and Content-Disposition

Browser Headers API rejects characters above U+00FF. The x-opencode-directory header carries raw filesystem paths, which breaks when paths contain Chinese/CJK characters. Also fixes Content-Disposition for non-ASCII filenames per RFC 5987.

* refactor: export header sanitization helpers, deduplicate, add tests

Export isLatin1Safe and sanitizeHeadersForBrowser from runtime-fetch.ts so VS Code webview can import them instead of duplicating the logic. Add tests: isLatin1Safe boundary checks, sanitizeHeadersForBrowser encoding/deduplication, runtimeFetch round-trip encode/decode, and Content-Disposition RFC 5987 output for both ASCII and non-ASCII filenames.

* fix: mark encoded directory headers

---------

Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
2026-06-23 19:49:44 +03:00

143 lines
5.4 KiB
JavaScript

import { createRealpathCache } from '../path-realpath-cache.js';
// Browser transport percent-encodes directory hints and marks them explicitly.
// Only marked values are decoded so literal percent sequences from direct API
// clients are preserved.
const safeDecodeMarkedURIComponent = (value, encoding) => {
if (encoding !== 'uri') return value;
try { return decodeURIComponent(value); } catch { return value; }
};
export const createProjectDirectoryRuntime = (dependencies) => {
const {
fsPromises,
path,
normalizeDirectoryPath,
readSettingsFromDiskMigrated,
getReadSettingsFromDiskMigrated,
sanitizeProjects,
} = dependencies;
const realpathCache = createRealpathCache({
realpath: fsPromises.realpath.bind(fsPromises),
});
const resolveDirectoryCandidate = (value) => {
if (typeof value !== 'string') {
return null;
}
const trimmed = value.trim();
if (!trimmed) {
return null;
}
const normalized = normalizeDirectoryPath(trimmed);
return path.resolve(normalized);
};
const validateDirectoryPath = async (candidate) => {
const resolved = resolveDirectoryCandidate(candidate);
if (!resolved) {
return { ok: false, error: 'Directory parameter is required' };
}
try {
const stats = await fsPromises.stat(resolved);
if (!stats.isDirectory()) {
return { ok: false, error: 'Specified path is not a directory' };
}
const realPath = await realpathCache.resolve(resolved);
return { ok: true, directory: realPath };
} catch (error) {
const err = error;
if (err && typeof err === 'object' && err.code === 'ENOENT') {
return { ok: false, error: 'Directory not found' };
}
if (err && typeof err === 'object' && err.code === 'EACCES') {
return { ok: false, error: 'Access to directory denied' };
}
return { ok: false, error: 'Failed to validate directory' };
}
};
const resolveProjectDirectory = async (req) => {
const rawHeaderDirectory = typeof req.get === 'function' ? req.get('x-opencode-directory') : null;
const headerEncoding = typeof req.get === 'function' ? req.get('x-opencode-directory-encoding') : null;
const headerDirectory = rawHeaderDirectory ? safeDecodeMarkedURIComponent(rawHeaderDirectory, headerEncoding) : null;
const queryDirectory = Array.isArray(req.query?.directory)
? req.query.directory[0]
: req.query?.directory;
const requested = headerDirectory || queryDirectory || null;
if (requested) {
const validated = await validateDirectoryPath(requested);
if (!validated.ok) {
return { directory: null, error: validated.error };
}
return { directory: validated.directory, error: null };
}
const readSettings = typeof getReadSettingsFromDiskMigrated === 'function'
? getReadSettingsFromDiskMigrated()
: readSettingsFromDiskMigrated;
const settings = await readSettings();
// `lastDirectory` reflects the directory the UI is currently browsing —
// useDirectoryStore.setDirectory() persists it on every navigation.
// Prefer it over activeProjectId, because the user may have navigated
// away from the project that was last "clicked" in the sidebar (e.g. via
// `go to parent`, directory picker, or a deep link), leaving
// activeProjectId stale. Fetches scoped to the stale project would 400
// with "Path is outside of active workspace".
if (typeof settings.lastDirectory === 'string' && settings.lastDirectory.trim()) {
const validated = await validateDirectoryPath(settings.lastDirectory);
if (validated.ok) {
return { directory: validated.directory, error: null };
}
}
const projects = sanitizeProjects(settings.projects) || [];
if (projects.length === 0) {
return { directory: null, error: 'Directory parameter or active project is required' };
}
const activeId = typeof settings.activeProjectId === 'string' ? settings.activeProjectId : '';
const active = projects.find((project) => project.id === activeId) || projects[0];
if (!active || !active.path) {
return { directory: null, error: 'Directory parameter or active project is required' };
}
const validated = await validateDirectoryPath(active.path);
if (!validated.ok) {
return { directory: null, error: validated.error };
}
return { directory: validated.directory, error: null };
};
const resolveOptionalProjectDirectory = async (req) => {
const rawHeaderDirectory = typeof req.get === 'function' ? req.get('x-opencode-directory') : null;
const headerEncoding = typeof req.get === 'function' ? req.get('x-opencode-directory-encoding') : null;
const headerDirectory = rawHeaderDirectory ? safeDecodeMarkedURIComponent(rawHeaderDirectory, headerEncoding) : null;
const queryDirectory = Array.isArray(req.query?.directory)
? req.query.directory[0]
: req.query?.directory;
const requested = headerDirectory || queryDirectory || null;
if (!requested) {
return { directory: null, error: null };
}
const validated = await validateDirectoryPath(requested);
if (!validated.ok) {
return { directory: null, error: validated.error };
}
return { directory: validated.directory, error: null };
};
return {
resolveDirectoryCandidate,
validateDirectoryPath,
resolveProjectDirectory,
resolveOptionalProjectDirectory,
};
};