Files
openchamber/packages/web/server/proxy-headers.js
T
Bohdan Triapitsyn c703db2745 fix: stop forwarding client auth to OpenCode and harden home/session state
Packaged desktop showed no sessions in 1.12.4. Root cause: the sanitized
session-list proxy path added in #1538 forwarded the renderer's
"authorization" header (the OpenChamber UI client token) to the managed
OpenCode upstream alongside the managed "Authorization" credential.
OpenCode does not recognize UI client tokens, so every session-list
request answered 401 — only in the packaged app, because only its
renderer (openchamber-ui:// origin) attaches a bearer token; dev web and
dev Electron run same-origin without one. The legacy http-proxy path
overwrote the header correctly, which is why everything except session
lists kept working.

Proxy fix:
- proxy-headers: filter the client "authorization" header out of
  forwarded request headers; the OpenCode upstream must only ever see
  its own managed credentials. Covered by tests.

Desktop cwd:
- electron: launch the managed OpenCode CLI from the user home instead
  of app userData, matching upstream desktop behavior. userData-as-cwd
  made OpenCode treat the app-data folder as a separate empty workspace.

Home directory poisoning loop:
- directoryPersistence: stop replaying localStorage homeDirectory
  through synchronizeHomeDirectory on boot/auth resync. The persisted
  value is only a boot-time cache; replaying it re-wrote stale values
  (e.g. a project path) into desktop settings on every start, overriding
  the authoritative /api/fs/home resolution.
- persistence: never overwrite an injected window.__OPENCHAMBER_HOME__
  with a persisted value.
- useDirectoryStore: host switches happen in place (no reload), so
  re-resolve home from the new runtime's /api/fs/home on endpoint
  change instead of keeping the previous host's value.
- opencode client: only short-circuit to the injected desktop home when
  the active runtime is local; remote runtimes ask /api/fs/home.

Settings hygiene:
- persistSettings: log field names only — change payloads can carry
  credentials (UI password, client tokens, tunnel tokens) that must not
  reach the log file; drop step-by-step log chatter.
- validateProjectEntries: only stat project paths when the incoming
  update actually touches the projects list, not on every settings save.
- remove the write-only approvedDirectories setting everywhere and add
  a migration that strips the stale key from persisted settings.

Tests:
- usePluginsStore.test: register an own runtime-fetch module mock so the
  suite is independent of process-global mock.module leakage from other
  files, and restore globalThis.fetch after the suite.
- persistence.test: clean up the window global created for the suite.
2026-06-12 01:53:38 +03:00

66 lines
1.7 KiB
JavaScript

const filteredRequestHeaders = new Set([
// Client credentials for the OpenChamber server (UI client tokens) must
// never reach the managed OpenCode upstream — it only accepts its own auth,
// so a forwarded client bearer turns every upstream response into a 401.
'authorization',
'host',
'connection',
'content-length',
'transfer-encoding',
'keep-alive',
'te',
'trailer',
'upgrade',
'accept-encoding',
]);
const filteredResponseHeaders = new Set([
'connection',
'content-length',
'transfer-encoding',
'keep-alive',
'te',
'trailer',
'upgrade',
'www-authenticate',
'content-encoding',
]);
export const collectForwardProxyHeaders = (requestHeaders, authHeaders = {}) => {
const headers = {};
for (const [key, value] of Object.entries(requestHeaders || {})) {
if (!value) continue;
const normalizedKey = key.toLowerCase();
if (filteredRequestHeaders.has(normalizedKey)) continue;
headers[normalizedKey] = Array.isArray(value) ? value.join(', ') : String(value);
}
if (authHeaders.Authorization) {
headers.Authorization = authHeaders.Authorization;
}
return headers;
};
export const shouldForwardProxyResponseHeader = (key) => {
if (typeof key !== 'string' || key.trim().length === 0) {
return false;
}
return !filteredResponseHeaders.has(key.toLowerCase());
};
export const applyForwardProxyResponseHeaders = (responseHeaders, response) => {
if (!responseHeaders || typeof response?.setHeader !== 'function') {
return;
}
for (const [key, value] of responseHeaders.entries()) {
if (!shouldForwardProxyResponseHeader(key)) {
continue;
}
response.setHeader(key, value);
}
};