The service on 127.0.0.1:3477 answered Origin: null (any site's sandboxed iframe or a data: page) with ACAO null, and had no Host check, so a hostile page could POST /api/chat on the user's magpie gateway key, read and rewrite conversations and settings, and drive MCP tools; a DNS-rebound page could read conversations with a plain GET. - Every /api/ route except ping, fonts.css and icon/* now needs a per-process token (x-magpie-chat-token, timing-safe compare). The token is put in index.html as a meta tag, and index.html goes out without CORS, so only a page that navigated to the service can read it. app.js sends it on every call and reloads once when the service restarted. - The Host must be 127.0.0.1:<port> or localhost:<port>, checked first. - index.mjs starts the service when the host calls server(), not on import, and passes the plugin option's port, which page/boot.js already read; before, a port set in the options moved the page and not the service. - Port 0 binds a free port (tests); the actual port is used for the Host and Origin lists. - /api/icon's remote icon answered 302 without a Location; it has one. - /api/bootstrap reports the package version instead of "0.1.0". - package.json: 0.1.0 in magpie-community/plugins, author credit, start runs serve.mjs. Co-authored-by: Magiczerowxy <magiczerowxy@users.noreply.github.com>
43 lines
1.7 KiB
JavaScript
43 lines
1.7 KiB
JavaScript
// @magpie-community/page-chat:在 magpie 窗口里加一个「对话」页面。
|
|
//
|
|
// 两件事:
|
|
// 1. 作为 OpenCode v1 插件被 magpie 的 Bun 宿主加载(不注册任何供应商,只借宿主的进程起服务)。
|
|
// 2. 宿主调起插件时起一个本地 HTTP 服务,窗口里的页面把自己导航过去,界面与 magpie 网关同源直连。
|
|
//
|
|
// magpie 的插件页面跑在 sandbox="allow-scripts" 的框架里,没有网络权限、不能内嵌框架、
|
|
// 不能用存储,唯一能做的联网动作是把自身导航到别处(文档已明说)。所以这里只是一个
|
|
// 「带路页」,真正的界面由本插件的服务提供。
|
|
|
|
import { startServer } from "./lib/server.mjs";
|
|
|
|
// 服务在宿主调 server() 时才起(宿主加载插件就会调),只起一次;
|
|
// 单纯 import 这个模块不占端口。插件选项里的 port 和带路页(page/boot.js)读的是同一个值。
|
|
let started = null;
|
|
function start(options) {
|
|
if (!started) {
|
|
const port = Number(options?.port);
|
|
started = startServer({
|
|
configDir: process.env.MAGPIE_CHAT_CONFIG || "",
|
|
options: Number.isFinite(port) && port > 0 ? { port } : {},
|
|
})
|
|
.then((info) => {
|
|
console.log(`[page-chat] listening on http://127.0.0.1:${info.port}`);
|
|
return info;
|
|
})
|
|
.catch((err) => {
|
|
console.error("[page-chat] could not start:", err?.message || err);
|
|
return null;
|
|
});
|
|
}
|
|
return started;
|
|
}
|
|
|
|
// magpie 会把每个导出的函数当成一个插件调用;用 OpenCode v1 的形式只导出服务器。
|
|
export default {
|
|
id: "magpie-chat",
|
|
server: async (input, options) => {
|
|
await start(options);
|
|
return {};
|
|
},
|
|
};
|