BigPizzaV3/CodexPlusPlus · error
当前页面不是安全上下文,WebCrypto 不可用
Error message
当前页面不是安全上下文,WebCrypto 不可用
What it means
The embedded mobile relay page derives an AES-GCM key from the user-entered passphrase via crypto.subtle (SHA-256 digest + importKey). WebCrypto's SubtleCrypto is only exposed in secure contexts (HTTPS or localhost). When the page is opened over plain HTTP on a non-localhost host, `crypto?.subtle` is undefined and cryptoKey() throws this error before any encryption can happen.
Solutions
- Serve the relay page over HTTPS (add TLS to the relay server or put it behind an HTTPS reverse proxy).
- Access via http://localhost / 127.0.0.1 where browsers still expose SubtleCrypto.
- Check `window.isSecureContext` before rendering the page and show a clear setup hint instead of failing at first use.
- As a last resort, fall back to a WebCrypto polyfill or refuse to run in insecure contexts explicitly.
Example fix
// before
async function cryptoKey() {
if (!crypto?.subtle) throw new Error("当前页面不是安全上下文,WebCrypto 不可用");
// after
async function cryptoKey() {
if (!window.isSecureContext || !crypto?.subtle) {
throw new Error("当前页面不是安全上下文(需 HTTPS 或 localhost),WebCrypto 不可用");
} Defensive patterns
Strategy: validation
Validate before calling
if (!window.isSecureContext || !crypto?.subtle) { alert("请通过 HTTPS 或 localhost 访问本页面"); throw new Error("WebCrypto 不可用"); } Type guard
function hasWebCrypto(w = window): boolean { return typeof crypto !== "undefined" && !!crypto.subtle && typeof crypto.subtle.importKey === "function"; } Try / catch
try { const key = await cryptoKey(); } catch (e) { if (String(e.message).includes("WebCrypto")) showInsecureContextHint(); else throw e; } Prevention
- Always serve the relay over HTTPS or access via localhost.
- Check `window.isSecureContext` at page load and disable crypto features early with a clear hint.
- Add a TLS reverse proxy for LAN/phone access instead of plain HTTP.
When it happens
Trigger: Calling cryptoKey() (directly or via encrypt()/decrypt() during a request or RPC) while `crypto.subtle` is undefined — i.e. the relay page is served over http:// from a LAN IP or hostname rather than https:// or http://localhost.
Common situations: Developer browses the relay page from a phone at http://192.168.x.x:port; reverse proxy terminates TLS elsewhere so the page itself is served over HTTP; browser treats the origin as insecure; mixed-content or self-signed-cert bypass leads to plain HTTP.
Related errors
- 不支持此分享的数据格式。
- API 地址须使用 HTTPS,且不含账号、密码、查询参数或片段
- API 地址须使用 HTTPS,且不含账号、密码、查询参数或片段
- 链接缺少解密密钥。请使用完整的分享链接。
- Base URL 不得指向本机或私有网络
AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19).
Data as JSON: /api/errors/d3820275e79838c1.
Report an issue: GitHub.
Appendix: source
Thrown at apps/codex-plus-mobile-relay/src/main.rs:792
const params = new URLSearchParams(location.search);
const statusEl = $("status");
const sessionsEl = $("sessions");
const messagesEl = $("messages");
const titleEl = $("threadTitle");
const metaEl = $("threadMeta");
const sessionsPane = $("sessionsPane");
const detailPane = $("detailPane");
function setStatus(text, error = false) { statusEl.textContent = text; statusEl.classList.toggle("error", error); }
function b64url(bytes) {
let text = btoa(String.fromCharCode(...new Uint8Array(bytes)));
return text.replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/g, "");
}
function b64urlDecode(text) {
const padded = text.replaceAll("-", "+").replaceAll("_", "/") + "===".slice((text.length + 3) % 4);
return Uint8Array.from(atob(padded), ch => ch.charCodeAt(0));
}
async function cryptoKey() {
if (!crypto?.subtle) throw new Error("当前页面不是安全上下文,WebCrypto 不可用");
const raw = new TextEncoder().encode($("key").value);
const digest = await crypto.subtle.digest("SHA-256", raw);
return crypto.subtle.importKey("raw", digest, "AES-GCM", false, ["encrypt", "decrypt"]);
}
async function encrypt(payload) {
if (!crypto?.subtle) {
setStatus("当前浏览器禁用 WebCrypto,已使用兼容模式", true);
return { type: "plaintext", payload };
}
const key = await cryptoKey();
const nonce = crypto.getRandomValues(new Uint8Array(12));
const plain = new TextEncoder().encode(JSON.stringify(payload));
const encrypted = await crypto.subtle.encrypt({ name: "AES-GCM", iv: nonce }, key, plain);
return { type: "encrypted", nonce: b64url(nonce), payload: b64url(encrypted) };
}
async function decrypt(envelope) {
if (envelope?.type === "plaintext") return envelope.payload;
if (!envelope || envelope.type !== "encrypted") throw new Error("收到未加密数据包");View on GitHub (pinned to b1ed92e5e4)