Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

fix(access): never emit http:// endpoints for token-bearing snippets on https (#132)

Robert06227df6ada3ff72
diff --git a/web/app/access.js b/web/app/access.js
index 7a42c53b..5fcfe8ee 100644
--- a/web/app/access.js
+++ b/web/app/access.js
@@ -15,15 +15,26 @@ function accessMcpAgentPrompt(spec,base,token){
}
// 对外展示的固定接入地址:优先使用部署下发的 APP_DOMAIN,避免硬编码固定域名。
function getPublicBaseURL(){
- const domain = (typeof window !== 'undefined' && (window.__AGENTFLOW_APP_DOMAIN__ || window.__AGENTFLOW_PUBLIC_BASE_URL__))
- || (typeof state !== 'undefined' && state?.appDomain ? state.appDomain : '')
- || (typeof window !== 'undefined' && window.location?.host ? window.location.host : '')
- || 'loopforge.example.com';
- return 'http://' + domain;
+ const configured = (typeof window !== 'undefined' && (window.__AGENTFLOW_APP_DOMAIN__ || window.__AGENTFLOW_PUBLIC_BASE_URL__))
+ || (typeof state !== 'undefined' && state?.appDomain ? state.appDomain : '');
+ return accessPublicBaseURL(configured, typeof window !== 'undefined' ? window.location : null);
+}
+// 接入片段会携带 Bearer Token:配置里显式的协议优先,否则跟随当前页面;
+// HTTPS 页面生成的地址不降级为 http://。只有配置显式写了 http:// 和非 443 端口时
+// 保留 http,因为把只提供 HTTP 的端口改成 https:// 会指向一个没有 TLS 的端口。
+function accessPublicBaseURL(configured, page){
+ const secure = page?.protocol === 'https:';
+ const fallback = (secure ? 'https:' : 'http:') + '//' + (page?.host || 'loopforge.example.com');
+ const raw = String(configured || '').trim();
+ if(!raw) return fallback;
+ const explicitScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(raw);
+ let url;
+ try{ url = new URL(explicitScheme ? raw : (secure ? 'https://' : 'http://') + raw) }catch(_){ return fallback }
+ if(!['http:','https:'].includes(url.protocol) || !url.host) return fallback;
+ const explicitHTTPPort = explicitScheme && url.protocol === 'http:' && url.port && url.port !== '443';
+ if(secure && !explicitHTTPPort) url.protocol = 'https:';
+ return url.protocol + '//' + url.host + url.pathname.replace(/\/+$/, '');
}
-const PUBLIC_BASE_URL = typeof window !== 'undefined' && (window.__AGENTFLOW_APP_DOMAIN__ || window.location?.host)
- ? ('http://' + (window.__AGENTFLOW_APP_DOMAIN__ || window.location.host))
- : 'http://loopforge.example.com';
function accessViewActive(){return !!$('#guideView')?.classList.contains('active')}
let credentialFocusRequest=0;
function gotoCredential(name){
diff --git a/webtest/lib/load-app.mjs b/webtest/lib/load-app.mjs
index c12208cc..83ade4eb 100644
--- a/webtest/lib/load-app.mjs
+++ b/webtest/lib/load-app.mjs
@@ -161,12 +161,12 @@ class ObserverStub {
takeRecords() { return []; }
}
-export async function loadApp({ routes = [], hash = '', dispatchReady = true } = {}) {
+export async function loadApp({ routes = [], hash = '', dispatchReady = true, origin = 'http://127.0.0.1' } = {}) {
const html = await readFile(path.join(WEB_ROOT, 'index.html'), 'utf8');
const dom = new JSDOM(html, {
runScripts: 'outside-only',
pretendToBeVisual: true,
- url: `http://127.0.0.1/${hash ? `#${hash.replace(/^#/, '')}` : ''}`,
+ url: `${origin}/${hash ? `#${hash.replace(/^#/, '')}` : ''}`,
});
const { window } = dom;
const router = createFetchRouter(routes);
diff --git a/webtest/tests/access-guides.test.mjs b/webtest/tests/access-guides.test.mjs
index 753d485e..d010edd5 100644
--- a/webtest/tests/access-guides.test.mjs
+++ b/webtest/tests/access-guides.test.mjs
@@ -165,6 +165,63 @@ test('personal token UI creates and revokes tokens through the API', async (t) =
assert.ok(app.router.calls.some(c => c.method === 'DELETE' && c.path === '/api/auth/tokens/token-1'));
});
+test('token-bearing access base never downgrades a secure deployment', async (t) => {
+ const app = await loadApp();
+ t.after(app.close);
+ const base = (configured, protocol, host = 'page.example') => app.evaluate(
+ `accessPublicBaseURL(${JSON.stringify(configured)}, ${JSON.stringify({ protocol, host })})`);
+ // Bare APP_DOMAIN follows the studio page scheme.
+ assert.equal(base('studio.example', 'https:'), 'https://studio.example');
+ assert.equal(base('studio.example:8443', 'https:'), 'https://studio.example:8443');
+ assert.equal(base('studio.example', 'http:'), 'http://studio.example');
+ // An explicit https base wins, and an https page upgrades an explicit http base.
+ assert.equal(base('https://studio.example/', 'http:'), 'https://studio.example');
+ assert.equal(base('https://studio.example/lf/', 'http:'), 'https://studio.example/lf');
+ assert.equal(base('http://studio.example', 'https:'), 'https://studio.example');
+ assert.equal(base('http://studio.example', 'http:'), 'http://studio.example');
+ assert.equal(base('http://studio.example:80', 'https:'), 'https://studio.example');
+ assert.equal(base('http://studio.example:443', 'https:'), 'https://studio.example');
+ // An explicit http base with its own port is an HTTP-only listener; https on
+ // that port would point the token at a port without TLS.
+ assert.equal(base('http://studio.example:8080', 'https:'), 'http://studio.example:8080');
+ assert.equal(base('http://studio.example:8080/lf/', 'https:'), 'http://studio.example:8080/lf');
+ assert.equal(base('https://studio.example:8443', 'https:'), 'https://studio.example:8443');
+ // Without a usable configured base, the page origin is used as is.
+ assert.equal(base('', 'https:'), 'https://page.example');
+ assert.equal(base('', 'http:', '127.0.0.1:8080'), 'http://127.0.0.1:8080');
+ assert.equal(base('javascript:alert(1)', 'https:'), 'https://page.example');
+ assert.equal(base('ftp://studio.example', 'http:'), 'http://page.example');
+});
+
+test('access snippets copied from an https studio use https endpoints', async (t) => {
+ const app = await loadApp({ origin: 'https://studio.example' });
+ t.after(app.close);
+ app.router.post('/api/auth/tokens', { token: 'af_live_secret' });
+ app.router.get('/api/auth/tokens', []);
+ app.evaluate(`
+ window.__AGENTFLOW_APP_DOMAIN__='studio.example';
+ state.currentUser={id:1,loginName:'alice',isGuest:false};
+ state.accessTokensStatus='ready';state.accessTokens=[];state.revealedToken='';
+ switchView('guide');
+ `);
+ assert.equal(app.evaluate('getPublicBaseURL()'), 'https://studio.example');
+ const form = app.document.querySelector('#accessTokenForm');
+ form.elements.name.value = 'Automation';
+ form.dispatchEvent(new app.window.Event('submit', { bubbles: true, cancelable: true }));
+ await settle(8);
+ assert.equal(app.evaluate(`state.revealedToken`), 'af_live_secret');
+ for (const client of ['cursor', 'claude', 'codebuddy', 'codex']) {
+ app.evaluate(`state.accessClient='${client}';renderAccessTokens()`);
+ const button = app.document.querySelector('#accessBody [data-copy-revealed-prompt]');
+ assert.ok(button, `${client} reveal offers the client prompt`);
+ const prompt = decodeURIComponent(button.dataset.copyRevealedPrompt);
+ assert.match(prompt, /af_live_secret/);
+ assert.match(prompt, /ENDPOINT = https:\/\/studio\.example\/mcp/);
+ assert.match(prompt, /https:\/\/studio\.example\/mcp-setup\.txt/);
+ assert.doesNotMatch(prompt + app.evaluate(`accessMcpClientSpec('${client}',getPublicBaseURL(),'t').code`), /http:\/\/studio\.example/, `${client} snippet downgraded the endpoint`);
+ }
+});
+
// renderAll() repaints every page; only the 访问密钥 page may ask for the token
// list, since Bearer sessions get 401 there and would leave it stuck on error.
test('the token list is only fetched once 访问密钥 is opened', async (t) => {