Decouple bundled UI from runtime API and add remote instance tooling (#1228)
Add a packaged-client runtime boundary so the shared UI can talk to local, desktop, remote, and VS Code runtimes through the right transport instead of assuming one same-origin web server. Centralize OpenChamber-owned API access behind RuntimeAPIs, runtimeFetch, and runtime URL helpers, while keeping official OpenCode traffic on the SDK path. Support runtime switching, remote host selection, desktop client credentials, and headless connection links for pairing packaged clients with remote OpenChamber servers. Harden the new auth model by moving long-lived client tokens out of browser URLs, introducing short-lived scoped URL tokens for browser-owned transports, restricting URL-token access to explicit readable/realtime routes, and making client-token management session-scoped or self-scoped as appropriate. Update browser-owned assets and preview proxy flows to work with the split runtime model, including authenticated project icons, preview token propagation, CSP-safe preview bridge injection, and preview proxy auth that survives short-lived URL-token expiry. Tighten Electron security boundaries for packaged clients by gating privileged preload state to trusted origins and requiring explicit confirmation before connect deep-links import or switch remote runtimes. Also refresh agent guidance and project skills so future runtime/API, auth, preview, UI, CLI, settings, locale, and drag-to-reorder work follows the new architecture.
This commit is contained in:
committed by
GitHub
parent
a4314c189b
commit
2031e3b4a8
@@ -1,6 +1,13 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { classifyPreviewNavigation, classifyPreviewResourceError, normalizeProxyTargetUrl, rewritePreviewBody } from './proxy-runtime.js';
|
||||
import {
|
||||
classifyPreviewNavigation,
|
||||
classifyPreviewResourceError,
|
||||
normalizeProxyTargetUrl,
|
||||
rewritePreviewBody,
|
||||
rewritePreviewCspHeader,
|
||||
rewritePreviewRedirectLocation,
|
||||
} from './proxy-runtime.js';
|
||||
|
||||
const rewrite = (bodyText, kind) => rewritePreviewBody({
|
||||
bodyText,
|
||||
@@ -90,6 +97,29 @@ describe('preview body URL rewriting', () => {
|
||||
expect(output).toContain('const url = "/api/data";');
|
||||
});
|
||||
|
||||
it('removes CSP meta tags that block the preview bridge', () => {
|
||||
const input = '<meta http-equiv="Content-Security-Policy" content="script-src \'self\'"><div>Preview</div>';
|
||||
const output = rewrite(input, 'html');
|
||||
|
||||
expect(output).not.toContain('Content-Security-Policy');
|
||||
expect(output).toContain('<div>Preview</div>');
|
||||
});
|
||||
|
||||
it('adds preview and URL auth tokens to rewritten proxy resources when provided', () => {
|
||||
const output = rewritePreviewBody({
|
||||
bodyText: '<script src="/entry.js"></script><a href="http://localhost:3000/docs?x=1&oc_client_token=legacy">Docs</a>',
|
||||
kind: 'html',
|
||||
proxyBasePath: '/api/preview/proxy/abc123',
|
||||
targetOrigin: 'http://127.0.0.1:3000',
|
||||
previewToken: 'preview-secret',
|
||||
urlAuthToken: 'url-secret',
|
||||
});
|
||||
|
||||
expect(output).toContain('src="/api/preview/proxy/abc123/entry.js?oc_preview_token=preview-secret&oc_url_token=url-secret"');
|
||||
expect(output).toContain('href="/api/preview/proxy/abc123/docs?x=1&oc_preview_token=preview-secret&oc_url_token=url-secret"');
|
||||
expect(output).not.toContain('oc_client_token');
|
||||
});
|
||||
|
||||
it('rewrites only CSS imports and url references in CSS responses', () => {
|
||||
const input = '@import "/theme.css"; .hero { background: url(/hero.png); } .copy::after { content: "/not-a-url"; }';
|
||||
const output = rewrite(input, 'css');
|
||||
@@ -108,6 +138,66 @@ describe('preview body URL rewriting', () => {
|
||||
expect(output).toContain('const url = "/api/data"');
|
||||
expect(output).toContain('fetch("/api/data")');
|
||||
});
|
||||
|
||||
it('adds URL auth tokens to CSS and JavaScript rewritten resources', () => {
|
||||
const cssOutput = rewritePreviewBody({
|
||||
bodyText: '@import "/theme.css"; .hero { background: url(/hero.png); }',
|
||||
kind: 'css',
|
||||
proxyBasePath: '/api/preview/proxy/abc123',
|
||||
targetOrigin: 'http://127.0.0.1:3000',
|
||||
previewToken: 'preview-secret',
|
||||
urlAuthToken: 'url-secret',
|
||||
});
|
||||
const jsOutput = rewritePreviewBody({
|
||||
bodyText: 'import("/entry.js"); import value from "/module.js";',
|
||||
kind: 'javascript',
|
||||
proxyBasePath: '/api/preview/proxy/abc123',
|
||||
targetOrigin: 'http://127.0.0.1:3000',
|
||||
previewToken: 'preview-secret',
|
||||
urlAuthToken: 'url-secret',
|
||||
});
|
||||
|
||||
expect(cssOutput).toContain('@import "/api/preview/proxy/abc123/theme.css?oc_preview_token=preview-secret&oc_url_token=url-secret"');
|
||||
expect(cssOutput).toContain('url(/api/preview/proxy/abc123/hero.png?oc_preview_token=preview-secret&oc_url_token=url-secret)');
|
||||
expect(jsOutput).toContain('import("/api/preview/proxy/abc123/entry.js?oc_preview_token=preview-secret&oc_url_token=url-secret")');
|
||||
expect(jsOutput).toContain('from "/api/preview/proxy/abc123/module.js?oc_preview_token=preview-secret&oc_url_token=url-secret"');
|
||||
});
|
||||
});
|
||||
|
||||
describe('preview redirect URL rewriting', () => {
|
||||
it('rewrites loopback redirects through the preview proxy', () => {
|
||||
expect(rewritePreviewRedirectLocation({
|
||||
location: 'http://localhost:3000/login?next=%2F#top',
|
||||
proxyBasePath: '/api/preview/proxy/abc123',
|
||||
targetOrigin: 'http://127.0.0.1:3000',
|
||||
})).toBe('/api/preview/proxy/abc123/login?next=%2F#top');
|
||||
});
|
||||
|
||||
it('leaves external redirects unchanged', () => {
|
||||
expect(rewritePreviewRedirectLocation({
|
||||
location: 'https://example.com/login',
|
||||
proxyBasePath: '/api/preview/proxy/abc123',
|
||||
targetOrigin: 'http://127.0.0.1:3000',
|
||||
})).toBe('https://example.com/login');
|
||||
});
|
||||
|
||||
it('adds proxy auth tokens to loopback redirects when provided', () => {
|
||||
expect(rewritePreviewRedirectLocation({
|
||||
location: 'http://localhost:3000/login?next=%2F#top',
|
||||
proxyBasePath: '/api/preview/proxy/abc123',
|
||||
targetOrigin: 'http://127.0.0.1:3000',
|
||||
previewToken: 'preview-secret',
|
||||
urlAuthToken: 'url-secret',
|
||||
})).toBe('/api/preview/proxy/abc123/login?next=%2F&oc_preview_token=preview-secret&oc_url_token=url-secret#top');
|
||||
});
|
||||
|
||||
it('leaves redirects unchanged when no target origin is provided', () => {
|
||||
expect(rewritePreviewRedirectLocation({
|
||||
location: 'http://localhost:5174/callback',
|
||||
proxyBasePath: '/api/preview/proxy/abc123',
|
||||
previewToken: 'preview-secret',
|
||||
})).toBe('http://localhost:5174/callback');
|
||||
});
|
||||
});
|
||||
|
||||
describe('preview navigation policy', () => {
|
||||
@@ -187,3 +277,41 @@ describe('proxy target normalization (SSRF guard)', () => {
|
||||
expect(normalizeProxyTargetUrl('http://[::ffff:127.0.0.1]/', { allowExternal: true }).ok).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('preview CSP rewrite', () => {
|
||||
it('drops frame-ancestors and require-trusted-types-for but keeps the rest', () => {
|
||||
const result = rewritePreviewCspHeader(
|
||||
"default-src 'self'; frame-ancestors 'none'; require-trusted-types-for 'script'",
|
||||
'abc123',
|
||||
);
|
||||
expect(result).not.toContain('frame-ancestors');
|
||||
expect(result).not.toContain('require-trusted-types-for');
|
||||
expect(result).toContain("default-src 'self'");
|
||||
});
|
||||
|
||||
it('adds the nonce to an existing script-src instead of removing it', () => {
|
||||
const result = rewritePreviewCspHeader("script-src 'self'", 'abc123');
|
||||
expect(result).toContain("script-src 'self' 'nonce-abc123'");
|
||||
});
|
||||
|
||||
it('adds the nonce to script-src-elem when present', () => {
|
||||
const result = rewritePreviewCspHeader("script-src-elem 'self'", 'abc123');
|
||||
expect(result).toContain("script-src-elem 'self' 'nonce-abc123'");
|
||||
});
|
||||
|
||||
it('synthesizes script-src from default-src when no script directive exists', () => {
|
||||
const result = rewritePreviewCspHeader("default-src 'self' https://cdn.example.com", 'abc123');
|
||||
expect(result).toContain("default-src 'self' https://cdn.example.com");
|
||||
expect(result).toContain("script-src 'self' https://cdn.example.com 'nonce-abc123'");
|
||||
});
|
||||
|
||||
it("drops a lone 'none' so the nonce takes effect", () => {
|
||||
const result = rewritePreviewCspHeader("script-src 'none'", 'abc123');
|
||||
expect(result).toBe("script-src 'nonce-abc123'");
|
||||
});
|
||||
|
||||
it('returns empty/unset CSP values unchanged', () => {
|
||||
expect(rewritePreviewCspHeader('', 'abc123')).toBe('');
|
||||
expect(rewritePreviewCspHeader(undefined, 'abc123')).toBe(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user