diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a09c50..f232ea8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Added - **`ext/web` 样式装配(`web_style` / `网页.样式装配`)**: 样式即数据表格 —— GFM 样式表(`|选择器|属性|值|`,可用 `|媒体|` 列分组进 `@media` 块)经 `样式装配` 函数转成 CSS 文本,再拼装成完整主题。替代"整段手写 CSS 字符串"的写法,贯彻 文档即代码。见 [marqdo-blog](examples/marqdo-blog/styles/theme.mq.md)。 +- **网络能力调研(`doc/design/web-net-capabilities.md`)**: 盘点 `ext/web` + `plugins/web` + `lib/net` 现状,对照主流语言网络栈(FastAPI / Express / Flask / axum),给出「开发一个完整 Web 项目」所需能力的差距清单与分波次补强路线(W1 中间件+JSON → W2 数据层 → W3 安全硬化 → W4 内容标配 → W5 上传 → W6 进阶)。 ### Changed - **Frontmatter import syntax**: `import bind:target` / `导入 bind:target` (file `.mq.md` or short name `lib.member`). Removed legacy `> path.mq.md` / `> use` imports. See [module-namespace.md](doc/design/module-namespace.md). diff --git a/Cargo.lock b/Cargo.lock index 383c977..a0f9269 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -94,6 +94,18 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "argon2" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c3610892ee6e0cbce8ae2700349fcf8f98adb0dbfbee85aec3c9179d29cc072" +dependencies = [ + "base64ct", + "blake2", + "cpufeatures", + "password-hash", +] + [[package]] name = "ascii" version = "1.1.0" @@ -115,6 +127,18 @@ dependencies = [ "wait-timeout", ] +[[package]] +name = "async-compression" +version = "0.4.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3976abdc8fe7d1133d43d304afd42abdf5bc3e1319d263d223bde07b5efc4be8" +dependencies = [ + "compression-codecs", + "compression-core", + "pin-project-lite", + "tokio", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -188,12 +212,27 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "base64ct" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + [[package]] name = "bitflags" version = "2.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b588b76d00fde79687d7646a9b5bdf3cc0f655e0bbd080335a95d7e96f3587da" +[[package]] +name = "blake2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" +dependencies = [ + "digest", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -311,6 +350,23 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" +[[package]] +name = "compression-codecs" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce2548391e9c1929c21bf6aa2680af86fe4c1b33e6cea9ac1cfeec0bd11218cf" +dependencies = [ + "compression-core", + "flate2", + "memchr", +] + +[[package]] +name = "compression-core" +version = "0.4.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc14f565cf027a105f7a44ccf9e5b424348421a1d8952a8fc9d499d313107789" + [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -365,6 +421,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] [[package]] @@ -811,8 +868,10 @@ dependencies = [ name = "marqdo_plugin_web" version = "0.1.0" dependencies = [ + "argon2", "axum", "futures-util", + "getrandom 0.2.17", "rusqlite", "serde", "serde_json", @@ -897,6 +956,17 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "password-hash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166" +dependencies = [ + "base64ct", + "rand_core 0.6.4", + "subtle", +] + [[package]] name = "percent-encoding" version = "2.3.2" @@ -1479,6 +1549,7 @@ version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" dependencies = [ + "async-compression", "bitflags", "bytes", "futures-core", @@ -1496,6 +1567,7 @@ dependencies = [ "tokio-util", "tower-layer", "tower-service", + "tracing", ] [[package]] diff --git a/doc/design/web-net-capabilities.md b/doc/design/web-net-capabilities.md new file mode 100644 index 0000000..c02af19 --- /dev/null +++ b/doc/design/web-net-capabilities.md @@ -0,0 +1,262 @@ +# 网络能力调研:从「能跑」到「完整项目」 + +| | | +|---|---| +| 状态 | **调研结论(Draft)** | +| 日期 | 2026-08-26 | +| 相关 | [ext-web.md](ext-web.md) · [ext-web-net.md](ext-web-net.md) · [ext-abi.md](ext-abi.md) · [stdlib-modules.md](stdlib-modules.md) | +| 目标 | 盘点 `ext/web` + `plugins/web` + `lib/net` 现状,对照主流语言网络栈,给出「开发一个完整 Web 项目」所需能力的差距清单与补强路线 | + +--- + +## 0. 一句话结论 + +**当前 `plugins/web` 已覆盖「单页渲染 + SQLite 表单 CRUD + 简易 admin 门禁 + echo WebSocket + 中间件管道(CORS/安全头/gzip/请求体限制)+ JSON API + 数据层深化(连接池/事务/分页/查询表达力)」,足以支撑中小型内容站点与前后端分离 API;但距「完整项目」(博客/CMS/社区)仍缺四类能力:③ 安全硬化(密码哈希 / CSRF / 限流 / 会话持久化);④ 内容站点标配(SEO / 搜索 / RSS / 分页导航);① 中余下上传下载 / HTTPS / 日志 / 错误页。** 全部按「基础设施进 `lib/net`,领域能力进 `plugins/web` + `ext/web`」的既有边界补强。 + + +--- + +## 1. 现状盘点(2026-08-26 实测) + +### 1.1 `lib/net`(标准库,L0.5 宿主原语薄包装) + +| 能力 | 归属 | 现状 | +|---|---|---| +| `http_get` / `获取` | host `net.rs` (ureq) | ✅ GET,返回 `{status, body}` | +| `http_post` / `提交` | host `net.rs` | ✅ POST,默认 JSON CT | +| `http_request` / `请求` | host `net.rs` | ✅ 通用 method+body+headers | +| `http_post_sse` / `提交流式` | host `net.rs` | ✅ OpenAI 兼容 SSE 流式消费 | +| `openai_sse_parse` / `解析流式` | host `net.rs` | ✅ 离线 SSE 解析 | +| `url_encode` / `编码地址` | host `net.rs` | ✅ URL 编码 | +| `cookie_parse` / `解析Cookie` | host `net.rs` | ✅ RFC6265 子集(请求头/响应头) | +| `multipart_parse` / `解析多部分` | host `net.rs` | ✅ multipart/form-data 解析 | + +**标准库网络原语已比较齐备**:客户端 GET/POST/SSE、URL 编码、cookie、multipart 都有。缺的是**响应元信息(响应头/Set-Cookie 取出)、上传(multipart 文件)与下载(二进制)**。 + +### 1.2 `plugins/web`(ABI v2,领域能力) + +**服务器侧已实现:** + +| 域 | ABI | 能力 | +|---|---|---| +| 页面 | `web_page_new` · `web_compose_components` · `web_compose_main` · `web_page_query` · `web_page_order` · `web_page_link_prefix` · `web_page_css` · `web_page_detail` | 页面装配、列表/排序、CSS、详情页 | +| 样式 | `web_style` | GFM 表 → CSS(`make_style`/`样式装配`) | +| 表单 | `web_form_new/fields/rules/validate/render/submit/from_schema` · `web_compose_form` | 字段表、校验表、渲染、提交、schema→form | +| 数据库 | `web_db_new/init/insert/select/get/update/delete/exec/table_info/list_tables` | SQLite CRUD + where + order + limit | +| 应用 | `web_app_new/route/mount_form/static/auth` · `web_app_route_ws` | 路由、静态目录、admin、鉴权、WS 端点 | +| 会话 | `web_session_new/set/get/del/destroy` | 内存 TTL 会话 | +| 鉴权 | `web_auth_login/check/logout/new` · `web_app_auth` | `/admin` 登录门禁 | +| WebSocket | `web_ws_connect`(客户端)· `web_app_route_ws`(服务器) | 单请求-响应式客户端 + echo 服务器 | + +**关键实现事实(决定差距):** + +- **中间件层已落地**(W1):`app.configure`/`应用.装配` 通过 GFM 表装配 CORS、安全响应头、gzip 压缩、请求体上限、JSON API 路由,`监听` 时统一挂到 axum `Router`(`middleware.rs`)。「配置即数据、装配即函数」。 +- **JSON API 已落地**(W1):`|路径|方法|表|条件|排序|上限|` 表声明端点,响应 `application/json`,支持 DB 查询 + 排序 + 上限。 +- **数据层深化已落地**(W2):进程级**连接池**(WAL + `busy_timeout` + `foreign_keys`)+ **事务** API(`db.事务` → `txn.insert/提交/回滚`,`txn` 句柄带 `_type` 走 Marqdo 类分发)+ **结果集裸查询**(`db.query` 返回 `{rows, count}`)+ **分页**(`select`/`paginate` 支持 `offset`,返回 `{rows, total}`)+ **查询表达力**(where 支持 OR 组 / `in` / `between` / `is null` / `like`)+ **聚合计数**(`db.count`)。 +- **无文件上传**:无 multipart extractor,`multipart_parse` 只在标准库侧做了纯解析,服务器没接收。 +- **会话为进程内内存**:重启即失效、多 worker 不共享;session id 非加密强度;密码明文存储比对。 +- **无 HTTPS**:仅 `TcpListener::bind` HTTP。 +- **无迁移机制**:schema 版本表 + 迁移脚本缺失;索引/唯一/外键声明待增强(init 表增强)。 +- **WebSocket 仅 echo**:无广播/房间/多客户端/业务分发。 +- **尚无自定义 404/500 错误页、无访问日志、无 ETag/Cache-Control 缓存头、无重定向增强。** + +--- + + +## 2. 主流网络栈对照(联网调研 2026-08) + +### 2.1 分层共识(决定「标准库 vs 扩展」边界) + +| 语言 | 标准库网络能力 | 扩展/框架能力 | 边界准则 | +|---|---|---|---| +| Python | `urllib`(HTTP 客户端)、`http.cookies`(cookie 解析器)、`http.server`(简易服务器)、`email` 多部分解析 | `requests`/`httpx`(连接池/session)、`FastAPI`/`Flask`(中间件/校验/模板) | 解析器进标准库;便利层/框架层在第三方 | +| Node.js | `node:http`(低层流式,**不解析 body**)、`node:https` | express、fastify、ws、formidable | 低层原语进核心;fancy 功能 userland 验证后再进 core | +| Go | `net/http`(服务器+客户端**都在标准库**)、`net/http/cookiejar`(RFC6265)、`net/smtp` | `golang.org/x/net`(演进性)、gin/echo | HTTP 与 stdin 同级的核心能力;cookie 解析进标准库 | +| Rust | 极小 std(无 HTTP) | `hyper`→`reqwest`→`axum` | 分层;HTTP 生态多样不进 std | + +**对本项目的三处判定(与 [ext-web-net.md](ext-web-net.md) 一致并延伸):** + +1. **纯解析原语**(cookie / multipart / url)→ `lib/net` 标准库。✅ 已达成。 +2. **HTTP 服务器结构能力**(JSON / 中间件 / 上传下载 / HTTPS / 缓存头)→ `plugins/web`(ABI)。这是 axum 本身就在依赖链里的,不需要新增核心依赖。 +3. **领域能力**(session / auth / 分页 / 搜索 / RSS / SEO / 限流)→ `plugins/web` + `ext/web` 作者面。 + +### 2.2 「完整 Web 框架」能力清单(FastAPI / Express / Flask / axum 共通项) + +| 能力 | FastAPI | Express | Flask | axum | 说明 | +|---|---|---|---|---|---| +| 路由(含动态参数) | ✅ | ✅ | ✅ | ✅ | 三框架全支持;marqdo 已有动态路由 `/post/{slug}` | +| 中间件管道 | ✅ | ✅ | ✅ | ✅(tower) | **marqdo 缺失** — 最根本缺口 | +| CORS | ✅ 内置 | ✅ 中间件 | ✅ flask-cors | ✅ tower-http | 缺失 | +| JSON 请求/响应 | ✅ 原生 | ✅ | ✅ | ✅ | 缺失 | +| 校验(schema/字段) | ✅ Pydantic | 手动(zod) | 手动 | ✅ extractor | marqdo 有 form 校验表(够用) | +| 静态文件 | ✅ Starlette | ✅ | ✅ | ✅ | ✅ 已有 | +| 模板/渲染 | ✅ Jinja | ✅ | ✅ | ✅ | marqdo 用 GFM 表装配(自研) | +| 会话 | ✅ | ✅ express-session | ✅ | ✅ | 有(内存,待持久化) | +| 鉴权 | ✅ OAuth2/APIKey | ✅ | ✅ | ✅ | 有(admin 门禁,待硬化) | +| 文件上传 | ✅ | ✅ multer | ✅ | ✅ | 缺失 | +| WebSocket | ✅ | ✅ ws | ✅ Flask-SocketIO | ✅ | 有(echo,待广播) | +| 安全头 | ✅ | ✅ helmet | ✅ | ✅ tower-http | 缺失 | +| 限流 | ✅ | ✅ | ✅ | ✅ | 缺失 | +| 日志 | ✅ | ✅ morgan | ✅ | ✅ | 缺失 | +| gzip 压缩 | ✅ | ✅ | ✅ | ✅ | 缺失 | +| 分页 | 手动 | 手动 | ✅ paginate | ✅ | marqdo 缺失(db 层无 offset) | +| 迁移 | ✅ alembic | ✅ | ✅ | ✅ sqlx | 缺失 | +| 测试工具 | ✅ TestClient | ✅ supertest | ✅ | ✅ | 缺失(依赖 gold 测试) | +| HTTPS/TLS | 反代 | 反代 | 反代 | ✅ rustls | 缺失(通常反代承担) | + +--- + + +## 3. 差距分析:按「完整项目」四类需求 + +### 3.1 类 A:结构化 HTTP 基础设施(做 API / 前后端分离 / SPA 支撑) + +| # | 缺失能力 | 影响 | 归属 | 优先级 | +|---|---|---|---|---| +| A1 | **JSON 请求/响应**(`Json` extractor、`application/json` 响应、body 大小限制) | ✅ **已实现(W1)**:`接口` 表声明 JSON API 端点;`请求体上限` 配置 body 限制 | `plugins/web` | **P0 ✅** | +| A2 | **中间件管道**(`app.configure` / `应用.装配`:CORS、安全头、压缩、请求体限制统一挂载) | ✅ **已实现(W1)**:`middleware.rs` 装配 GFM 表并 `app.layer` 挂到 axum | `plugins/web` | **P0 ✅** | +| A3 | **CORS 支持**(`Access-Control-Allow-Origin` 可配) | ✅ **已实现(W1)**:`跨域` 表 `|允许来源|方法|头|暴露头|凭证|` | `plugins/web` | P1 ✅ | +| A4 | **安全响应头**(CSP / X-Frame-Options / X-Content-Type-Options / HSTS / Referrer-Policy / 去 `X-Powered-By`) | ✅ **已实现(W1)**:`安全` 表 `|头|值|` | `plugins/web` | P1 ✅ | +| A5 | **文件上传接收**(multipart extractor → 校验类型/大小 → 落盘)与**下载**(`Content-Disposition`) | 头像/附件/图片上传是博客/CMS 刚需 | `plugins/web` | P1 | +| A6 | **HTTPS / TLS**(rustls 终止或反代提示) | 生产部署安全 | `plugins/web`(或文档指引反代) | P2 | +| A7 | **gzip / br 压缩** + **ETag / Cache-Control 缓存头** | gzip ✅ **已实现(W1,`压缩=真`)**;ETag/Cache-Control 待补 | `plugins/web`(tower-http 现成) | P2 | +| A8 | **访问日志**(请求方法/路径/状态/耗时) | 排障、审计 | `plugins/web`(tower-http TraceLayer) | P2 | +| A9 | **自定义 404/500 错误页** | 用户体感 | `plugins/web` | P2 | +| A10 | **重定向增强**(permanent/自定义状态码) | 301 SEO | `plugins/web` | P3 | + +### 3.2 类 B:数据层深化(内容站点/社区的根基) + +| # | 缺失能力 | 影响 | 归属 | 优先级 | +|---|---|---|---|---| +| B1 | **连接池 / busy_timeout / WAL** | 高并发下 `database is locked`、每请求重连开销 | `plugins/web`(db.rs) | **P0 ✅** | +| B2 | **事务 API**(`begin` / `commit` / `rollback`,或 `with_transaction` 包装) | 批量/多表写无原子性 | `plugins/web` | **P0 ✅** | +| B3 | **结果集型裸查询**(exec 返回 rows) | count / join / group / 子查询完全不可用 | `plugins/web` | P1 ✅ | +| B4 | **分页**(db 层 `limit`+`offset`;页面层上一页/下一页) | 列表页只能限量 | `plugins/web` + `ext/web` | P1 ✅(db 层;页面 UI 属 D4) | +| B5 | **查询表达力**:OR / IN / BETWEEN / IS NULL / LIKE 转义 / 括号组合 | where 只能 AND | `plugins/web` | P1 ✅ | +| B6 | **迁移机制**(schema 版本表 + 迁移脚本) | 无法演进表结构 | `plugins/web` + `ext/web` | P2 | +| B7 | **索引 / 唯一 / 外键声明**(init 表增强) | 完整性、查询性能 | `plugins/web` | P2 | +| B8 | **聚合辅助**:count / group 便捷 API | 统计面板、标签计数 | `plugins/web` | P2 ✅(`db.count`) | +| B9 | **全文搜索**(SQLite FTS5) | 站内搜索刚需 | `plugins/web`(FTS5 现成) | P2 | +| B10 | **时间戳 / 审计字段自动维护** | 博客发布日期、更新时间 | `plugins/web` | P3 | + +### 3.3 类 C:安全硬化(能上线的前提) + +| # | 缺失能力 | 影响 | 归属 | 优先级 | +|---|---|---|---|---| +| C1 | **密码哈希**(argon2/bcrypt/sha+盐,替代明文) | 任何人读到库即得全部口令 | `plugins/web`(argon2 在依赖链) | **P0** | +| C2 | **CSRF 防护**(session-bound token,写操作校验) | 表单 POST 可被跨站伪造 | `plugins/web` | **P0** | +| C3 | **登录限速/失败锁定**(每 IP/用户名 5 次/15min) | 登录接口可被爆破 | `plugins/web` | P1 | +| C4 | **会话持久化**(SQLite/文件,替代内存) | 重启全员登出、多 worker 失效 | `plugins/web` | P1 | +| C5 | **加密安全 session id**(CSPRNG + 碰撞重试) | 会话劫持 | `plugins/web`(getrandom) | P1 | +| C6 | **cookie 增强**:`Secure` 标志、签名、滑动过期 | HTTPS 下泄露、篡改、长会话断线 | `plugins/web` | P2 | +| C7 | **防暴力破解 / 会话上限 / 后台过期清理** | 内存膨胀、滥用 | `plugins/web` | P2 | +| C8 | **RBAC / 多角色**(作者/管理员/访客) | 多作者站点 | `plugins/web` + `ext/web` | P3 | + +### 3.4 类 D:内容站点标配(博客/CMS 特有) + +| # | 缺失能力 | 影响 | 归属 | 优先级 | +|---|---|---|---|---| +| D1 | **SEO**:每页 ``/`meta description`/OG 标签/`canonical`/结构化数据 | 搜索引擎收录 | `ext/web`(页面表扩展,无需插件) | **P0** | +| D2 | **sitemap.xml / robots.txt** 生成 | SEO 刚需 | `ext/web` + `web_page_*` 辅助 | P1 | +| D3 | **RSS/Atom 输出**(`content_type=application/rss+xml` 路由) | 订阅、聚合 | `ext/web` + 新的 XML 装配 | P1 | +| D4 | **分页导航 UI**(上一页/下一页,依赖 B4) | 列表页体验 | `ext/web` | P1 | +| D5 | **Markdown 渲染**(正文存储为 Markdown,渲染为 HTML) | 博客正文 | `lib/net`/标准库(纯解析,与 cookie 同模式) | **P0** | +| D6 | **标签/分类聚合页**(`/tag/{slug}` 动态路由 + 计数) | 博客结构 | `ext/web`(依赖 B8 计数) | P2 | +| D7 | **评论系统**(表单 + 审核 + 反垃圾) | 社区化 | `ext/web`(表单已具备) | P2 | +| D8 | **草稿/发布/定时**(状态字段 + 过滤查询) | 内容管理 | `ext/web` + db where | P2 | +| D9 | **图片/附件库**(依赖 A5 上传 + 相册页) | CMS 必备 | `ext/web` | P3 | + +--- + + +## 4. 补强路线(分波次,按「标准库 vs 扩展」边界) + +### 4.1 波次建议(每波独立可交付、可回归) + +| 波次 | 主题 | 内容 | 对应差距 | +|---|---|---|---| +| **W1** | 服务器基础设施 | 中间件管道(CORS/安全头/压缩/请求体限制)+ JSON API(`application/json` 响应 + DB 查询端点) | A1 A2 A3 A4 A7 ✅ **已完成** | +| **W2** | 数据层深化 | 连接池/busy_timeout/WAL + 事务 API + 结果集裸查询 + 分页(offset) + OR/IN/BETWEEN + 聚合 count | B1 B2 B3 B4 B5 B8 ✅ **已完成** | +| **W3** | 安全硬化 | 密码哈希 + CSRF + 会话持久化 + CSPRNG session id + cookie 增强 + 登录限速/锁定 | C1–C7 | +| **W4** | 内容站点标配 | SEO 元数据 + sitemap/robots + RSS + Markdown 渲染 + 分页导航 UI + 标签/分类 | D1–D6 | +| **W5** | 上传与媒体 | multipart 文件上传接收 + 落盘 + 下载 + 图片库 | A5 D9 | +| **W6** | 进阶 | 全文搜索(FTS5) + 迁移机制 + 评论系统 + 草稿/发布 + WS 广播 | B6 B9 D7 D8 + WS 增强 | + +### 4.2 边界判定要点(严格遵守既有约束) + +1. **进标准库 `lib/net`**(纯解析、无网络 I/O、支持扩展库开发): + - `markdown_parse`(GFM/Markdown → HTML,纯解析)→ D5。 + - `xml_escape` / RSS/Atom 纯文本装配辅助(如需)→ D3。 + - **不得**放进标准库:JSON API、中间件、上传接收、事务、会话(均涉 I/O 或领域语义)。 +2. **进 `plugins/web`(ABI)**:A1–A10、B1–B10、C1–C8、D7–D8 的插件侧;tower-http 已提供 CORS/压缩/安全头/Trace 等现成 layer,`axum` 提供 `Json`/`Multipart`/`Nest` 等 extractor,**均不新增核心运行时依赖**(已锁定 rustc 1.81)。 +3. **进 `ext/web` 作者面(Marqdo)**:D1–D4 中纯装配/元数据/分页 UI 部分,以及所有插件能力的 GFM 表包装(中英双面)。 +4. **已具备、无需新增**:动态路由、静态文件、表单校验/回显、session 增删查、admin 门禁、WebSocket 单连接 echo、SSE 客户端、cookie/multipart 解析。 + +### 4.3 作者面示例(示 W4 落地形态,保持「代码即文档」) + +**SEO 元数据(扩展页面表):** + +```markdown +`meta` = + +| 键 | 值 | +|---|---| +| `title` | 文章标题 | +| `description` | 摘要 | +| `og_type` | article | +| `canonical` | /post/{slug} | +``` + +页面装配时并入 `<head>` —— 仍是「表 + 函数装配」,不引入 `json.*` 袋。 + +**RSS(扩展路由,响应头 `content_type=application/rss+xml`):** + +```markdown +*feed = > 页面.列出表 rows=`文章` 分页=20 排序="published desc"* +*xml = > 文本.拼装 xs=`rss 片段们`* +*`app` = > `应用`.路由 路径="/feed.xml" 内容=`xml` 类型="application/rss+xml"* +``` + +--- + +## 5. 结论与建议顺序 + +1. **W1(中间件 + JSON)已落地**:`app.configure`/`应用.装配` 用 GFM 表装配 CORS/安全头/gzip/请求体限制/JSON API,经 gold 测试(离线 smoke + 在线 live + curl 实测)与 blog 示例回归验证。 +2. **W2(数据层)已落地**:连接池/事务/分页/查询表达力/计数经 `ext_web_db_w2_smoke` gold 测试(离线)全绿——事务提交/回滚后 `count` 正确、`IN/BETWEEN/OR` 过滤、`paginate` 返回 `{rows,total}`、裸 SQL 结果集。`database is locked` 与无法分页的实际痛点已解决。 +3. **W3 安全硬化**:上线前必须完成密码哈希 + CSRF + 会话持久化,否则「完整项目」仅能演示。 +4. **W4 内容标配**:让博客示例真正「完整」——SEO/Markdown/分页/RSS 是博客系统的门面。 +5. **W5/W6**:按项目实际需要取舍(上传、搜索、评论、迁移)。 + +**一句话:W1(中间件 + JSON)+ W2(事务 + 分页 + 查询表达力)已补齐;离「完整项目」最近的敲门砖变成「密码哈希 + CSRF」和「SEO/Markdown/RSS」两块。这两块补齐后,博客/CMS/社区类站点即可在 `ext/web` 上完整实现;其余为锦上添花。** + +--- + +## 6. 附:现有能力自测对照表(对博客示例逐项) + +| 博客功能 | 现状 | 需要的补强 | +|---|---|---| +| 文章列表(首页) | ✅ 已实现 | — | +| 文章详情(动态路由) | ✅ 已实现 | — | +| 标签/分类 | ⚠️ 可做(db where) | 计数聚合(B8 ✅ 已有 `db.count`) | +| 分页 | ✅ db 层(`paginate` 返回 `{rows,total}`) | 页面导航 UI(D4) | +| 搜索 | ❌ | B9(FTS5) | +| SEO 元数据 | ⚠️ 可手写 | D1 系统化 | +| RSS | ❌ | D3 | +| 评论 | ⚠️ 表单可做 | 审核/反垃圾 | +| 后台写文章 | ✅ admin CRUD | 草稿/发布(D8) | +| 上传配图 | ❌ | A5 + D9 | +| 用户登录 | ⚠️ admin 门禁 | 密码哈希/CSRF/限流(W3) | + + +--- + +## 7. 附:正文渲染现状(决定 D5 的必要性) + +博客详情页正文由 `plugins/web/src/render.rs` 的 `render_article_body` 渲染,但它是**「Markdown-ish」极简子集**,仅支持: + +- 段落(`\n\n` 分段) +- `# ` / `## ` 标题(统一渲染为 `<h2>`) +- ` ``` ` 围栏代码块(转义后进 `<pre>`) + +**不支持**:有序/无序列表、行内代码、粗体/斜体、链接、图片、表格、引用块、代码高亮。 +**结论**:博客正文的 Markdown 渲染是真实缺口 → D5(`markdown_parse` 纯解析原语进 `lib/net` 标准库,与 `cookie_parse`/`multipart_parse` 同模式,由 `plugins/web` 渲染层或 `ext/web` 作者面调用)优先级 **P0**。 diff --git a/doc/roadmap/ext-web.md b/doc/roadmap/ext-web.md index 04d6633..cf98e43 100644 --- a/doc/roadmap/ext-web.md +++ b/doc/roadmap/ext-web.md @@ -2,7 +2,7 @@ | | | |---|---| -| 状态 | **W0–W3 骨架已落地;W4 待** | +| 状态 | **W0–W3.5 + 安全硬化已落地;内容标配 W4 进行中** | | 日期 | 2026-08-11 | | **锁定设计** | [design/ext-web.md](../design/ext-web.md) | | 相关 | [ext-cli.md](../design/ext-cli.md) · [ext-abi.md](../design/ext-abi.md) · [ext-llm.md](../design/ext-llm.md) | @@ -25,11 +25,13 @@ | **W0** | CATALOG + L1 + `web_listen` Hello | **done** | | **W1** | env 端口;`page` 四栏;nav 表;static | **done** | | **W2** | SQLite `define`/`migrate`/`all` | **done** | -| **W3** | 三列绑定;变量名即表名;`/admin` CRUD+日志+布局 | **done**(登录鉴权仍简) | +| **W3** | 三列绑定;变量名即表名;`/admin` CRUD+日志+布局 | **done** | +| **W3s** | 安全硬化:argon2 密码哈希、CSRF、SQLite 会话持久化、CSPRNG、登录限速 | **done** | | **W3.5** | 人写面首版:`web.assemble` + `` `表`.`字段` `` + `examples/man-write-site` | **done**(编排袋仍可再瘦) | | **W4** | Postgres;Redis / S3 驱动 | pending | +| **W4c** | 内容标配:SEO / Markdown / RSS / 分页 UI(见 [web-net-capabilities.md](../design/web-net-capabilities.md)) | in progress | -验收金样:`tests/ext/web-smoke.mq.md`、`ext_cli_add_web`。 +验收金样:`tests/ext/web-smoke.mq.md`、`tests/ext/web-security-smoke.mq.md`、`ext_cli_add_web`。 --- diff --git a/ext/web/web.mq.md b/ext/web/web.mq.md index 5c59790..6a0b3d9 100644 --- a/ext/web/web.mq.md +++ b/ext/web/web.mq.md @@ -139,9 +139,116 @@ Style tables live as `##` exports in site style modules; compose resolves them b ## insert + `table` + `rows` + + `txn`=None *url = self[^url]* -**> web_db_insert url=`url` table=`table` rows=`rows`** +**> web_db_insert url=`url` table=`table` rows=`rows` txn=`txn`** + +## select + + `table` + + `where`=None + + `limit`=200 + + `order`=None + + `txn`=None + +Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作|值|` (`=` `!=` `>` `>=` `<` `<=` `like` `in` `between` `is null`; add `|或|` = `是` to join a row with `OR`). `order` is a column name with optional `-` prefix for descending (`"created_at"`, `"-created_at"`), comma-separated for multiple keys. Pass `txn` to read inside an open transaction. For pages (with a total), use `paginate`. + +*url = self[^url]* +*r = > web_db_select url=`url` table=`table` where=`where` limit=`limit` order=`order` offset=None txn=`txn`* +**r[^rows]** + +## paginate + + `table` + + `where`=None + + `limit`=200 + + `order`=None + + `跳过`=0 + + `txn`=None + +Like `select` but returns `{ rows, total }` — the total counts rows matching `where` regardless of `limit`/`跳过`, so you can render `上一页 / 下一页`. Set `跳过` to the number of rows to skip (e.g. page 2 with 10 per page ⇒ `跳过`=10). + +*url = self[^url]* +**> web_db_select url=`url` table=`table` where=`where` limit=`limit` order=`order` offset=`跳过` txn=`txn`** + +## get + + `table` + + `id` + + `txn`=None + +*url = self[^url]* +**> web_db_get url=`url` table=`table` id=`id` txn=`txn`** + +## update + + `table` + + `id` + + `row` + + `txn`=None + +*url = self[^url]* +**> web_db_update url=`url` table=`table` id=`id` row=`row` txn=`txn`** + +## delete + + `table` + + `id` + + `txn`=None + +*url = self[^url]* +**> web_db_delete url=`url` table=`table` id=`id` txn=`txn`** + +## exec + + `sql` + + `args`=None + + `txn`=None + +*url = self[^url]* +**> web_db_exec url=`url` sql=`sql` args=`args` txn=`txn`** + +## query + + `sql` + + `args`=None + + `txn`=None + +Run bare SQL and return the result set — count / join / group / subqueries. Returns `{ rows, count }`. + +*url = self[^url]* +**> web_db_query url=`url` sql=`sql` args=`args` txn=`txn`** + +## count + + `table` + + `where`=None + + `txn`=None + +Count rows matching a `where` filter (same syntax as `select`). Returns a number. + +*url = self[^url]* +*r = > web_db_count url=`url` table=`table` where=`where` txn=`txn`* +**r[^count]** + +## 事务 + +Begin a transaction: borrows the pooled connection exclusively and returns a +`txn` handle. Write inside it, then `提交` (commit) or `回滚` (roll back). +Every statement runs on the same connection, so a batch is atomic. + +*url = self[^url]* +**> web_db_begin url=`url`** + +# txn + + `txn` + + `url` + +A transaction handle from `db.事务`. All CRUD here runs on the transaction's +connection; finish with `提交` or `回滚`. + +**self** + +## insert + + `table` + + `rows` + +*url = self[^url]* +*txn = self[^txn]* +**> web_db_insert url=`url` table=`table` rows=`rows` txn=`txn`** ## select + `table` @@ -149,10 +256,11 @@ Style tables live as `##` exports in site style modules; compose resolves them b + `limit`=200 + `order`=None -Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作|值|` (`=` `!=` `>` `>=` `<` `<=` `like`). `order` is a column name with optional `-` prefix for descending (`"created_at"`, `"-created_at"`), comma-separated for multiple keys. +Same filters as `db.select`; runs inside the transaction. *url = self[^url]* -**> web_db_select url=`url` table=`table` where=`where` limit=`limit` order=`order`** +*txn = self[^txn]* +*r = > web_db_select url=`url` table=`table` where=`where` limit=`limit` order=`order` offset=None txn=`txn`* **r[^rows]** ## get @@ -160,7 +268,8 @@ Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作 + `id` *url = self[^url]* -**> web_db_get url=`url` table=`table` id=`id`** +*txn = self[^txn]* +**> web_db_get url=`url` table=`table` id=`id` txn=`txn`** ## update + `table` @@ -168,21 +277,38 @@ Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作 + `row` *url = self[^url]* -**> web_db_update url=`url` table=`table` id=`id` row=`row`** +*txn = self[^txn]* +**> web_db_update url=`url` table=`table` id=`id` row=`row` txn=`txn`** ## delete + `table` + `id` *url = self[^url]* -**> web_db_delete url=`url` table=`table` id=`id`** +*txn = self[^txn]* +**> web_db_delete url=`url` table=`table` id=`id` txn=`txn`** ## exec + `sql` + `args`=None *url = self[^url]* -**> web_db_exec url=`url` sql=`sql` args=`args`** +*txn = self[^txn]* +**> web_db_exec url=`url` sql=`sql` args=`args` txn=`txn`** + +## 提交 + +Commit the transaction and return its connection to the pool. + +*txn = self[^txn]* +**> web_db_commit txn=`txn`** + +## 回滚 + +Roll the transaction back (undo every write) and return its connection. + +*txn = self[^txn]* +**> web_db_rollback txn=`txn`** # form + `table`=None @@ -258,6 +384,19 @@ Serve files from `dir` under `mount` (default `/static`). Path is resolved from **> web_app_static app=`self` dir=`dir` mount=`mount`** +## configure + + `cors`=None + + `security`=None + + `compress`=None + + `body_limit`=None + + `json`=None + +Each capability is declared as a data table and assembled at listen time. The `cors` parameter takes a `|允许来源|方法|头|暴露头|凭证|` table (one row per origin; an empty `允许来源` column means any origin). The `security` parameter takes a `|头|值|` response-header table (e.g. `X-Frame-Options`, `Content-Security-Policy`, `X-Content-Type-Options`, `Referrer-Policy`, `Strict-Transport-Security`). Set `compress` to `True` to gzip response bodies. `body_limit` is the max request body bytes (e.g. `1048576`). The `json` parameter takes a `|路径|方法|表|条件|排序|上限|` table of JSON API endpoints backed by DB queries (each returns `application/json`). + +Tables stay as data; `configure` assembles them. 配置即数据、装配即函数. + +**> web_app_middleware app=`self` cors=`cors` security=`security` compress=`compress` body_limit=`body_limit` json_routes=`json`** + ## listen Serve `/`, routed pages, `/_part/{id}` (home) and `{path}/_part/{id}` (routes), `/_form/{id}` (from mounts + page embeds), optional `/static` (or custom mount), and optional `/admin`. @@ -313,6 +452,13 @@ Destroy the session. **> web_auth_logout session_id=`session_id`** +## hash_password + + `password` + +Hash a plaintext password for storage in admin user tables (argon2id). Store the returned `hash` in the `password` column; login verifies automatically. + +**> web_password_hash password=`password`** + # ws + `timeout_sec`=30 diff --git a/ext/web/网页.mq.md b/ext/web/网页.mq.md index 338b2eb..05b0366 100644 --- a/ext/web/网页.mq.md +++ b/ext/web/网页.mq.md @@ -126,9 +126,113 @@ import sys:lib/sys.mq.md ## 插入 + `表` + `行` + + `事务`=None *url = self[^url]* -**> web_db_insert url=`url` table=`表` rows=`行`** +**> web_db_insert url=`url` table=`表` rows=`行` txn=`事务`** + +## 查询 + + `表` + + `条件`=None + + `上限`=200 + + `排序`=None + + `事务`=None + +简单条件:单行列→值映射(AND `=`),或 `|字段|操作|值|` 表(`=` `!=` `>` `>=` `<` `<=` `like` `in` `between` `is null`;加 `|或|` 为 `是` 可让该行与前一行用 `OR` 连接)。`排序` 是列名,`-` 前缀表示倒序(`"created_at"`、`"-created_at"`),可逗号分隔多列。传入 `事务` 可在已开启的事务内读取。要分页(带总数)用 `分页`。 + +*url = self[^url]* +*r = > web_db_select url=`url` table=`表` where=`条件` limit=`上限` order=`排序` offset=None txn=`事务`* +**r[^rows]** + +## 分页 + + `表` + + `条件`=None + + `上限`=200 + + `排序`=None + + `跳过`=0 + + `事务`=None + +同 `查询`,但返回 `{ rows, total }`——`total` 是满足 `条件` 的行数(不受 `上限`/`跳过` 影响),供渲染 `上一页 / 下一页`。`跳过` 为要跳过的行数(如每页 10 条的第 2 页 ⇒ `跳过`=10)。 + +*url = self[^url]* +**> web_db_select url=`url` table=`表` where=`条件` limit=`上限` order=`排序` offset=`跳过` txn=`事务`** + +## 获取 + + `表` + + `id` + + `事务`=None + +*url = self[^url]* +**> web_db_get url=`url` table=`表` id=`id` txn=`事务`** + +## 更新 + + `表` + + `id` + + `行` + + `事务`=None + +*url = self[^url]* +**> web_db_update url=`url` table=`表` id=`id` row=`行` txn=`事务`** + +## 删除 + + `表` + + `id` + + `事务`=None + +*url = self[^url]* +**> web_db_delete url=`url` table=`表` id=`id` txn=`事务`** + +## 执行 + + `sql` + + `参数`=None + + `事务`=None + +*url = self[^url]* +**> web_db_exec url=`url` sql=`sql` args=`参数` txn=`事务`** + +## 原生查询 + + `sql` + + `参数`=None + + `事务`=None + +执行裸 SQL 并返回结果集——count / join / group / 子查询都行。返回 `{ rows, count }`。 + +*url = self[^url]* +**> web_db_query url=`url` sql=`sql` args=`参数` txn=`事务`** + +## 计数 + + `表` + + `条件`=None + + `事务`=None + +统计满足 `条件` 的行数(语法同 `查询`)。返回数字。 + +*url = self[^url]* +*r = > web_db_count url=`url` table=`表` where=`条件` txn=`事务`* +**r[^count]** + +## 事务 + +开启一个事务:从连接池独占借用连接,返回 `事务` 句柄。在事务内写入,然后 `提交` 或 `回滚`。所有语句在同一连接上执行,整批写入是原子的。 + +*url = self[^url]* +**> web_db_begin url=`url`** + +# 事务 + + `事务` + + `地址` + +数据库.事务 返回的句柄。这里的所有 CRUD 都在该事务连接上执行;结束时 提交 或 回滚。 + +**self** + +## 插入 + + `表` + + `行` + +*url = self[^地址]* +*txn = self[^事务]* +**> web_db_insert url=`url` table=`表` rows=`行` txn=`txn`** ## 查询 + `表` @@ -136,40 +240,59 @@ import sys:lib/sys.mq.md + `上限`=200 + `排序`=None -简单条件:单行列→值映射(AND `=`),或 `|字段|操作|值|` 表(`=` `!=` `>` `>=` `<` `<=` `like`)。`排序` 是列名,`-` 前缀表示倒序(`"created_at"`、`"-created_at"`),可逗号分隔多列。 +筛选语法同 `数据库.查询`;在事务内执行。 -*url = self[^url]* -*r = > web_db_select url=`url` table=`表` where=`条件` limit=`上限` order=`排序`* +*url = self[^地址]* +*txn = self[^事务]* +*r = > web_db_select url=`url` table=`表` where=`条件` limit=`上限` order=`排序` offset=None txn=`txn`* **r[^rows]** ## 获取 + `表` + `id` -*url = self[^url]* -**> web_db_get url=`url` table=`表` id=`id`** +*url = self[^地址]* +*txn = self[^事务]* +**> web_db_get url=`url` table=`表` id=`id` txn=`txn`** ## 更新 + `表` + `id` + `行` -*url = self[^url]* -**> web_db_update url=`url` table=`表` id=`id` row=`行`** +*url = self[^地址]* +*txn = self[^事务]* +**> web_db_update url=`url` table=`表` id=`id` row=`行` txn=`txn`** ## 删除 + `表` + `id` -*url = self[^url]* -**> web_db_delete url=`url` table=`表` id=`id`** +*url = self[^地址]* +*txn = self[^事务]* +**> web_db_delete url=`url` table=`表` id=`id` txn=`txn`** ## 执行 + `sql` + `参数`=None -*url = self[^url]* -**> web_db_exec url=`url` sql=`sql` args=`参数`** +*url = self[^地址]* +*txn = self[^事务]* +**> web_db_exec url=`url` sql=`sql` args=`参数` txn=`txn`** + +## 提交 + +提交事务,并把连接归还连接池。 + +*txn = self[^事务]* +**> web_db_commit txn=`txn`** + +## 回滚 + +回滚事务(撤销所有写入),并归还连接。 + +*txn = self[^事务]* +**> web_db_rollback txn=`txn`** # 表单 + `表`=None @@ -243,6 +366,19 @@ import sys:lib/sys.mq.md **> web_app_static app=`self` dir=`目录` mount=`挂载`** +## 装配 + + `跨域`=None + + `安全`=None + + `压缩`=None + + `请求体上限`=None + + `接口`=None + +每项能力用数据表声明,`监听` 时统一装配。`跨域` 参数接受 `|允许来源|方法|头|暴露头|凭证|` 表(每来源一行;`允许来源` 留空表示允许任意来源)。`安全` 参数接受 `|头|值|` 响应头表(如 `X-Frame-Options`、`Content-Security-Policy`、`X-Content-Type-Options`、`Referrer-Policy`、`Strict-Transport-Security`)。`压缩` 为 `真` 时对响应体做 gzip 压缩。`请求体上限` 是请求体最大字节数(如 `1048576`)。`接口` 参数接受 `|路径|方法|表|条件|排序|上限|` JSON API 端点表(每个返回 `application/json`)。 + +表仍是数据,`装配` 把它们变成能力。配置即数据、装配即函数。 + +**> web_app_middleware app=`self` cors=`跨域` security=`安全` compress=`压缩` body_limit=`请求体上限` json_routes=`接口`** + ## 监听 提供 `/`、路由页、首页 `/_part/{id}`、路由 `{路径}/_part/{id}`、`/_form/{id}`、可选静态目录,以及可选 `/admin`。 @@ -298,6 +434,13 @@ import sys:lib/sys.mq.md **> web_auth_logout session_id=`会话id`** +## 哈希密码 + + `密码` + +将明文口令哈希为 argon2id 字符串,写入用户表的 `密码` 列;登录时自动校验。 + +**> web_password_hash password=`密码`** + # 实时 + `超时秒`=30 diff --git a/plugins/web/Cargo.toml b/plugins/web/Cargo.toml index d1217c4..389f677 100644 --- a/plugins/web/Cargo.toml +++ b/plugins/web/Cargo.toml @@ -14,8 +14,10 @@ crate-type = ["cdylib"] serde = { version = "1", features = ["derive"] } serde_json = "1" tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "sync", "time", "signal"] } -axum = { version = "0.8", features = ["ws"] } -tower-http = { version = "0.6", features = ["fs"] } +axum = { version = "0.8", features = ["ws", "json", "query"] } +tower-http = { version = "0.6", features = ["fs", "cors", "compression-gzip", "limit", "set-header", "trace", "propagate-header"] } rusqlite = { version = "0.32", features = ["bundled"] } tokio-tungstenite = "0.24" futures-util = "0.3" +argon2 = "0.5" +getrandom = "0.2" diff --git a/plugins/web/src/db.rs b/plugins/web/src/db.rs index 1bcef22..076088e 100644 --- a/plugins/web/src/db.rs +++ b/plugins/web/src/db.rs @@ -1,10 +1,67 @@ -//! SQLite helpers for `# db` CRUD (`init` / `insert` / `select` / `get` / `update` / `delete` / `exec`). +//! SQLite helpers for `# db` CRUD and W2 data-layer capabilities. +//! +//! - **连接池**:进程级 `Pool` 复用连接(WAL + busy_timeout + foreign_keys), +//! 避免每请求重连与并发写 `database is locked`。 +//! - **事务**:`begin` 把连接从池中借出并独占,`commit` / `rollback` 后归还。 +//! - **查询表达力**:where 支持 OR 组、`in` / `between` / `is null`。 +//! - **分页**:`select` 支持 `offset`,返回 `{rows, total}`。 use rusqlite::{types::ValueRef, Connection, OptionalExtension, params_from_iter}; use serde_json::{json, Map, Value}; +use std::collections::HashMap; use std::path::PathBuf; +use std::sync::{Arc, Mutex}; -fn open(url: &str) -> Result<Connection, String> { +/// Process-wide pool: url → shared connection. Connections are reused across +/// calls instead of reopening per request. +static POOL: Mutex<Option<HashMap<String, Arc<Mutex<Connection>>>>> = Mutex::new(None); + +/// Process-wide active transactions: txn id → (url, borrowed connection). +static TXNS: Mutex<Option<HashMap<String, (String, Arc<Mutex<Connection>>)>>> = + Mutex::new(None); + +/// Reset pools (used between listens / tests). +#[allow(dead_code)] +pub fn reset_pool() { + let mut p = POOL.lock().unwrap_or_else(|e| e.into_inner()); + *p = Some(HashMap::new()); + let mut t = TXNS.lock().unwrap_or_else(|e| e.into_inner()); + *t = Some(HashMap::new()); +} + +fn next_txn_id() -> String { + use std::time::{SystemTime, UNIX_EPOCH}; + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|d| d.subsec_nanos()) + .unwrap_or(0); + let pid = std::process::id(); + static COUNTER: Mutex<u64> = Mutex::new(0x243f_6a88_85a3_08d3); + let mut c = COUNTER.lock().unwrap_or_else(|e| e.into_inner()); + let mut x = *c; + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + *c = x; + format!("txn-{pid:x}{nanos:x}{x:x}") +} + +/// Open (or reuse) a pooled connection. WAL + busy_timeout + foreign_keys are +/// applied once per physical connection. +fn pooled(url: &str) -> Result<Arc<Mutex<Connection>>, String> { + let mut guard = POOL.lock().unwrap_or_else(|e| e.into_inner()); + let pool = guard.get_or_insert_with(HashMap::new); + if let Some(c) = pool.get(url) { + return Ok(c.clone()); + } + let conn = open_conn(url)?; + let arc = Arc::new(Mutex::new(conn)); + pool.insert(url.to_string(), arc.clone()); + Ok(arc) +} + +/// Open a fresh physical connection to `url`, creating parent dirs as needed. +fn open_conn(url: &str) -> Result<Connection, String> { let path = url .strip_prefix("sqlite:") .or_else(|| url.strip_prefix("SQLITE:")) @@ -15,11 +72,32 @@ fn open(url: &str) -> Result<Connection, String> { } } let conn = Connection::open(path).map_err(|e| e.to_string())?; - conn.execute_batch("PRAGMA foreign_keys = ON;") - .map_err(|e| e.to_string())?; + conn.execute_batch( + "PRAGMA foreign_keys = ON; + PRAGMA journal_mode = WAL; + PRAGMA busy_timeout = 5000;", + ) + .map_err(|e| e.to_string())?; Ok(conn) } +/// Borrow a connection for a single statement. If `txn_id` is set, the txn's +/// borrowed connection is used (transactional reads/writes); otherwise the pool. +fn conn_for<'a>( + url: &str, + txn_id: Option<&'a str>, +) -> Result<Arc<Mutex<Connection>>, String> { + if let Some(tid) = txn_id { + let guard = TXNS.lock().unwrap_or_else(|e| e.into_inner()); + match guard.as_ref().and_then(|m| m.get(tid)) { + Some((_, c)) => Ok(c.clone()), + None => Err(format!("unknown transaction `{tid}`")), + } + } else { + pooled(url) + } +} + fn ident(s: &str) -> Result<&str, String> { let s = s.trim(); if s.is_empty() @@ -139,16 +217,18 @@ pub fn init(url: &str, table: &str, fields: &Value) -> Result<Value, String> { "CREATE TABLE IF NOT EXISTS \"{table}\" ({})", parts.join(", ") ); - let conn = open(url)?; - conn.execute_batch(&sql).map_err(|e| e.to_string())?; + let conn = pooled(url)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); + c.execute_batch(&sql).map_err(|e| e.to_string())?; Ok(json!({ "_type": "db_table", "name": table, "url": url })) } -pub fn insert(url: &str, table: &str, rows: &Value) -> Result<Value, String> { +pub fn insert(url: &str, table: &str, rows: &Value, txn_id: Option<&str>) -> Result<Value, String> { let table = ident(table)?; let rows = crate::table::as_rows(rows); let arr = rows.as_array().ok_or("rows must be a list")?; - let conn = open(url)?; + let conn = conn_for(url, txn_id)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let mut n = 0i64; for row in arr { let obj = row.as_object().ok_or("row must be a map")?; @@ -172,7 +252,7 @@ pub fn insert(url: &str, table: &str, rows: &Value) -> Result<Value, String> { cols.join(", "), placeholders.join(", ") ); - conn.execute(&sql, params_from_iter(vals.iter().cloned())) + c.execute(&sql, params_from_iter(vals.iter().cloned())) .map_err(|e| e.to_string())?; n += 1; } @@ -192,64 +272,161 @@ fn where_op(raw: &str) -> Result<&'static str, String> { "<" | "lt" | "小于" => Ok("<"), "<=" | "lte" | "小于等于" => Ok("<="), "like" | "contains" | "包含" | "匹配" => Ok("LIKE"), + "in" | "在" => Ok("IN"), + "between" | "介于" | "在之间" => Ok("BETWEEN"), + "is null" | "isnull" | "为空" | "空" => Ok("IS NULL"), other => Err(format!("unsupported where op `{other}`")), } } -/// Parse simple filters: map of column→value (AND `=`), or rows -/// `{field|字段, op|操作|=, value|值}` / `{列, 值}`. -fn parse_where(where_v: Option<&Value>) -> Result<(Vec<String>, Vec<rusqlite::types::Value>), String> { - let mut clauses = Vec::new(); - let mut vals = Vec::new(); - let Some(w) = where_v else { - return Ok((clauses, vals)); - }; - match w { - Value::Null => {} - Value::Object(m) => { - // Filter-row shape vs column→value map. - let field = m - .get("field") - .or_else(|| m.get("字段")) - .or_else(|| m.get("列")) - .map(cell_str) - .unwrap_or_default(); - if !field.is_empty() - && (m.contains_key("value") - || m.contains_key("值") - || m.contains_key("op") - || m.contains_key("操作")) - { - let col = ident(&field)?; - let op = where_op( - &m.get("op") - .or_else(|| m.get("操作")) - .map(cell_str) - .unwrap_or_default(), - )?; - let val = m - .get("value") - .or_else(|| m.get("值")) - .cloned() - .unwrap_or(Value::Null); - clauses.push(format!("\"{col}\" {op} ?")); - vals.push(to_sql(&val)); - } else { - for (k, v) in m { - if skip_where_key(k) { - continue; - } - let col = ident(k)?; - clauses.push(format!("\"{col}\" = ?")); - vals.push(to_sql(v)); - } - } +/// One parsed filter clause: column, op, bound values, and whether the clause +/// joins the previous one with `OR` (false ⇒ AND). +struct Clause { + expr: String, + vals: Vec<rusqlite::types::Value>, + or: bool, +} + +fn in_values(v: &Value) -> Vec<rusqlite::types::Value> { + match v { + Value::Array(a) => a.iter().map(to_sql).collect(), + Value::String(s) => s + .split(',') + .map(|p| to_sql(&Value::String(p.trim().to_string()))) + .collect(), + other => vec![to_sql(other)], + } +} + +fn between_values(v: &Value) -> Result<Vec<rusqlite::types::Value>, String> { + match v { + Value::Array(a) if a.len() == 2 => Ok(vec![to_sql(&a[0]), to_sql(&a[1])]), + Value::String(s) => { + let mut it = s.split(','); + let lo = it.next().unwrap_or("").trim().to_string(); + let hi = it.next().unwrap_or("").trim().to_string(); + Ok(vec![ + to_sql(&Value::String(lo)), + to_sql(&Value::String(hi)), + ]) } + _ => Err("between expects two bounds (\"lo,hi\" or [lo,hi])".into()), + } +} + +/// Parse a filter table row into a `Clause`. +fn clause_from_row(m: &Map<String, Value>) -> Result<Option<Clause>, String> { + let field = m + .get("field") + .or_else(|| m.get("字段")) + .or_else(|| m.get("列")) + .map(cell_str) + .unwrap_or_default(); + if field.is_empty() { + return Ok(None); + } + let col = ident(&field)?; + let op = where_op( + &m.get("op") + .or_else(|| m.get("操作")) + .map(cell_str) + .unwrap_or_default(), + )?; + let val = m + .get("value") + .or_else(|| m.get("值")) + .cloned() + .unwrap_or(Value::Null); + let or = m + .get("or") + .or_else(|| m.get("或")) + .and_then(|v| match v { + Value::Bool(b) => Some(*b), + Value::String(s) => Some(matches!(s.trim().to_ascii_lowercase().as_str(), "1" | "true" | "是" | "or" | "或者")), + _ => None, + }) + .unwrap_or(false); + + let (expr, vals) = match op { + "IS NULL" => (format!("\"{col}\" IS NULL"), vec![]), + "IN" => { + let vs = in_values(&val); + let ph: Vec<&str> = vs.iter().map(|_| "?").collect(); + ( + format!("\"{col}\" IN ({})", ph.join(", ")), + vs, + ) + } + "BETWEEN" => { + let vs = between_values(&val)?; + (format!("\"{col}\" BETWEEN ? AND ?"), vs) + } + _ => (format!("\"{col}\" {op} ?"), vec![to_sql(&val)]), + }; + Ok(Some(Clause { expr, vals, or })) +} + +/// Parse simple filters: map of column→value (AND `=`), or rows +/// `{field|字段, op|操作|=, value|值, or|或?}` / `{列, 值}`. +/// Supports `in` / `between` / `is null` and OR groups via the `或` column. +/// +/// GFM tables reach the plugin in columnar form (`{字段:[…], 值:[…]}`), so the +/// value is first normalized via `as_rows` into a row list. +fn parse_where(where_v: Option<&Value>) -> Result<(Vec<String>, Vec<rusqlite::types::Value>), String> { + let mut clauses: Vec<Clause> = Vec::new(); + let Some(w) = where_v else { + return Ok((vec![], vec![])); + }; + // Normalize GFM tables (columnar object) into row arrays; a single map + // (column→value filter) becomes a one-element row list. + let w = crate::table::as_rows(w); + match &w { + Value::Null => {} Value::Array(arr) => { for item in arr { - let (c, v) = parse_where(Some(item))?; - clauses.extend(c); - vals.extend(v); + match item { + Value::Object(m) => { + let field = m + .get("field") + .or_else(|| m.get("字段")) + .or_else(|| m.get("列")) + .map(cell_str) + .unwrap_or_default(); + if !field.is_empty() + && (m.contains_key("value") + || m.contains_key("值") + || m.contains_key("op") + || m.contains_key("操作")) + { + if let Some(cl) = clause_from_row(m)? { + clauses.push(cl); + } + } else { + for (k, v) in m { + if skip_where_key(k) { + continue; + } + let col = ident(k)?; + clauses.push(Clause { + expr: format!("\"{col}\" = ?"), + vals: vec![to_sql(v)], + or: false, + }); + } + } + } + other => { + return Err(format!( + "where must be a map or filter table, got {}", + match other { + Value::String(_) => "string", + Value::Number(_) => "number", + Value::Bool(_) => "bool", + _ => "value", + } + )); + } + } } } other => { @@ -264,7 +441,26 @@ fn parse_where(where_v: Option<&Value>) -> Result<(Vec<String>, Vec<rusqlite::ty )); } } - Ok((clauses, vals)) + // Assemble: AND by default, OR where the clause is marked. + let mut exprs = Vec::new(); + let mut vals = Vec::new(); + let mut group: Vec<String> = Vec::new(); + for cl in clauses { + if cl.or { + group.push(cl.expr); + } else { + if !group.is_empty() { + exprs.push(format!("({})", group.join(" OR "))); + group.clear(); + } + group.push(cl.expr); + } + vals.extend(cl.vals); + } + if !group.is_empty() { + exprs.push(format!("({})", group.join(" OR "))); + } + Ok((exprs, vals)) } pub fn select( @@ -273,28 +469,37 @@ pub fn select( limit: i64, where_v: Option<&Value>, ) -> Result<Value, String> { - select_order(url, table, limit, where_v, None) + select_order(url, table, limit, where_v, None, None, None) } -/// Like `select`, with an optional `ORDER BY`. +/// Like `select`, with an optional `ORDER BY`, `OFFSET` and a transaction. /// -/// `order` is a column name, optionally prefixed with `-` for descending -/// (`"created_at"`, `"-created_at"`). Comma-separated lists are allowed. +/// - `order` is a column name, optionally prefixed with `-` for descending +/// (`"created_at"`, `"-created_at"`). Comma-separated lists are allowed. +/// - `offset` (页偏移/跳过) enables pagination: `LIMIT … OFFSET …`. +/// - When `offset` is provided, the result carries `total` (row count without +/// limit/offset) so pages can render `上一页 / 下一页`. +/// - `txn_id` routes the read through an open transaction (事务内一致性读). pub fn select_order( url: &str, table: &str, limit: i64, where_v: Option<&Value>, order: Option<&str>, + offset: Option<i64>, + txn_id: Option<&str>, ) -> Result<Value, String> { let table = ident(table)?; - let (clauses, mut vals) = parse_where(where_v)?; - let conn = open(url)?; + let (exprs, mut vals) = parse_where(where_v)?; + let conn = conn_for(url, txn_id)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let mut sql = format!("SELECT * FROM \"{table}\""); - if !clauses.is_empty() { - sql.push_str(" WHERE "); - sql.push_str(&clauses.join(" AND ")); - } + let where_sql = if exprs.is_empty() { + String::new() + } else { + format!(" WHERE {}", exprs.join(" AND ")) + }; + sql.push_str(&where_sql); if let Some(order) = order { let order = order.trim(); if !order.is_empty() { @@ -303,30 +508,61 @@ pub fn select_order( .map(|p| p.trim()) .filter(|p| !p.is_empty()) .collect(); - let mut exprs = Vec::new(); + let mut order_exprs = Vec::new(); for part in parts { let (col, dir) = match part.strip_prefix('-') { Some(col) => (col, " DESC"), None => (part, ""), }; - exprs.push(format!("\"{}\"{}", ident(col)?, dir)); + order_exprs.push(format!("\"{}\"{}", ident(col)?, dir)); } - if !exprs.is_empty() { + if !order_exprs.is_empty() { sql.push_str(" ORDER BY "); - sql.push_str(&exprs.join(", ")); + sql.push_str(&order_exprs.join(", ")); } } } sql.push_str(&format!(" LIMIT ?{}", vals.len() + 1)); vals.push(rusqlite::types::Value::Integer(limit)); - let mut stmt = conn.prepare(&sql).map_err(|e| e.to_string())?; + if let Some(off) = offset { + if off > 0 { + sql.push_str(&format!(" OFFSET ?{}", vals.len() + 1)); + vals.push(rusqlite::types::Value::Integer(off)); + } + } + let rows = query_rows(&c, &sql, &vals)?; + + // Total count (ignoring limit/offset) for pagination. + let total = if offset.is_some() { + let (_exprs2, where_vals) = parse_where(where_v)?; + let n_where = where_vals.len(); + let count_sql = format!("SELECT COUNT(*) FROM \"{table}\"{where_sql}"); + count_rows(&c, &count_sql, &vals[..n_where]).unwrap_or(rows.len() as i64) + } else { + rows.len() as i64 + }; + + if offset.is_some() { + Ok(json!({ "rows": rows, "total": total })) + } else { + Ok(json!({ "rows": rows })) + } +} + +/// Run `SELECT …` and collect rows as a list of column→value maps. +fn query_rows( + c: &Connection, + sql: &str, + vals: &[rusqlite::types::Value], +) -> Result<Vec<Value>, String> { + let mut stmt = c.prepare(sql).map_err(|e| e.to_string())?; let names: Vec<String> = (0..stmt.column_count()) .map(|i| stmt.column_name(i).unwrap_or("?").to_string()) .collect(); - let mut rows = Vec::new(); let mut rows_iter = stmt .query(params_from_iter(vals.iter().cloned())) .map_err(|e| e.to_string())?; + let mut rows = Vec::new(); while let Some(r) = rows_iter.next().map_err(|e| e.to_string())? { let mut m = Map::new(); for (i, name) in names.iter().enumerate() { @@ -334,14 +570,25 @@ pub fn select_order( } rows.push(Value::Object(m)); } - Ok(json!({ "rows": rows })) + Ok(rows) } -pub fn get(url: &str, table: &str, id: &str) -> Result<Value, String> { +/// Run `SELECT COUNT(*) …` and return the scalar. +fn count_rows( + c: &Connection, + sql: &str, + vals: &[rusqlite::types::Value], +) -> Result<i64, String> { + c.query_row(sql, params_from_iter(vals.iter().cloned()), |r| r.get(0)) + .map_err(|e| e.to_string()) +} + +pub fn get(url: &str, table: &str, id: &str, txn_id: Option<&str>) -> Result<Value, String> { let table = ident(table)?; - let conn = open(url)?; + let conn = conn_for(url, txn_id)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let sql = format!("SELECT * FROM \"{table}\" WHERE \"id\" = ?1 LIMIT 1"); - let mut stmt = conn.prepare(&sql).map_err(|e| e.to_string())?; + let mut stmt = c.prepare(&sql).map_err(|e| e.to_string())?; let names: Vec<String> = (0..stmt.column_count()) .map(|i| stmt.column_name(i).unwrap_or("?").to_string()) .collect(); @@ -358,7 +605,13 @@ pub fn get(url: &str, table: &str, id: &str) -> Result<Value, String> { Ok(row.unwrap_or(Value::Null)) } -pub fn update(url: &str, table: &str, id: &str, row: &Value) -> Result<Value, String> { +pub fn update( + url: &str, + table: &str, + id: &str, + row: &Value, + txn_id: Option<&str>, +) -> Result<Value, String> { let table = ident(table)?; let obj = row.as_object().ok_or("row must be a map")?; let mut sets = Vec::new(); @@ -379,38 +632,112 @@ pub fn update(url: &str, table: &str, id: &str, row: &Value) -> Result<Value, St "UPDATE \"{table}\" SET {} WHERE \"id\" = ?", sets.join(", ") ); - let conn = open(url)?; - let n = conn + let conn = conn_for(url, txn_id)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); + let n = c .execute(&sql, params_from_iter(vals.iter().cloned())) .map_err(|e| e.to_string())?; Ok(json!({ "ok": true, "updated": n })) } -pub fn delete(url: &str, table: &str, id: &str) -> Result<Value, String> { +pub fn delete(url: &str, table: &str, id: &str, txn_id: Option<&str>) -> Result<Value, String> { let table = ident(table)?; - let conn = open(url)?; + let conn = conn_for(url, txn_id)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let sql = format!("DELETE FROM \"{table}\" WHERE \"id\" = ?1"); - let n = conn + let n = c .execute(&sql, rusqlite::params![id]) .map_err(|e| e.to_string())?; Ok(json!({ "ok": true, "deleted": n })) } -pub fn exec(url: &str, sql: &str, args: Option<&Value>) -> Result<Value, String> { - let conn = open(url)?; +pub fn exec(url: &str, sql: &str, args: Option<&Value>, txn_id: Option<&str>) -> Result<Value, String> { + let conn = conn_for(url, txn_id)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let vals: Vec<rusqlite::types::Value> = match args { Some(Value::Array(a)) => a.iter().map(to_sql).collect(), _ => Vec::new(), }; - let n = conn + let n = c .execute(sql, params_from_iter(vals.iter().cloned())) .map_err(|e| e.to_string())?; Ok(json!({ "ok": true, "changes": n })) } +/// Run a `SELECT …` and return the result set (count / join / group / subquery). +/// Bare SQL — the result carries `{ rows, count }`. +pub fn query(url: &str, sql: &str, args: Option<&Value>, txn_id: Option<&str>) -> Result<Value, String> { + let conn = conn_for(url, txn_id)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); + let vals: Vec<rusqlite::types::Value> = match args { + Some(Value::Array(a)) => a.iter().map(to_sql).collect(), + _ => Vec::new(), + }; + let rows = query_rows(&c, sql, &vals)?; + Ok(json!({ "rows": rows, "count": rows.len() })) +} + +/// Count rows matching a `where` filter (aggregation helper). +pub fn count(url: &str, table: &str, where_v: Option<&Value>, txn_id: Option<&str>) -> Result<Value, String> { + let table = ident(table)?; + let (exprs, vals) = parse_where(where_v)?; + let conn = conn_for(url, txn_id)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); + let mut sql = format!("SELECT COUNT(*) FROM \"{table}\""); + if !exprs.is_empty() { + sql.push_str(&format!(" WHERE {}", exprs.join(" AND "))); + } + let n = count_rows(&c, &sql, &vals)?; + Ok(json!({ "count": n })) +} + +/// Begin a transaction. Borrows the pooled connection exclusively and records +/// it under a fresh txn id; returns `{ txn, url }` for `commit` / `rollback`. +pub fn begin(url: &str) -> Result<Value, String> { + let conn = pooled(url)?; + { + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); + c.execute_batch("BEGIN IMMEDIATE").map_err(|e| e.to_string())?; + } + let id = next_txn_id(); + { + let mut guard = TXNS.lock().unwrap_or_else(|e| e.into_inner()); + guard + .get_or_insert_with(HashMap::new) + .insert(id.clone(), (url.to_string(), conn)); + } + Ok(json!({ "_type": "txn", "txn": id, "url": url, "事务": id, "地址": url })) +} + +/// Commit a transaction and return its connection to the pool. +pub fn commit(txn_id: &str) -> Result<Value, String> { + let (_, conn) = take_txn(txn_id)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); + c.execute_batch("COMMIT").map_err(|e| e.to_string())?; + Ok(json!({ "ok": true })) +} + +/// Roll back a transaction and return its connection to the pool. +pub fn rollback(txn_id: &str) -> Result<Value, String> { + let (_, conn) = take_txn(txn_id)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); + c.execute_batch("ROLLBACK").map_err(|e| e.to_string())?; + Ok(json!({ "ok": true })) +} + +/// Remove a txn from the registry, returning its connection for reuse. +fn take_txn(txn_id: &str) -> Result<(String, Arc<Mutex<Connection>>), String> { + let mut guard = TXNS.lock().unwrap_or_else(|e| e.into_inner()); + guard + .as_mut() + .and_then(|m| m.remove(txn_id)) + .ok_or_else(|| format!("unknown transaction `{txn_id}`")) +} + pub fn list_tables(url: &str) -> Result<Vec<String>, String> { - let conn = open(url)?; - let mut stmt = conn + let conn = pooled(url)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); + let mut stmt = c .prepare( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_marqdo%' ORDER BY name", ) @@ -434,9 +761,10 @@ pub struct ColumnInfo { /// `PRAGMA table_info` → column metadata for admin forms. pub fn table_info(url: &str, table: &str) -> Result<Vec<ColumnInfo>, String> { let table = ident(table)?; - let conn = open(url)?; + let conn = pooled(url)?; + let c = conn.lock().unwrap_or_else(|e| e.into_inner()); let sql = format!("PRAGMA table_info(\"{table}\")"); - let mut stmt = conn.prepare(&sql).map_err(|e| e.to_string())?; + let mut stmt = c.prepare(&sql).map_err(|e| e.to_string())?; let rows = stmt .query_map([], |r| { Ok(ColumnInfo { diff --git a/plugins/web/src/form.rs b/plugins/web/src/form.rs index 6975580..b94adca 100644 --- a/plugins/web/src/form.rs +++ b/plugins/web/src/form.rs @@ -429,6 +429,7 @@ pub fn render_body( form_id: &str, data: Option<&Value>, errors: Option<&Value>, + csrf: Option<&str>, ) -> String { let fields = form .get("fields") @@ -490,6 +491,12 @@ pub fn render_body( "<form method=\"post\" action=\"{}\">", esc(&post_to) )); + if let Some(token) = csrf.filter(|s| !s.is_empty()) { + body.push_str(&format!( + "<input type=\"hidden\" name=\"_csrf\" value=\"{}\"/>", + esc(token) + )); + } if action == "update" && !row_id.is_empty() { body.push_str(&format!( "<input type=\"hidden\" name=\"id\" value=\"{}\"/>", @@ -569,7 +576,13 @@ pub fn render_body( } /// Standalone form document (`GET|POST /_form/{id}`). -pub fn render(form: &Value, form_id: &str, data: Option<&Value>, errors: Option<&Value>) -> String { +pub fn render( + form: &Value, + form_id: &str, + data: Option<&Value>, + errors: Option<&Value>, + csrf: Option<&str>, +) -> String { let heading = form .get("title") .and_then(|v| v.as_str()) @@ -593,7 +606,7 @@ body{font-family:"IBM Plex Sans","Noto Sans SC",sans-serif;margin:1.5rem;backgro </style></head><body>"#, ); body.push_str(&format!("<h1>{}</h1>", esc(heading))); - body.push_str(&render_body(form, form_id, data, errors)); + body.push_str(&render_body(form, form_id, data, errors, csrf)); body.push_str("</body></html>"); body } @@ -631,9 +644,9 @@ pub fn submit(form: &Value, data: &Value, db_url: &str) -> Result<Value, String> .filter(|s| !s.is_empty()) .or_else(|| data_map(&data).get("id").map(cell_str)) .ok_or_else(|| "update requires `id`".to_string())?; - db::update(db_url, table, &id, &row)? + db::update(db_url, table, &id, &row, None)? } else { - db::insert(db_url, table, &json!([row]))? + db::insert(db_url, table, &json!([row]), None)? }; let redirect = form .get("redirect") diff --git a/plugins/web/src/http.rs b/plugins/web/src/http.rs index fe04cf5..7998349 100644 --- a/plugins/web/src/http.rs +++ b/plugins/web/src/http.rs @@ -16,13 +16,15 @@ use tower_http::services::ServeDir; use crate::db; use crate::form; +use crate::middleware::Middleware; +use crate::rate_limit; use crate::render; use crate::session; #[derive(Clone)] -struct AppState { +pub struct AppState { page: Value, - db_url: Option<String>, + pub db_url: Option<String>, admin: bool, forms: HashMap<String, Value>, /// Page that owns an embedded form (for re-render on validation errors). @@ -30,6 +32,7 @@ struct AppState { /// Admin users table (`|用户名|密码|`) when login-gated admin is configured. auth_users: Option<Value>, session_ttl: u64, + cookie_secure: bool, } fn collect_page_forms( @@ -66,14 +69,22 @@ pub fn listen( static_mount: &str, auth_users: Option<Value>, session_ttl: u64, + cookie_secure: bool, ws_routes: HashMap<String, bool>, + middleware: &Middleware, ) -> Result<Value, String> { let mut form_owners = HashMap::new(); collect_page_forms(page, &mut forms, &mut form_owners); for p in routes.values() { collect_page_forms(p, &mut forms, &mut form_owners); } + session::configure(session::Config { + db_url: db_url.map(|s| s.to_string()), + ttl_sec: session_ttl, + cookie_secure, + }); session::reset(session_ttl); + rate_limit::reset(); let state = Arc::new(AppState { page: page.clone(), db_url: db_url.map(|s| s.to_string()), @@ -82,6 +93,7 @@ pub fn listen( form_owners, auth_users, session_ttl, + cookie_secure, }); let mut app = Router::new() @@ -111,10 +123,13 @@ pub fn listen( let page_for_render = page.clone(); app = app.route( &path, - get(async move |State(st): State<Arc<AppState>>, Path(params): Path<HashMap<String, String>>| { + get(async move |State(st): State<Arc<AppState>>, headers: axum::http::HeaderMap, Path(params): Path<HashMap<String, String>>| { let mut p = page_for_render.clone(); inject_params(&mut p, ¶ms); - Html(render::render_page(&p, st.db_url.as_deref())).into_response() + let (_, csrf, set_cookie) = resolve_session(&headers); + let mut resp = Html(render::render_page(&p, st.db_url.as_deref(), Some(&csrf))).into_response(); + append_set_cookie(&mut resp, set_cookie); + resp }), ); // Dynamic `{path}/_part/{id}` with the same path params. @@ -132,9 +147,12 @@ pub fn listen( let page_for_render = page.clone(); app = app.route( &path, - get(async move |State(st): State<Arc<AppState>>| { - Html(render::render_page(&page_for_render, st.db_url.as_deref())) - .into_response() + get(async move |State(st): State<Arc<AppState>>, headers: axum::http::HeaderMap| { + let (_, csrf, set_cookie) = resolve_session(&headers); + let mut resp = Html(render::render_page(&page_for_render, st.db_url.as_deref(), Some(&csrf))) + .into_response(); + append_set_cookie(&mut resp, set_cookie); + resp }), ); let part_path = format!("{path}/_part/{{id}}"); @@ -178,6 +196,7 @@ pub fn listen( app = app.nest_service(&mount, svc); } + let app = crate::middleware::apply(app, middleware); let app = app.with_state(state); let addr: SocketAddr = format!("{host}:{port}") @@ -213,9 +232,12 @@ pub fn normalize_static_mount(raw: &str) -> String { m } -async fn home(State(st): State<Arc<AppState>>) -> Html<String> { - let html = render::render_page(&st.page, st.db_url.as_deref()); - Html(html) +async fn home(State(st): State<Arc<AppState>>, headers: axum::http::HeaderMap) -> Response { + let (_, csrf, set_cookie) = resolve_session(&headers); + let html = render::render_page(&st.page, st.db_url.as_deref(), Some(&csrf)); + let mut resp = Html(html).into_response(); + append_set_cookie(&mut resp, set_cookie); + resp } /// Inject captured dynamic-route params into a page copy as `params` (a map), @@ -256,15 +278,23 @@ async fn home_part(State(st): State<Arc<AppState>>, Path(id): Path<String>) -> R render_part_from_page(&st.page, &id, st.db_url.as_deref()) } -async fn form_get(State(st): State<Arc<AppState>>, Path(id): Path<String>) -> Response { +async fn form_get( + State(st): State<Arc<AppState>>, + headers: axum::http::HeaderMap, + Path(id): Path<String>, +) -> Response { let Some(frm) = st.forms.get(&id) else { return Html(format!("<p>unknown form {id}</p>")).into_response(); }; - Html(form::render(frm, &id, None, None)).into_response() + let (_, csrf, set_cookie) = resolve_session(&headers); + let mut resp = Html(form::render(frm, &id, None, None, Some(&csrf))).into_response(); + append_set_cookie(&mut resp, set_cookie); + resp } async fn form_post( State(st): State<Arc<AppState>>, + headers: axum::http::HeaderMap, Path(id): Path<String>, Form(posted): Form<HashMap<String, String>>, ) -> Response { @@ -274,7 +304,13 @@ async fn form_post( let Some(url) = st.db_url.as_deref() else { return Html(String::from("<p>no database</p>")).into_response(); }; - submit_and_respond(&st, frm, &id, url, posted, None) + let (_, csrf, set_cookie, posted) = match validate_csrf_post(&headers, posted) { + Ok(v) => v, + Err(r) => return r, + }; + let mut resp = submit_and_respond(&st, frm, &id, url, posted, None, Some(&csrf)); + append_set_cookie(&mut resp, set_cookie); + resp } fn posted_to_value(posted: HashMap<String, String>) -> Value { @@ -300,6 +336,7 @@ fn submit_and_respond( url: &str, posted: HashMap<String, String>, admin_table: Option<&str>, + csrf: Option<&str>, ) -> Response { let data_v = posted_to_value(posted); match form::submit(frm, &data_v, url) { @@ -331,6 +368,7 @@ fn submit_and_respond( form_id, Some(&data_v), errors, + csrf, )) .into_response() } else if let Some(page) = st.form_owners.get(form_id) { @@ -339,10 +377,11 @@ fn submit_and_respond( st.db_url.as_deref(), Some(&data_v), errors, + csrf, )) .into_response() } else { - Html(form::render(frm, form_id, Some(&data_v), errors)).into_response() + Html(form::render(frm, form_id, Some(&data_v), errors, csrf)).into_response() } } Err(e) => Html(format!("<p>submit error: {}</p>", esc(&e))).into_response(), @@ -355,6 +394,50 @@ fn cookie_from_headers(headers: &axum::http::HeaderMap) -> Option<&str> { .and_then(|v| v.to_str().ok()) } +fn client_ip(headers: &axum::http::HeaderMap) -> String { + headers + .get("x-forwarded-for") + .and_then(|v| v.to_str().ok()) + .and_then(|s| s.split(',').next()) + .map(|s| s.trim().to_string()) + .filter(|s| !s.is_empty()) + .unwrap_or_else(|| "127.0.0.1".into()) +} + +fn resolve_session(headers: &axum::http::HeaderMap) -> (String, String, Option<String>) { + let cookie = cookie_from_headers(headers); + let (id, set_cookie) = session::ensure_from_cookie(cookie); + let csrf = session::csrf_for(&id).unwrap_or_default(); + (id, csrf, set_cookie) +} + +fn append_set_cookie(resp: &mut Response, set_cookie: Option<String>) { + if let Some(c) = set_cookie { + let _ = resp.headers_mut().try_append( + axum::http::header::SET_COOKIE, + c.parse().unwrap(), + ); + } +} + +fn validate_csrf_post( + headers: &axum::http::HeaderMap, + posted: HashMap<String, String>, +) -> Result<(String, String, Option<String>, HashMap<String, String>), Response> { + let (sid, csrf, set_cookie) = resolve_session(headers); + let token = posted.get("_csrf").map(|s| s.as_str()).unwrap_or(""); + if !session::validate_csrf(&sid, token) { + return Err(Html( + "<p class=\"flash err\">Invalid or missing CSRF token. Refresh and try again.</p>" + .to_string(), + ) + .into_response()); + } + let mut posted = posted; + posted.remove("_csrf"); + Ok((sid, csrf, set_cookie, posted)) +} + fn admin_gate(st: &AppState, cookie_header: Option<&str>) -> Option<Response> { if !st.admin { return Some(Html(admin_shell(st, "Admin", None, "<p class=\"flash err\">Admin is disabled for this app.</p>")).into_response()); @@ -380,51 +463,70 @@ fn admin_authed(_st: &AppState, cookie_header: Option<&str>) -> bool { session::session_get(&sid, "username").is_some() } -#[derive(Deserialize)] -struct LoginForm { - username: Option<String>, - password: Option<String>, -} - -async fn admin_login_get(State(st): State<Arc<AppState>>) -> Response { - // Already logged in → straight to admin home. +async fn admin_login_get(State(st): State<Arc<AppState>>, headers: axum::http::HeaderMap) -> Response { if st.auth_users.is_none() { return Redirect::to("/admin").into_response(); } - Html(login_page(&st, None)).into_response() + if admin_authed(&st, cookie_from_headers(&headers)) { + return Redirect::to("/admin").into_response(); + } + let (_, csrf, set_cookie) = resolve_session(&headers); + let mut resp = Html(login_page(&st, None, Some(&csrf))).into_response(); + append_set_cookie(&mut resp, set_cookie); + resp } async fn admin_login_post( State(st): State<Arc<AppState>>, - Form(posted): Form<LoginForm>, + headers: axum::http::HeaderMap, + Form(posted): Form<HashMap<String, String>>, ) -> Response { let Some(users) = &st.auth_users else { return Redirect::to("/admin").into_response(); }; - let username = posted.username.unwrap_or_default(); - let password = posted.password.unwrap_or_default(); - if session::check_credentials(users, &username, &password).is_none() { - return Html(login_page(&st, Some("Invalid username or password."))).into_response(); + let (_, csrf, set_cookie, posted) = match validate_csrf_post(&headers, posted) { + Ok(v) => v, + Err(r) => return r, + }; + let username = posted.get("username").cloned().unwrap_or_default(); + let password = posted.get("password").cloned().unwrap_or_default(); + let ip = client_ip(&headers); + if let Err(msg) = rate_limit::check(&ip, &username) { + let mut resp = Html(login_page(&st, Some(&msg), Some(&csrf))).into_response(); + append_set_cookie(&mut resp, set_cookie); + return resp; } + if session::check_credentials(users, &username, &password).is_none() { + rate_limit::record_failure(&ip, &username); + let mut resp = + Html(login_page(&st, Some("Invalid username or password."), Some(&csrf))) + .into_response(); + append_set_cookie(&mut resp, set_cookie); + return resp; + } + rate_limit::clear_success(&ip, &username); let sid = session::session_new(Some(st.session_ttl)); session::session_set(&sid, "username", json!(username)); let mut resp = Redirect::to("/admin").into_response(); - if let Ok(h) = resp.headers_mut().try_append( + let _ = resp.headers_mut().try_append( axum::http::header::SET_COOKIE, - session::session_cookie(&sid, st.session_ttl) + session::session_cookie(&sid, st.session_ttl, st.cookie_secure) .parse() .unwrap(), - ) { - let _ = h; - } + ); + append_set_cookie(&mut resp, set_cookie); resp } -fn login_page(_st: &AppState, error: Option<&str>) -> String { +fn login_page(_st: &AppState, error: Option<&str>, csrf: Option<&str>) -> String { let err = match error { Some(e) => format!("<p class=\"flash err\">{}</p>", esc(e)), None => String::new(), }; + let csrf_field = csrf + .filter(|s| !s.is_empty()) + .map(|t| format!("<input type=\"hidden\" name=\"_csrf\" value=\"{}\"/>", esc(t))) + .unwrap_or_default(); format!( r#"<!DOCTYPE html> <html lang="zh-CN"><head> @@ -452,6 +554,7 @@ body {{ margin:0; min-height:100vh; display:grid; place-items:center; background <p class="sub">Sign in to manage this site.</p> {err} <form method="post" action="/admin/login"> +{csrf_field} <label>Username<input name="username" autocomplete="username" required autofocus/></label> <label>Password<input name="password" type="password" autocomplete="current-password" required/></label> <button type="submit">Sign in</button> @@ -459,6 +562,7 @@ body {{ margin:0; min-height:100vh; display:grid; place-items:center; background </div> </body></html>"#, err = err, + csrf_field = csrf_field, ) } @@ -616,13 +720,14 @@ fn admin_form_page( form_id: &str, data: Option<&Value>, errors: Option<&Value>, + csrf: Option<&str>, ) -> String { let mut body = format!( "<p class=\"crumbs\"><a href=\"/admin\">Admin</a> / <a href=\"/admin/{t}\">{t}</a> / {kind}</p><h1>{kind} {t}</h1>", t = esc(table), kind = esc(kind), ); - body.push_str(&form::render_body(frm, form_id, data, errors)); + body.push_str(&form::render_body(frm, form_id, data, errors, csrf)); admin_shell(st, &format!("{kind} {table}"), Some(table), &body) } @@ -767,17 +872,23 @@ async fn admin_new_get( return r; } let url = st.db_url.as_deref().unwrap(); + let (_, csrf, set_cookie) = resolve_session(&headers); match form::from_schema(url, &table, "insert", None) { - Ok(frm) => Html(admin_form_page( - &st, - &table, - "New", - &frm, - &format!("admin-{table}-new"), - None, - None, - )) - .into_response(), + Ok(frm) => { + let mut resp = Html(admin_form_page( + &st, + &table, + "New", + &frm, + &format!("admin-{table}-new"), + None, + None, + Some(&csrf), + )) + .into_response(); + append_set_cookie(&mut resp, set_cookie); + resp + } Err(e) => Html(admin_shell( &st, &table, @@ -798,15 +909,24 @@ async fn admin_new_post( return r; } let url = st.db_url.as_deref().unwrap(); + let (_, csrf, set_cookie, posted) = match validate_csrf_post(&headers, posted) { + Ok(v) => v, + Err(r) => return r, + }; match form::from_schema(url, &table, "insert", None) { - Ok(frm) => submit_and_respond( - &st, - &frm, - &format!("admin-{table}-new"), - url, - posted, - Some(&table), - ), + Ok(frm) => { + let mut resp = submit_and_respond( + &st, + &frm, + &format!("admin-{table}-new"), + url, + posted, + Some(&table), + Some(&csrf), + ); + append_set_cookie(&mut resp, set_cookie); + resp + } Err(e) => Html(admin_shell( &st, &table, @@ -826,7 +946,7 @@ async fn admin_edit_get( return r; } let url = st.db_url.as_deref().unwrap(); - let row = match db::get(url, &table, &id) { + let row = match db::get(url, &table, &id, None) { Ok(Value::Null) => { return Html(admin_shell( &st, @@ -847,17 +967,23 @@ async fn admin_edit_get( .into_response(); } }; + let (_, csrf, set_cookie) = resolve_session(&headers); match form::from_schema(url, &table, "update", Some(&id)) { - Ok(frm) => Html(admin_form_page( - &st, - &table, - "Edit", - &frm, - &format!("admin-{table}-edit"), - Some(&row), - None, - )) - .into_response(), + Ok(frm) => { + let mut resp = Html(admin_form_page( + &st, + &table, + "Edit", + &frm, + &format!("admin-{table}-edit"), + Some(&row), + None, + Some(&csrf), + )) + .into_response(); + append_set_cookie(&mut resp, set_cookie); + resp + } Err(e) => Html(admin_shell( &st, &table, @@ -878,15 +1004,24 @@ async fn admin_edit_post( return r; } let url = st.db_url.as_deref().unwrap(); + let (_, csrf, set_cookie, posted) = match validate_csrf_post(&headers, posted) { + Ok(v) => v, + Err(r) => return r, + }; match form::from_schema(url, &table, "update", Some(&id)) { - Ok(frm) => submit_and_respond( - &st, - &frm, - &format!("admin-{table}-edit"), - url, - posted, - Some(&table), - ), + Ok(frm) => { + let mut resp = submit_and_respond( + &st, + &frm, + &format!("admin-{table}-edit"), + url, + posted, + Some(&table), + Some(&csrf), + ); + append_set_cookie(&mut resp, set_cookie); + resp + } Err(e) => Html(admin_shell( &st, &table, @@ -906,7 +1041,7 @@ async fn admin_delete( return r; } let url = st.db_url.as_deref().unwrap(); - if let Err(e) = db::delete(url, &table, &id) { + if let Err(e) = db::delete(url, &table, &id, None) { return Html(admin_shell( &st, &table, diff --git a/plugins/web/src/lib.rs b/plugins/web/src/lib.rs index 3b2db0d..64fec88 100644 --- a/plugins/web/src/lib.rs +++ b/plugins/web/src/lib.rs @@ -4,6 +4,9 @@ mod compose; mod db; mod form; mod http; +mod middleware; +mod password; +mod rate_limit; mod render; mod session; mod table; @@ -355,7 +358,7 @@ web_ffi!(web_compose_form, |args: &Value| { web_ffi!(web_render, |args: &Value| { let page = args.get("page").cloned().unwrap_or_else(|| args.clone()); let db_url = db_url_of(args).ok(); - let html = render::render_page(&page, db_url.as_deref()); + let html = render::render_page(&page, db_url.as_deref(), None); Ok(Value::String(html)) }); @@ -381,7 +384,8 @@ web_ffi!(web_db_insert, |args: &Value| { .or_else(|| args.get("row")) .cloned() .unwrap_or(json!([])); - db::insert(&url, &table, &rows) + let txn = args.get("txn").and_then(|v| v.as_str()); + db::insert(&url, &table, &rows, txn) }); web_ffi!(web_db_select, |args: &Value| { @@ -403,18 +407,51 @@ web_ffi!(web_db_select, |args: &Value| { Some(v) => Some(v), }; let order = args.get("order").and_then(|v| v.as_str()); - if order.is_some_and(|s| !s.trim().is_empty()) { - db::select_order(&url, &table, limit, where_v, order) - } else { - db::select(&url, &table, limit, where_v) - } + let offset = args + .get("offset") + .or_else(|| args.get("跳过")) + .and_then(|v| v.as_i64()); + let txn = args.get("txn").and_then(|v| v.as_str()); + db::select_order(&url, &table, limit, where_v, order, offset, txn) +}); + +web_ffi!(web_db_query, |args: &Value| { + let url = db_url_of(args)?; + let sql = arg_str(args, "sql")?.to_string(); + let args_v = args.get("args"); + let txn = args.get("txn").and_then(|v| v.as_str()); + db::query(&url, &sql, args_v, txn) +}); + +web_ffi!(web_db_count, |args: &Value| { + let url = db_url_of(args)?; + let table = arg_str(args, "table")?.to_string(); + let where_v = args.get("where").filter(|v| !v.is_null()); + let txn = args.get("txn").and_then(|v| v.as_str()); + db::count(&url, &table, where_v, txn) +}); + +web_ffi!(web_db_begin, |args: &Value| { + let url = db_url_of(args)?; + db::begin(&url) +}); + +web_ffi!(web_db_commit, |args: &Value| { + let txn = arg_str(args, "txn")?.to_string(); + db::commit(&txn) +}); + +web_ffi!(web_db_rollback, |args: &Value| { + let txn = arg_str(args, "txn")?.to_string(); + db::rollback(&txn) }); web_ffi!(web_db_get, |args: &Value| { let url = db_url_of(args)?; let table = arg_str(args, "table")?.to_string(); let id = arg_text(args, "id")?; - db::get(&url, &table, &id) + let txn = args.get("txn").and_then(|v| v.as_str()); + db::get(&url, &table, &id, txn) }); web_ffi!(web_db_update, |args: &Value| { @@ -422,21 +459,24 @@ web_ffi!(web_db_update, |args: &Value| { let table = arg_str(args, "table")?.to_string(); let id = arg_text(args, "id")?; let row = args.get("row").cloned().unwrap_or(json!({})); - db::update(&url, &table, &id, &row) + let txn = args.get("txn").and_then(|v| v.as_str()); + db::update(&url, &table, &id, &row, txn) }); web_ffi!(web_db_delete, |args: &Value| { let url = db_url_of(args)?; let table = arg_str(args, "table")?.to_string(); let id = arg_text(args, "id")?; - db::delete(&url, &table, &id) + let txn = args.get("txn").and_then(|v| v.as_str()); + db::delete(&url, &table, &id, txn) }); web_ffi!(web_db_exec, |args: &Value| { let url = db_url_of(args)?; let sql = arg_str(args, "sql")?.to_string(); let args_v = args.get("args"); - db::exec(&url, &sql, args_v) + let txn = args.get("txn").and_then(|v| v.as_str()); + db::exec(&url, &sql, args_v, txn) }); web_ffi!(web_db_list_tables, |args: &Value| { @@ -597,6 +637,10 @@ web_ffi!(web_auth_logout, |args: &Value| { session::abi_auth_logout(args) }); +web_ffi!(web_password_hash, |args: &Value| { + session::abi_password_hash(args) +}); + web_ffi!(web_auth_new, |args: &Value| { let users = args.get("users").cloned().unwrap_or(Value::Null); let session_ttl = args @@ -667,6 +711,69 @@ web_ffi!(web_app_static, |args: &Value| { Ok(app) }); +// Configure cross-cutting HTTP middleware on an app: CORS, security response +// headers, gzip compression, request-body limits, and JSON API routes. +// +// Every capability is passed in as *data* (GFM tables from the Marqdo side, +// normalized here into the `middleware` map): 配置即数据、装配即函数. +// +// - `cors` : `|允许来源|方法|头|暴露头|凭证|` table (one row per origin). +// - `security` : `|头|值|` table of response headers (e.g. `X-Frame-Options`). +// - `compress` : bool — enable gzip response compression. +// - `body_limit` : number — max request body bytes (e.g. `1048576`). +// - `json_routes`: `|路径|方法|表|条件|排序|上限|` table of JSON API endpoints +// backed by db queries (method GET/POST, `表`=table name). +web_ffi!(web_app_middleware, |args: &Value| { + let mut app = args.get("app").cloned().unwrap_or(json!({})); + let obj = app + .as_object_mut() + .ok_or_else(|| "app must be a map".to_string())?; + let mw = obj + .entry("middleware".to_string()) + .or_insert_with(|| json!({})); + let m = mw + .as_object_mut() + .ok_or_else(|| "app.middleware must be a map".to_string())?; + + if let Some(cors) = args.get("cors") { + m.insert( + "cors".into(), + middleware::cors_from_table(cors), + ); + } + if let Some(security) = args.get("security") { + m.insert( + "security".into(), + middleware::security_from_table(security), + ); + } + if let Some(compress) = args.get("compress") { + let on = match compress { + Value::Bool(b) => *b, + Value::String(s) => matches!(s.as_str(), "true" | "True" | "1" | "yes" | "on"), + Value::Number(n) => n.as_i64().unwrap_or(0) != 0, + _ => false, + }; + m.insert("compress".into(), json!(on)); + } + if let Some(bl) = args.get("body_limit") { + let n = bl + .as_u64() + .or_else(|| bl.as_i64().map(|i| i as u64)) + .unwrap_or(0); + if n > 0 { + m.insert("body_limit".into(), json!(n)); + } + } + if let Some(routes) = args.get("json_routes") { + m.insert( + "json_routes".into(), + middleware::json_routes_from_table(routes), + ); + } + Ok(app) +}); + web_ffi!(web_form_new, |args: &Value| { let table = arg_str_opt(args, "table"); let action = arg_str_opt(args, "action").unwrap_or("insert"); @@ -712,6 +819,7 @@ web_ffi!(web_form_render, |args: &Value| { id, data, errors, + arg_str_opt(args, "csrf"), ))) }); @@ -770,6 +878,8 @@ web_ffi!(web_listen, |args: &Value| { auth_users, session_ttl, ws_routes, + middleware, + cookie_secure, ) = if args.get("page").is_some() || args.get("host").is_some() { let page = args.get("page").cloned().unwrap_or(json!({})); let db_url = db_url_of(args).ok(); @@ -795,6 +905,10 @@ web_ffi!(web_listen, |args: &Value| { .get("session_ttl") .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))) .unwrap_or(3600); + let cookie_secure = args + .get("cookie_secure") + .and_then(|v| v.as_bool()) + .unwrap_or(false); ( page, db_url, @@ -808,6 +922,8 @@ web_ffi!(web_listen, |args: &Value| { auth_users, session_ttl, HashMap::new(), + middleware::Middleware::default(), + cookie_secure, ) } else { let app = args.get("app").cloned().unwrap_or_else(|| args.clone()); @@ -887,6 +1003,15 @@ web_ffi!(web_listen, |args: &Value| { .unwrap_or(3600), ), }; + let middleware = middleware::parse(&app); + let mw_summary = middleware::summary(&app); + if !mw_summary.is_empty() { + eprintln!("marqdo web middleware: {mw_summary}"); + } + let cookie_secure = app + .get("cookie_secure") + .and_then(|v| v.as_bool()) + .unwrap_or(false); ( page, db_url, @@ -900,6 +1025,8 @@ web_ffi!(web_listen, |args: &Value| { auth_users, session_ttl, ws_routes, + middleware, + cookie_secure, ) }; let static_dir = static_dir.map(|p| { @@ -921,7 +1048,9 @@ web_ffi!(web_listen, |args: &Value| { &static_mount, auth_users, session_ttl, + cookie_secure, ws_routes, + &middleware, ) }); @@ -1003,16 +1132,29 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int ("web_render", "page", web_render as PluginFn), ("web_db_new", "url", web_db_new as PluginFn), ("web_db_init", "url,name,fields", web_db_init as PluginFn), - ("web_db_insert", "url,table,rows", web_db_insert as PluginFn), + ("web_db_insert", "url,table,rows,txn", web_db_insert as PluginFn), ( "web_db_select", - "url,table,where,limit,order", + "url,table,where,limit,order,offset,txn", web_db_select as PluginFn, ), - ("web_db_get", "url,table,id", web_db_get as PluginFn), - ("web_db_update", "url,table,id,row", web_db_update as PluginFn), - ("web_db_delete", "url,table,id", web_db_delete as PluginFn), - ("web_db_exec", "url,sql,args", web_db_exec as PluginFn), + ( + "web_db_query", + "url,sql,args,txn", + web_db_query as PluginFn, + ), + ( + "web_db_count", + "url,table,where,txn", + web_db_count as PluginFn, + ), + ("web_db_begin", "url", web_db_begin as PluginFn), + ("web_db_commit", "txn", web_db_commit as PluginFn), + ("web_db_rollback", "txn", web_db_rollback as PluginFn), + ("web_db_get", "url,table,id,txn", web_db_get as PluginFn), + ("web_db_update", "url,table,id,row,txn", web_db_update as PluginFn), + ("web_db_delete", "url,table,id,txn", web_db_delete as PluginFn), + ("web_db_exec", "url,sql,args,txn", web_db_exec as PluginFn), ("web_db_list_tables", "url", web_db_list_tables as PluginFn), ( "web_app_new", @@ -1030,6 +1172,11 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int "app,dir,mount", web_app_static as PluginFn, ), + ( + "web_app_middleware", + "app,cors,security,compress,body_limit,json_routes", + web_app_middleware as PluginFn, + ), ("web_form_new", "table,action,id", web_form_new as PluginFn), ("web_form_fields", "form,fields", web_form_fields as PluginFn), ("web_form_rules", "form,rules", web_form_rules as PluginFn), @@ -1058,6 +1205,7 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int ), ("web_auth_check", "session_id", web_auth_check as PluginFn), ("web_auth_logout", "session_id", web_auth_logout as PluginFn), + ("web_password_hash", "password", web_password_hash as PluginFn), ("web_auth_new", "users,session_ttl", web_auth_new as PluginFn), ("web_app_auth", "app,users,session_ttl", web_app_auth as PluginFn), ( diff --git a/plugins/web/src/middleware.rs b/plugins/web/src/middleware.rs new file mode 100644 index 0000000..0d9c8fa --- /dev/null +++ b/plugins/web/src/middleware.rs @@ -0,0 +1,438 @@ +//! Declarative middleware for the web app. +//! +//! All cross-cutting HTTP capabilities (CORS, security headers, compression, +//! body limits, JSON API routes) are configured as *data* on the `app` object +//! and applied to the axum router here. This keeps the authoring surface +//! declarative: a GFM table in Marqdo becomes a `middleware` map, and this +//! module turns it into axum layers — 配置即数据、装配即函数. + +use std::sync::Arc; + +use axum::body::Body; +use axum::extract::State; +use axum::http::{header, HeaderValue, Method, StatusCode}; +use axum::response::{IntoResponse, Response}; +use axum::routing::{get, post}; +use axum::Router; +use serde_json::{json, Map, Value}; +use tower_http::compression::CompressionLayer; +use tower_http::cors::{Any, CorsLayer}; +use tower_http::limit::RequestBodyLimitLayer; +use tower_http::set_header::SetResponseHeaderLayer; + +use crate::db; + +/// Render a cell value as text (strings stay, numbers/bools stringify). +fn cell_text(v: &Value) -> String { + match v { + Value::Null => String::new(), + Value::String(s) => s.clone(), + Value::Bool(b) => b.to_string(), + Value::Number(n) => n.to_string(), + other => other.to_string(), + } +} + +/// Parsed middleware configuration (from `app["middleware"]`). +#[derive(Clone, Default)] +pub struct Middleware { + pub cors: Option<CorsConfig>, + pub security: Vec<(String, String)>, + pub compress: bool, + pub body_limit: Option<u64>, + /// JSON API routes: `path -> { method, table, where?, order?, limit? }`. + pub json_routes: Vec<(String, JsonRoute)>, +} + +#[derive(Clone)] +pub struct CorsConfig { + pub allow_origins: Vec<String>, + pub methods: Vec<String>, + pub headers: Vec<String>, + pub expose_headers: Vec<String>, + pub credentials: bool, +} + +#[derive(Clone)] +pub struct JsonRoute { + pub method: String, + pub table: String, + pub where_v: Option<Value>, + pub order: Option<String>, + pub limit: i64, +} + +/// Parse `app["middleware"]` (a map) into a `Middleware`. +pub fn parse(app: &Value) -> Middleware { + let mut mw = Middleware::default(); + let Some(obj) = app.get("middleware").and_then(|v| v.as_object()) else { + return mw; + }; + if let Some(c) = obj.get("cors") { + mw.cors = parse_cors(c); + } + if let Some(sec) = obj.get("security").and_then(|v| v.as_object()) { + mw.security = sec + .iter() + .map(|(k, v)| (k.clone(), v.as_str().unwrap_or("").to_string())) + .filter(|(_, v)| !v.is_empty()) + .collect(); + } + mw.compress = obj + .get("compress") + .and_then(|v| v.as_bool()) + .unwrap_or(false); + mw.body_limit = obj + .get("body_limit") + .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|i| i as u64))) + .filter(|n| *n > 0); + if let Some(routes) = obj.get("json_routes").and_then(|v| v.as_object()) { + for (path, spec) in routes { + if let Some(route) = parse_json_route(path, spec) { + mw.json_routes.push(route); + } + } + mw.json_routes.sort_by(|a, b| a.0.cmp(&b.0)); + } + mw +} + +fn parse_cors(v: &Value) -> Option<CorsConfig> { + let obj = v.as_object()?; + let str_list = |key: &str| -> Vec<String> { + obj.get(key) + .and_then(|v| v.as_array()) + .map(|arr| { + arr.iter() + .filter_map(|x| x.as_str().map(|s| s.to_string())) + .collect() + }) + .unwrap_or_default() + }; + Some(CorsConfig { + allow_origins: str_list("allow_origins"), + methods: str_list("methods"), + headers: str_list("headers"), + expose_headers: str_list("expose_headers"), + credentials: obj + .get("credentials") + .and_then(|v| v.as_bool()) + .unwrap_or(false), + }) +} + +fn parse_json_route(path: &str, spec: &Value) -> Option<(String, JsonRoute)> { + let spec = spec.as_object()?; + let method = spec + .get("method") + .and_then(|v| v.as_str()) + .unwrap_or("GET") + .to_uppercase(); + let table = spec.get("table")?.as_str()?.to_string(); + let order = spec.get("order").and_then(|v| v.as_str()).map(|s| s.to_string()); + let limit = spec + .get("limit") + .and_then(|v| v.as_i64()) + .unwrap_or(200); + Some(( + format!("/{}", path.trim_start_matches('/')), + JsonRoute { + method, + table, + where_v: spec.get("where").cloned(), + order, + limit, + }, + )) +} + +/// Apply the middleware configuration to a router (state already provided). +pub fn apply( + router: Router<Arc<crate::http::AppState>>, + mw: &Middleware, +) -> Router<Arc<crate::http::AppState>> { + let mut app = router; + + // JSON API routes first: axum layers only wrap routes registered before + // the `.layer()` call, so these must be mounted ahead of the pipeline. + for (path, route) in &mw.json_routes { + app = mount_json(app, path, route.clone()); + } + + // CORS + if let Some(c) = &mw.cors { + let mut layer = CorsLayer::new(); + if c.allow_origins.is_empty() { + layer = layer.allow_origin(Any); + } else { + layer = layer.allow_origin( + c.allow_origins + .iter() + .filter_map(|o| o.parse::<HeaderValue>().ok()) + .collect::<Vec<_>>(), + ); + } + if c.methods.is_empty() { + layer = layer.allow_methods(Any); + } else { + layer = layer.allow_methods( + c.methods + .iter() + .filter_map(|m| m.parse::<Method>().ok()) + .collect::<Vec<_>>(), + ); + } + if c.headers.is_empty() { + layer = layer.allow_headers(Any); + } else { + layer = layer.allow_headers( + c.headers + .iter() + .filter_map(|h| h.parse::<axum::http::HeaderName>().ok()) + .collect::<Vec<_>>(), + ); + } + if !c.expose_headers.is_empty() { + layer = layer.expose_headers( + c.expose_headers + .iter() + .filter_map(|h| h.parse::<axum::http::HeaderName>().ok()) + .collect::<Vec<_>>(), + ); + } + layer = layer.allow_credentials(c.credentials); + app = app.layer(layer); + } + + // Security response headers + for (name, value) in &mw.security { + let Some(header_name) = header_name_from(&name.to_ascii_lowercase()) else { + continue; + }; + if let Ok(v) = HeaderValue::from_str(value) { + app = app.layer(SetResponseHeaderLayer::overriding(header_name, v)); + } + } + + // Compression + if mw.compress { + app = app.layer(CompressionLayer::new()); + } + + // Request body limit + if let Some(bytes) = mw.body_limit { + app = app.layer(RequestBodyLimitLayer::new(bytes as usize)); + } + + app +} + +fn header_name_from(name: &str) -> Option<axum::http::HeaderName> { + let h = match name { + "x_frame_options" | "x-frame-options" => header::X_FRAME_OPTIONS, + "content_security_policy" | "content-security-policy" => header::CONTENT_SECURITY_POLICY, + "x_content_type_options" | "x-content-type-options" => header::X_CONTENT_TYPE_OPTIONS, + "referrer_policy" | "referrer-policy" => header::REFERRER_POLICY, + "strict_transport_security" | "strict-transport-security" | "hsts" => { + header::STRICT_TRANSPORT_SECURITY + } + "x_xss_protection" | "x-xss-protection" => header::X_XSS_PROTECTION, + _ => return None, + }; + Some(h) +} + +/// Mount a JSON API route backed by a db query. +fn mount_json( + app: Router<Arc<crate::http::AppState>>, + path: &str, + route: JsonRoute, +) -> Router<Arc<crate::http::AppState>> { + let handler = match route.method.as_str() { + "POST" => post(move |State(st): State<Arc<crate::http::AppState>>| { + json_handler(st, route.clone()) + }), + _ => get(move |State(st): State<Arc<crate::http::AppState>>| { + json_handler(st, route.clone()) + }), + }; + app.route(path, handler) +} + +async fn json_handler(st: Arc<crate::http::AppState>, route: JsonRoute) -> Response { + let Some(url) = st.db_url.as_deref() else { + return ( + StatusCode::INTERNAL_SERVER_ERROR, + JsonBody(json!({ "error": "no database" })), + ) + .into_response(); + }; + let res = match &route.order { + Some(o) if !o.trim().is_empty() => { + db::select_order(url, &route.table, route.limit, route.where_v.as_ref(), Some(o), None, None) + } + _ => db::select(url, &route.table, route.limit, route.where_v.as_ref()), + }; + match res { + Ok(data) => JsonBody(data).into_response(), + Err(e) => ( + StatusCode::INTERNAL_SERVER_ERROR, + JsonBody(json!({ "error": e })), + ) + .into_response(), + } +} + +/// A JSON body response wrapper (sets `Content-Type: application/json`). +pub struct JsonBody(pub Value); + +impl IntoResponse for JsonBody { + fn into_response(self) -> Response { + Response::builder() + .status(StatusCode::OK) + .header(header::CONTENT_TYPE, "application/json; charset=utf-8") + .body(Body::from(self.0.to_string())) + .unwrap() + } +} + +/// Normalize a `security` table into the `middleware.security` map shape. +/// Accepts a GFM table `|头|值|` (list of rows) or an already-shaped map. +pub fn security_from_table(table: &Value) -> Value { + let rows = crate::table::as_rows(table); + let mut m = Map::new(); + if let Some(arr) = rows.as_array() { + for row in arr { + let col = |keys: &[&str]| -> String { + keys.iter() + .find_map(|k| row.get(*k).map(cell_text)) + .unwrap_or_default() + }; + let k = col(&["头", "header", "Header", "name"]); + let v = col(&["值", "value", "Value"]); + if !k.is_empty() { + m.insert(k, json!(v)); + } + } + } + Value::Object(m) +} + +/// Normalize a CORS table `|允许来源|方法|头|暴露头|凭证|` (one row per origin) +/// into the `middleware.cors` map shape. +pub fn cors_from_table(table: &Value) -> Value { + let rows = crate::table::as_rows(table); + let mut allow_origins = Vec::new(); + let mut methods = Vec::new(); + let mut headers = Vec::new(); + let mut expose_headers = Vec::new(); + let mut credentials = false; + if let Some(arr) = rows.as_array() { + for row in arr { + let col = |keys: &[&str]| -> String { + keys.iter() + .find_map(|k| row.get(*k).map(cell_text)) + .unwrap_or_default() + }; + let origin = col(&["允许来源", "origin", "Origin"]); + if !origin.is_empty() && origin != "*" { + allow_origins.push(origin); + } + for m in col(&["方法", "methods", "Methods"]).split(',') { + let m = m.trim(); + if !m.is_empty() { + methods.push(m.to_string()); + } + } + for h in col(&["头", "headers", "Headers"]).split(',') { + let h = h.trim(); + if !h.is_empty() { + headers.push(h.to_string()); + } + } + for h in col(&["暴露头", "expose_headers", "Expose-Headers"]).split(',') { + let h = h.trim(); + if !h.is_empty() { + expose_headers.push(h.to_string()); + } + } + let cred = col(&["凭证", "credentials", "Credentials"]); + if cred.eq_ignore_ascii_case("true") + || cred == "1" + || cred.eq_ignore_ascii_case("yes") + { + credentials = true; + } + } + } + json!({ + "allow_origins": allow_origins, + "methods": methods, + "headers": headers, + "expose_headers": expose_headers, + "credentials": credentials, + }) +} + +/// Normalize a JSON-routes table into `middleware.json_routes` map shape. +/// Accepts a GFM table `|路径|方法|表|条件|排序|上限|` (rows). +pub fn json_routes_from_table(table: &Value) -> Value { + let rows = crate::table::as_rows(table); + let mut routes = Map::new(); + if let Some(arr) = rows.as_array() { + for row in arr { + let col = |keys: &[&str]| -> String { + keys.iter() + .find_map(|k| row.get(*k).map(cell_text)) + .unwrap_or_default() + }; + let path = col(&["路径", "path", "Path"]).trim_start_matches('/').to_string(); + if path.is_empty() { + continue; + } + let method = col(&["方法", "method", "Method"]).to_uppercase(); + let method = if method.is_empty() { "GET".into() } else { method }; + let table = col(&["表", "table", "Table"]); + if table.is_empty() { + continue; + } + let order = col(&["排序", "order", "Order"]); + let limit = col(&["上限", "limit", "Limit"]).parse::<i64>().unwrap_or(200); + let mut spec = Map::new(); + spec.insert("method".into(), json!(method)); + spec.insert("table".into(), json!(table)); + if !order.is_empty() { + spec.insert("order".into(), json!(order)); + } + spec.insert("limit".into(), json!(limit)); + routes.insert(path, Value::Object(spec)); + } + } + Value::Object(routes) +} + +/// Read the `middleware` map out of an app object (for logging/debug). +pub fn summary(app: &Value) -> String { + let mw = parse(app); + let mut parts = Vec::new(); + if mw.cors.is_some() { + parts.push("cors".to_string()); + } + if !mw.security.is_empty() { + parts.push("security".to_string()); + } + if mw.compress { + parts.push("compress".to_string()); + } + if mw.body_limit.is_some() { + parts.push("body_limit".to_string()); + } + if !mw.json_routes.is_empty() { + parts.push(format!("json:{}", mw.json_routes.len())); + } + if parts.is_empty() { + String::new() + } else { + parts.join(" + ") + } +} diff --git a/plugins/web/src/password.rs b/plugins/web/src/password.rs new file mode 100644 index 0000000..5c80668 --- /dev/null +++ b/plugins/web/src/password.rs @@ -0,0 +1,64 @@ +//! Password hashing (argon2id) for admin users and auth tables. + +use argon2::{ + password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString}, + Argon2, +}; +use getrandom::getrandom; + +/// True when `stored` looks like an argon2 PHC string. +pub fn is_hashed(stored: &str) -> bool { + stored.starts_with("$argon2") +} + +/// Hash a plaintext password (argon2id, random salt). +pub fn hash_password(plain: &str) -> Result<String, String> { + if plain.is_empty() { + return Err("password must not be empty".into()); + } + let mut salt_bytes = [0u8; 16]; + getrandom(&mut salt_bytes).map_err(|e| format!("getrandom: {e}"))?; + let salt = SaltString::encode_b64(&salt_bytes).map_err(|e| e.to_string())?; + let argon2 = Argon2::default(); + argon2 + .hash_password(plain.as_bytes(), &salt) + .map(|h| h.to_string()) + .map_err(|e| e.to_string()) +} + +/// Verify plaintext against stored hash or legacy plaintext (dev / gold tests). +pub fn verify_password(plain: &str, stored: &str) -> bool { + if stored.is_empty() || plain.is_empty() { + return false; + } + if is_hashed(stored) { + let parsed = match PasswordHash::new(stored) { + Ok(h) => h, + Err(_) => return false, + }; + Argon2::default() + .verify_password(plain.as_bytes(), &parsed) + .is_ok() + } else { + plain == stored + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hash_and_verify_roundtrip() { + let hash = hash_password("secret").unwrap(); + assert!(is_hashed(&hash)); + assert!(verify_password("secret", &hash)); + assert!(!verify_password("wrong", &hash)); + } + + #[test] + fn legacy_plaintext_still_works() { + assert!(verify_password("secret", "secret")); + assert!(!verify_password("other", "secret")); + } +} diff --git a/plugins/web/src/rate_limit.rs b/plugins/web/src/rate_limit.rs new file mode 100644 index 0000000..e879f9a --- /dev/null +++ b/plugins/web/src/rate_limit.rs @@ -0,0 +1,95 @@ +//! Login rate limiting — max failures per IP+username within a window. + +use std::collections::HashMap; +use std::sync::Mutex; +use std::time::{SystemTime, UNIX_EPOCH}; + +const MAX_FAILS: u32 = 5; +const WINDOW_SEC: u64 = 900; // 15 minutes + +struct Window { + count: u32, + start: u64, +} + +static ATTEMPTS: Mutex<Option<HashMap<String, Window>>> = Mutex::new(None); + +fn now_secs() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|d| d.as_secs()) + .unwrap_or(0) +} + +fn key(ip: &str, username: &str) -> String { + format!("{ip}|{username}") +} + +fn with_map<T>(f: impl FnOnce(&mut HashMap<String, Window>) -> T) -> T { + let mut guard = ATTEMPTS.lock().unwrap_or_else(|e| e.into_inner()); + let map = guard.get_or_insert_with(HashMap::new); + f(map) +} + +/// Reset attempt counters (between listens / tests). +pub fn reset() { + let mut guard = ATTEMPTS.lock().unwrap_or_else(|e| e.into_inner()); + *guard = Some(HashMap::new()); +} + +/// Returns `Err(message)` when the client is temporarily locked out. +pub fn check(ip: &str, username: &str) -> Result<(), String> { + let k = key(ip, username); + let now = now_secs(); + with_map(|map| { + if let Some(w) = map.get(&k) { + if now.saturating_sub(w.start) < WINDOW_SEC && w.count >= MAX_FAILS { + return Err( + "Too many failed login attempts. Try again in about 15 minutes.".into(), + ); + } + if now.saturating_sub(w.start) >= WINDOW_SEC { + map.remove(&k); + } + } + Ok(()) + }) +} + +pub fn record_failure(ip: &str, username: &str) { + let k = key(ip, username); + let now = now_secs(); + with_map(|map| { + let entry = map.entry(k).or_insert(Window { count: 0, start: now }); + if now.saturating_sub(entry.start) >= WINDOW_SEC { + entry.count = 0; + entry.start = now; + } + entry.count = entry.count.saturating_add(1); + }); +} + +pub fn clear_success(ip: &str, username: &str) { + let k = key(ip, username); + with_map(|map| { + map.remove(&k); + }); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn locks_after_max_fails() { + reset(); + let ip = "127.0.0.1"; + let user = "admin"; + for _ in 0..MAX_FAILS { + record_failure(ip, user); + } + assert!(check(ip, user).is_err()); + clear_success(ip, user); + assert!(check(ip, user).is_ok()); + } +} diff --git a/plugins/web/src/render.rs b/plugins/web/src/render.rs index 6ce688f..f4ea5d7 100644 --- a/plugins/web/src/render.rs +++ b/plugins/web/src/render.rs @@ -188,7 +188,7 @@ fn select_page_data( } let order = args.get("order").and_then(|v| v.as_str()); if order.is_some_and(|s| !s.trim().is_empty()) { - db::select_order(url, table, limit, where_v.as_ref(), order) + db::select_order(url, table, limit, where_v.as_ref(), order, None, None) } else { db::select(url, table, limit, where_v.as_ref()) } @@ -321,8 +321,8 @@ a:hover { color:var(--accent); } .article-code { background:#f5f5f4; border:1px solid var(--line); border-radius:6px; padding:1rem; overflow-x:auto; font-size:.9rem; } "#; -pub fn render_page(args: &Value, db_url: Option<&str>) -> String { - render_page_ex(args, db_url, None, None) +pub fn render_page(args: &Value, db_url: Option<&str>, csrf: Option<&str>) -> String { + render_page_ex(args, db_url, None, None, csrf) } /// Like `render_page`, optionally replaying form POST data/errors into the embedded form. @@ -331,6 +331,7 @@ pub fn render_page_ex( db_url: Option<&str>, form_data: Option<&Value>, form_errors: Option<&Value>, + csrf: Option<&str>, ) -> String { let title = args .get("title") @@ -372,6 +373,7 @@ pub fn render_page_ex( form_id, form_data, form_errors, + csrf, )); } if !items.is_empty() { @@ -466,7 +468,7 @@ pub fn render_fragment(args: &Value, db_url: Option<&str>) -> String { .get("form_id") .and_then(|v| v.as_str()) .unwrap_or("form"); - body.push_str(&crate::form::render_body(form, form_id, None, None)); + body.push_str(&crate::form::render_body(form, form_id, None, None, None)); } if !items.is_empty() { let is_detail = args diff --git a/plugins/web/src/session.rs b/plugins/web/src/session.rs index 0268ec1..3f27d53 100644 --- a/plugins/web/src/session.rs +++ b/plugins/web/src/session.rs @@ -1,22 +1,40 @@ -//! In-memory session store + cookie helpers (design ext-web-net §3.1 / §3.2). +//! Session store + cookie helpers + CSRF tokens (ext-web-net W3). //! -//! Sessions live for the lifetime of the plugin process (same as `web_listen`). -//! Session ids are random hex strings; cookies are `marqdo_sid=<id>` with -//! `HttpOnly; Path=/; SameSite=Lax`. +//! - **CSPRNG** session ids (`getrandom`). +//! - **SQLite persistence** when `configure` receives a `db_url`; otherwise in-memory (offline gold). +//! - **Sliding expiry** on read/write. +//! - **CSRF** token per session (`_csrf` key). use std::collections::HashMap; use std::sync::Mutex; use std::time::{SystemTime, UNIX_EPOCH}; +use getrandom::getrandom; use serde_json::{json, Map, Value}; +use crate::db; +use crate::password; + type SessionMap = HashMap<String, Value>; -struct Store { + +#[derive(Clone, Default)] +pub struct Config { + pub db_url: Option<String>, + pub ttl_sec: u64, + pub cookie_secure: bool, +} + +static CONFIG: Mutex<Option<Config>> = Mutex::new(None); + +struct MemStore { ttl_sec: u64, data: HashMap<String, (u64, SessionMap)>, } -static STORE: Mutex<Option<Store>> = Mutex::new(None); +static MEM: Mutex<Option<MemStore>> = Mutex::new(None); + +const SESSION_TABLE: &str = "_marqdo_sessions"; +const CSRF_KEY: &str = "_csrf"; fn now_secs() -> u64 { SystemTime::now() @@ -25,111 +43,310 @@ fn now_secs() -> u64 { .unwrap_or(0) } -fn with_store<T>(f: impl FnOnce(&mut Store) -> T) -> T { - let mut guard = STORE.lock().unwrap_or_else(|e| e.into_inner()); - let store = guard.get_or_insert_with(|| Store { - ttl_sec: 3600, +fn cfg() -> Config { + CONFIG + .lock() + .unwrap_or_else(|e| e.into_inner()) + .clone() + .unwrap_or(Config { + db_url: None, + ttl_sec: 3600, + cookie_secure: false, + }) +} + +/// Configure session backend before `listen` or offline ABI calls. +pub fn configure(config: Config) { + let ttl = if config.ttl_sec > 0 { + config.ttl_sec + } else { + 3600 + }; + let mut c = config; + c.ttl_sec = ttl; + if let Some(ref url) = c.db_url { + let _ = ensure_session_table(url); + } + let mut guard = CONFIG.lock().unwrap_or_else(|e| e.into_inner()); + *guard = Some(c); +} + +/// Reset in-memory sessions (tests / each listen). +pub fn reset(ttl_sec: u64) { + let ttl = if ttl_sec > 0 { ttl_sec } else { 3600 }; + { + let mut guard = CONFIG.lock().unwrap_or_else(|e| e.into_inner()); + if let Some(ref mut c) = *guard { + c.ttl_sec = ttl; + } else { + *guard = Some(Config { + db_url: None, + ttl_sec: ttl, + cookie_secure: false, + }); + } + } + let mut mem = MEM.lock().unwrap_or_else(|e| e.into_inner()); + *mem = Some(MemStore { + ttl_sec: ttl, + data: HashMap::new(), + }); +} + +fn ensure_session_table(url: &str) -> Result<(), String> { + db::exec( + url, + &format!( + "CREATE TABLE IF NOT EXISTS \"{SESSION_TABLE}\" ( + id TEXT PRIMARY KEY, + expires_at INTEGER NOT NULL, + data TEXT NOT NULL + ); + CREATE INDEX IF NOT EXISTS idx_marqdo_sessions_exp ON \"{SESSION_TABLE}\"(expires_at);" + ), + None, + None, + )?; + Ok(()) +} + +fn prune_mem(store: &mut MemStore) { + let now = now_secs(); + store.data.retain(|_, (exp, _)| *exp > now); +} + +fn prune_sql(url: &str) { + let now = now_secs() as i64; + let _ = db::exec( + url, + &format!("DELETE FROM \"{SESSION_TABLE}\" WHERE expires_at <= ?1"), + Some(&json!([now])), + None, + ); +} + +fn secure_random_id() -> Result<String, String> { + let mut bytes = [0u8; 32]; + getrandom(&mut bytes).map_err(|e| format!("getrandom: {e}"))?; + Ok(bytes.iter().map(|b| format!("{b:02x}")).collect()) +} + +fn random_csrf() -> Result<String, String> { + let mut bytes = [0u8; 24]; + getrandom(&mut bytes).map_err(|e| format!("getrandom: {e}"))?; + Ok(bytes.iter().map(|b| format!("{b:02x}")).collect()) +} + +fn map_to_json(m: &SessionMap) -> Result<String, String> { + let obj: Map<String, Value> = m.iter().map(|(k, v)| (k.clone(), v.clone())).collect(); + serde_json::to_string(&Value::Object(obj)).map_err(|e| e.to_string()) +} + +fn json_to_map(s: &str) -> SessionMap { + match serde_json::from_str::<Value>(s) { + Ok(Value::Object(m)) => m.into_iter().collect(), + _ => HashMap::new(), + } +} + +fn load_sql(url: &str, id: &str) -> Option<(u64, SessionMap)> { + let q = db::query( + url, + &format!("SELECT expires_at, data FROM \"{SESSION_TABLE}\" WHERE id = ?1"), + Some(&json!([id])), + None, + ) + .ok()?; + let rows = q.get("rows")?.as_array()?; + let row = rows.first()?; + let exp = row.get("expires_at")?.as_i64()? as u64; + let data_s = row.get("data")?.as_str()?; + Some((exp, json_to_map(data_s))) +} + +fn save_sql(url: &str, id: &str, exp: u64, data: &SessionMap) -> Result<(), String> { + let data_s = map_to_json(data)?; + db::exec( + url, + &format!( + "INSERT INTO \"{SESSION_TABLE}\" (id, expires_at, data) VALUES (?1, ?2, ?3) + ON CONFLICT(id) DO UPDATE SET expires_at = excluded.expires_at, data = excluded.data" + ), + Some(&json!([id, exp as i64, data_s])), + None, + )?; + Ok(()) +} + +fn delete_sql(url: &str, id: &str) { + let _ = db::exec( + url, + &format!("DELETE FROM \"{SESSION_TABLE}\" WHERE id = ?1"), + Some(&json!([id])), + None, + ); +} + +fn touch_expiry(ttl: u64) -> u64 { + now_secs() + ttl +} + +fn ensure_csrf(data: &mut SessionMap) -> Result<String, String> { + if let Some(Value::String(t)) = data.get(CSRF_KEY) { + if !t.is_empty() { + return Ok(t.clone()); + } + } + let t = random_csrf()?; + data.insert(CSRF_KEY.to_string(), json!(t)); + Ok(t) +} + +pub fn session_new(ttl_sec: Option<u64>) -> String { + let cfg = cfg(); + let ttl = ttl_sec.unwrap_or(cfg.ttl_sec); + if ttl > 0 { + if let Ok(mut guard) = CONFIG.lock() { + if let Some(ref mut c) = *guard { + c.ttl_sec = ttl; + } + } + } + let id = secure_random_id().unwrap_or_else(|_| format!("fallback-{}", now_secs())); + let exp = touch_expiry(ttl); + let mut data = SessionMap::new(); + let _ = ensure_csrf(&mut data); + if let Some(ref url) = cfg.db_url { + prune_sql(url); + let _ = save_sql(url, &id, exp, &data); + } else { + with_mem(|store| { + store.ttl_sec = ttl; + prune_mem(store); + store.data.insert(id.clone(), (exp, data)); + }); + } + id +} + +fn with_mem<T>(f: impl FnOnce(&mut MemStore) -> T) -> T { + let mut guard = MEM.lock().unwrap_or_else(|e| e.into_inner()); + let store = guard.get_or_insert_with(|| MemStore { + ttl_sec: cfg().ttl_sec, data: HashMap::new(), }); f(store) } -fn prune(store: &mut Store) { +fn get_record(id: &str) -> Option<(u64, SessionMap)> { + let cfg = cfg(); let now = now_secs(); - store.data.retain(|_, (exp, _)| *exp > now); -} - -fn random_id() -> String { - use std::time::{SystemTime, UNIX_EPOCH}; - let nanos = SystemTime::now() - .duration_since(UNIX_EPOCH) - .map(|d| d.subsec_nanos()) - .unwrap_or(0); - let pid = std::process::id(); - // Cheap but adequate unique-enough id for an in-memory session store. - format!("{pid:x}{nanos:x}{:x}", rand_word()) -} - -fn rand_word() -> u64 { - // xorshift64 from a counter; not cryptographically strong but fine for - // session cookies when combined with pid+time. (Plugin is single-process.) - static COUNTER: Mutex<u64> = Mutex::new(0x9e3779b97f4a7c15); - let mut c = COUNTER.lock().unwrap_or_else(|e| e.into_inner()); - let mut x = *c; - x ^= x << 13; - x ^= x >> 7; - x ^= x << 17; - *c = x; - x -} - -/// Reset the store (used between listens / for tests). -pub fn reset(ttl_sec: u64) { - let mut guard = STORE.lock().unwrap_or_else(|e| e.into_inner()); - *guard = Some(Store { - ttl_sec, - data: HashMap::new(), - }); -} - -pub fn session_new(ttl_sec: Option<u64>) -> String { - with_store(|store| { - if let Some(ttl) = ttl_sec { - if ttl > 0 { - store.ttl_sec = ttl; - } + if let Some(ref url) = cfg.db_url { + prune_sql(url); + let (exp, data) = load_sql(url, id)?; + if exp <= now { + delete_sql(url, id); + return None; } - prune(store); - let id = random_id(); - let exp = now_secs() + store.ttl_sec; - store.data.insert(id.clone(), (exp, SessionMap::new())); - id - }) + Some((exp, data)) + } else { + with_mem(|store| { + prune_mem(store); + let (exp, data) = store.data.get(id)?.clone(); + if exp <= now { + store.data.remove(id); + return None; + } + Some((exp, data)) + }) + } +} + +fn put_record(id: &str, exp: u64, data: SessionMap) -> bool { + let cfg = cfg(); + if let Some(ref url) = cfg.db_url { + save_sql(url, id, exp, &data).is_ok() + } else { + with_mem(|store| { + store.data.insert(id.to_string(), (exp, data)); + true + }) + } } pub fn session_set(id: &str, key: &str, value: Value) -> bool { - with_store(|store| { - prune(store); - let Some((exp, m)) = store.data.get_mut(id) else { - return false; - }; - if *exp <= now_secs() { - return false; - } - m.insert(key.to_string(), value); - true - }) + let cfg = cfg(); + let Some((_, mut data)) = get_record(id) else { + return false; + }; + data.insert(key.to_string(), value); + put_record(id, touch_expiry(cfg.ttl_sec), data) } pub fn session_get(id: &str, key: &str) -> Option<Value> { - with_store(|store| { - prune(store); - let (exp, m) = store.data.get(id)?; - if *exp <= now_secs() { - return None; - } - m.get(key).cloned() - }) + let cfg = cfg(); + let (_, data) = get_record(id)?; + let v = data.get(key).cloned()?; + put_record(id, touch_expiry(cfg.ttl_sec), data); + Some(v) } pub fn session_del(id: &str, key: &str) -> bool { - with_store(|store| { - prune(store); - let Some((exp, m)) = store.data.get_mut(id) else { - return false; - }; - if *exp <= now_secs() { - return false; - } - m.remove(key).is_some() - }) + let cfg = cfg(); + let Some((_, mut data)) = get_record(id) else { + return false; + }; + let ok = data.remove(key).is_some(); + if ok { + put_record(id, touch_expiry(cfg.ttl_sec), data); + } + ok } pub fn session_destroy(id: &str) -> bool { - with_store(|store| { - prune(store); - store.data.remove(id).is_some() - }) + let cfg = cfg(); + if let Some(ref url) = cfg.db_url { + delete_sql(url, id); + true + } else { + with_mem(|store| store.data.remove(id).is_some()) + } +} + +/// CSRF token for an existing session (creates one if missing). +pub fn csrf_for(id: &str) -> Option<String> { + let cfg = cfg(); + let (_, mut data) = get_record(id)?; + let token = ensure_csrf(&mut data).ok()?; + put_record(id, touch_expiry(cfg.ttl_sec), data); + Some(token) +} + +pub fn validate_csrf(id: &str, token: &str) -> bool { + if token.is_empty() { + return false; + } + match session_get(id, CSRF_KEY) { + Some(Value::String(stored)) => stored == token, + _ => false, + } +} + +/// Resolve session from cookie: reuse valid id or create a new one. +/// Returns `(session_id, set_cookie_header)`. +pub fn ensure_from_cookie(cookie_header: Option<&str>) -> (String, Option<String>) { + if let Some(id) = session_id_from_cookie(cookie_header) { + if get_record(&id).is_some() { + let _ = csrf_for(&id); + return (id, None); + } + } + let id = session_new(None); + let cfg = cfg(); + ( + id.clone(), + Some(session_cookie(&id, cfg.ttl_sec, cfg.cookie_secure)), + ) } /// Parse the session id from a `Cookie` request header (looks for `marqdo_sid=…`). @@ -147,16 +364,18 @@ pub fn session_id_from_cookie(cookie_header: Option<&str>) -> Option<String> { None } -/// `Set-Cookie: marqdo_sid=<id>; HttpOnly; Path=/; SameSite=Lax; Max-Age=<ttl>` -pub fn session_cookie(id: &str, ttl_sec: u64) -> String { - format!( +/// `Set-Cookie: marqdo_sid=<id>; HttpOnly; Path=/; SameSite=Lax; Max-Age=<ttl>[; Secure]` +pub fn session_cookie(id: &str, ttl_sec: u64, secure: bool) -> String { + let mut s = format!( "marqdo_sid={id}; HttpOnly; Path=/; SameSite=Lax; Max-Age={ttl_sec}" - ) + ); + if secure { + s.push_str("; Secure"); + } + s } -/// Validate `username`/`password` against an admin-users table (`|用户名|密码|` -/// with headers `username`/`密码` or `用户`/`密码`, or plain `username`/`password`). -/// Returns the matched username on success. +/// Validate credentials; supports argon2 hashes or legacy plaintext in the password column. pub fn check_credentials(users: &Value, username: &str, password: &str) -> Option<String> { let rows = match users { Value::Array(a) => a, @@ -173,7 +392,7 @@ pub fn check_credentials(users: &Value, username: &str, password: &str) -> Optio let p = pick_cell(m, &["密码", "password", "pass", "口令"]) .map(cell_str) .unwrap_or_default(); - if u == username && p == password { + if u == username && password::verify_password(password, &p) { return Some(u); } } @@ -194,7 +413,6 @@ fn cell_str(v: &Value) -> String { } } -/// ABI helpers. pub fn abi_session_new(args: &Value) -> Result<Value, String> { let ttl = args .get("ttl_sec") @@ -260,6 +478,12 @@ pub fn abi_auth_logout(args: &Value) -> Result<Value, String> { Ok(json!({ "ok": session_destroy(id) })) } +pub fn abi_password_hash(args: &Value) -> Result<Value, String> { + let plain = arg_str(args, "password")?; + let hash = password::hash_password(plain)?; + Ok(json!({ "hash": hash })) +} + fn arg_str<'a>(v: &'a Value, key: &str) -> Result<&'a str, String> { v.get(key) .and_then(|x| x.as_str()) @@ -283,10 +507,7 @@ mod tests { let id = session_new(None); assert!(!id.is_empty()); assert!(session_set(&id, "username", json!("admin"))); - assert_eq!( - session_get(&id, "username"), - Some(json!("admin")) - ); + assert_eq!(session_get(&id, "username"), Some(json!("admin"))); assert!(session_del(&id, "username")); assert_eq!(session_get(&id, "username"), None); assert!(session_destroy(&id)); @@ -305,7 +526,6 @@ mod tests { let sid = r["session_id"].as_str().unwrap().to_string(); let check = abi_auth_check(&json!({ "session_id": sid })).unwrap(); assert_eq!(check["ok"].as_bool(), Some(true)); - assert_eq!(check["username"], "admin"); let bad = abi_auth_login(&json!({ "username": "admin", @@ -315,39 +535,64 @@ mod tests { .unwrap(); assert_eq!(bad["ok"].as_bool(), Some(false)); - // Chinese column headers also work. - let zh = abi_auth_login(&json!({ - "username": "站长", - "password": "pw123", - "users": users(), - })) - .unwrap(); - assert_eq!(zh["ok"].as_bool(), Some(true)); - let out = abi_auth_logout(&json!({ "session_id": sid })).unwrap(); assert_eq!(out["ok"].as_bool(), Some(true)); - let check2 = abi_auth_check(&json!({ "session_id": sid })).unwrap(); - assert_eq!(check2["ok"].as_bool(), Some(false)); + } + + #[test] + fn csrf_roundtrip() { + reset(3600); + let id = session_new(None); + let t = csrf_for(&id).unwrap(); + assert!(validate_csrf(&id, &t)); + assert!(!validate_csrf(&id, "bad")); } #[test] fn cookie_roundtrip() { reset(3600); let id = session_new(None); - let cookie = session_cookie(&id, 3600); + let cookie = session_cookie(&id, 3600, false); assert!(cookie.starts_with("marqdo_sid=")); - let got = session_id_from_cookie(Some(&format!( - "theme=light; {cookie}" - ))); + let got = session_id_from_cookie(Some(&format!("theme=light; {cookie}"))); assert_eq!(got.as_deref(), Some(id.as_str())); - assert_eq!(session_id_from_cookie(Some("theme=light")), None); - assert_eq!(session_id_from_cookie(None), None); } #[test] - fn check_credentials_missing() { - assert!(check_credentials(&json!(null), "a", "b").is_none()); - assert!(check_credentials(&json!("x"), "a", "b").is_none()); - assert!(check_credentials(&json!([]), "a", "b").is_none()); + fn hashed_password_login() { + reset(3600); + let hash = password::hash_password("secret").unwrap(); + let users = json!([{ "username": "admin", "password": hash }]); + let r = abi_auth_login(&json!({ + "username": "admin", + "password": "secret", + "users": users, + })) + .unwrap(); + assert_eq!(r["ok"].as_bool(), Some(true)); + } + + #[test] + fn session_persists_in_sqlite() { + let dir = std::env::temp_dir().join(format!("marqdo_web_sess_{}", std::process::id())); + let _ = std::fs::create_dir_all(&dir); + let path = dir.join("persist.db"); + let _ = std::fs::remove_file(&path); + let url = format!("sqlite:{}", path.display()); + configure(Config { + db_url: Some(url.clone()), + ttl_sec: 3600, + cookie_secure: false, + }); + let id = session_new(None); + assert!(session_set(&id, "theme", json!("dark"))); + // Re-configure (simulates listen restart with same db). + configure(Config { + db_url: Some(url), + ttl_sec: 3600, + cookie_secure: false, + }); + assert_eq!(session_get(&id, "theme"), Some(json!("dark"))); + let _ = std::fs::remove_dir_all(dir); } } diff --git a/src/lex/mod.rs b/src/lex/mod.rs index 9a89201..5b99c9a 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs @@ -293,4 +293,30 @@ mod tests { "expected fence body as Code, got {lines:?}" ); } + + #[test] + fn star_line_classification_is_star_terminated() { + // A `*…*` Marqdo statement line must END with `*` to be treated as code. + // If it accidentally ends with a backtick (e.g. `table="t"`), the line + // falls back to narrative Comment and swallows the following paragraph. + // Regression: `store.count` calls after a blank line were being dropped. + let star_term = "*c = > `store`.count table=\"t\"*"; + assert!( + is_marqdo_star_line(star_term), + "star-terminated assignment must be code" + ); + assert_eq!(classify_line(star_term), LineKind::Code); + + let backtick_term = "*c = > `store`.count table=\"t\"`"; + assert!( + !is_marqdo_star_line(backtick_term), + "backtick-terminated assignment must NOT be marqdo star line" + ); + assert_eq!(classify_line(backtick_term), LineKind::Comment); + + // In paragraph context the misplaced line must not stay Code. + let src = "> `store`.insert table=t rows=`数据`\n\n*c = > `store`.count table=\"t\"`\n> print text=count1-done\n"; + let lines = classify_source(src); + assert_eq!(lines[2].kind, LineKind::Comment, "bad terminator → comment"); + } } diff --git a/tests/ext/web-db-w2-smoke.mq.md b/tests/ext/web-db-w2-smoke.mq.md new file mode 100644 index 0000000..3c8b38a --- /dev/null +++ b/tests/ext/web-db-w2-smoke.mq.md @@ -0,0 +1,146 @@ +--- +title: web db W2 smoke — 事务 / 分页 / 原生查询 / 计数 / 查询表达力 +import web:ext/web/web.mq.md +import json:lib/json.mq.md +--- + +# main + +*store = > web.db url="sqlite:web-fixtures/data/w2-smoke.db"* + +`字段` = + +| 字段 | 类型 | 可空 | +|------|------|------| +| title | text | 否 | +| body | text | 是 | +| views | int | 是 | + +> `store`.init name=posts fields=`字段` +> `store`.exec sql="DELETE FROM posts" + +`数据` = + +| title | body | views | +|-------|------|-------| +| Alpha | first | 10 | +| Beta | second | 20 | +| Gamma | third | 30 | +| Delta | fourth | 40 | + +> `store`.insert table=posts rows=`数据` + +*c = > `store`.count table="posts"* +1. `c` == 4 + > print text=count-ok +2. * + > print text=count-fail + +`视图条件` = + +| 字段 | 操作 | 值 | +|------|------|-----| +| views | = | 30 | + +*c2 = > `store`.count table="posts" where=`视图条件`* +1. `c2` == 1 + > print text=count-where-ok +2. * + > print text=count-where-fail + +*page1 = > `store`.paginate table="posts" limit=2 跳过=0* +*total1 = page1[^total]* +*p1r = page1[^rows]* +*n1 = > len value=`p1r`* +1. `total1` == 4 +1. `n1` == 2 + > print text=page-ok +2. * + > print text=page-fail + +*page2 = > `store`.paginate table="posts" limit=2 跳过=2* +*p2r = page2[^rows]* +*n2 = > len value=`p2r`* +1. `n2` == 2 + > print text=page2-ok +2. * + > print text=page2-fail + +`in条件` = + +| 字段 | 操作 | 值 | +|------|------|-----| +| title | in | "Alpha,Beta" | + +*in_rows = > `store`.select table="posts" where=`in条件` limit=10* +*nin = > len value=`in_rows`* +1. `nin` == 2 + > print text=in-ok +2. * + > print text=in-fail + +`between条件` = + +| 字段 | 操作 | 值 | +|------|------|-----| +| views | between | "20,35" | + +*bt_rows = > `store`.select table="posts" where=`between条件` limit=10* +*nbt = > len value=`bt_rows`* +1. `nbt` == 2 + > print text=between-ok +2. * + > print text=between-fail + +`or条件` = + +| 字段 | 操作 | 值 | 或 | +|------|------|-----|-----| +| title | = | Alpha | | +| title | = | Gamma | 是 | + +*or_rows = > `store`.select table="posts" where=`or条件` limit=10* +*nor = > len value=`or_rows`* +1. `nor` == 2 + > print text=or-ok +2. * + > print text=or-fail + +*agg = > `store`.query sql="SELECT title, views FROM posts WHERE views >= 30 ORDER BY views DESC"* +*nagg = agg[^count]* +1. `nagg` == 2 + > print text=query-ok +2. * + > print text=query-fail + +*txn = > `store`.事务* +`更多` = + +| title | body | views | +|-------|------|-------| +| Epsilon | fifth | 50 | + +> `txn`.insert table=posts rows=`更多` +> `txn`.提交 + +*c5 = > `store`.count table="posts"* +1. `c5` == 5 + > print text=txn-commit-ok +2. * + > print text=txn-commit-fail + +*txn2 = > `store`.事务* +`更多2` = + +| title | body | views | +|-------|------|-------| +| Zeta | sixth | 60 | + +> `txn2`.insert table=posts rows=`更多2` +> `txn2`.回滚 + +*c6 = > `store`.count table="posts"* +1. `c6` == 5 + > print text=txn-rollback-ok +2. * + > print text=txn-rollback-fail diff --git a/tests/ext/web-middleware-live-server.mq.md b/tests/ext/web-middleware-live-server.mq.md new file mode 100644 index 0000000..756a746 --- /dev/null +++ b/tests/ext/web-middleware-live-server.mq.md @@ -0,0 +1,62 @@ +--- +title: web middleware live server +description: Offline-assembled app that listens with CORS/security/compress/body_limit/json. +import web:ext/web/web.mq.md +import plugin:lib/plugin.mq.md +import sys:lib/sys.mq.md +--- + +# main + +<!-- load web plugin so ABI primitives are registered --> + +*p = > plugin.native_path name="web"* +1. `p` + > plugin.load path=`p` +2. * + > sys.exit code=1 + +`schema` = + +| 字段 | 类型 | +|------|------| +| title | text | +| body | text | + +*store = > web.db url="sqlite:web-fixtures/data/mw-live.db"* +> `store`.init name=articles fields=`schema` +> `store`.exec sql="DELETE FROM articles" + +`rows` = + +| title | body | +|-------|------| +| Hello | first post | + +> `store`.insert table=articles rows=`rows` + +*pg = > web.page title="live"* +*app = > web.app page=`pg` db=`store` port=18099* + +`cors` = + +| 允许来源 | 方法 | 头 | 暴露头 | 凭证 | +|----------|------|----|--------|------| +| https://a.example | GET,POST | Content-Type | X-Total | true | + +`sec` = + +| 头 | 值 | +|----|----| +| X-Frame-Options | DENY | +| X-Content-Type-Options | nosniff | + +`api` = + +| 路径 | 方法 | 表 | 条件 | 排序 | 上限 | +|------|------|----|----|----|------| +| /api/posts | GET | articles | | | 10 | +| /api/publish | POST | articles | | | 10 | + +*app = > `app`.configure cors=`cors` security=`sec` compress=True body_limit=1000 json=`api`* +> `app`.listen diff --git a/tests/ext/web-middleware-smoke.mq.md b/tests/ext/web-middleware-smoke.mq.md new file mode 100644 index 0000000..8175c73 --- /dev/null +++ b/tests/ext/web-middleware-smoke.mq.md @@ -0,0 +1,147 @@ +--- +title: web middleware smoke +description: Offline assemble of CORS/security/compress/json middleware (no listen). +import web:ext/web/web.mq.md +import plugin:lib/plugin.mq.md +import sys:lib/sys.mq.md +--- + +# main + +<!-- load web plugin so ABI primitives are registered --> + +*p = > plugin.native_path name="web"* +1. `p` + > plugin.load path=`p` +2. * + > sys.exit code=1 + +<!-- CORS table: |允许来源|方法|头|暴露头|凭证| --> + +`cors` = + +| 允许来源 | 方法 | 头 | 暴露头 | 凭证 | +|----------|------|----|--------|------| +| https://a.example | GET,POST | Content-Type,Authorization | X-Total | true | +| https://b.example | GET | Content-Type | | | + +<!-- security table: |头|值| --> + +`sec` = + +| 头 | 值 | +|----|----| +| X-Frame-Options | DENY | +| X-Content-Type-Options | nosniff | +| Content-Security-Policy | default-src 'self' | + +<!-- json routes table: |路径|方法|表|条件|排序|上限| --> + +`api` = + +| 路径 | 方法 | 表 | 条件 | 排序 | 上限 | +|------|------|----|----|----|------| +| /api/posts | GET | articles | | "-created_at" | 20 | +| /api/publish | POST | articles | | | 10 | + +<!-- assemble via app.configure --> + +*pg = > web.page title="mw"* +*app = > web.app page=`pg`* +*app = > `app`.configure cors=`cors` security=`sec` compress=True body_limit=1048576 json=`api`* + +*mw = app[^middleware]* + +<!-- cors --> + +*cobj = mw[^cors]* +*origins = cobj[^allow_origins]* +*o0 = origins[^1]* +1. `o0` == "https://a.example" + > print text=cors-origin-ok +2. * + > print text=cors-origin-fail + +*methods = cobj[^methods]* +*m1 = methods[^2]* +1. `m1` == "POST" + > print text=cors-method-ok +2. * + > print text=cors-method-fail + +*expose = cobj[^expose_headers]* +*e0 = expose[^1]* +1. `e0` == "X-Total" + > print text=cors-expose-ok +2. * + > print text=cors-expose-fail + +*cred = cobj[^credentials]* +1. `cred` + > print text=cors-cred-ok +2. * + > print text=cors-cred-fail + +<!-- security --> + +*secmap = mw[^security]* +*frame = secmap[^X-Frame-Options]* +1. `frame` == "DENY" + > print text=security-frame-ok +2. * + > print text=security-frame-fail + +*csp = secmap[^Content-Security-Policy]* +1. `csp` == "default-src 'self'" + > print text=security-csp-ok +2. * + > print text=security-csp-fail + +<!-- compress / body_limit --> + +*comp = mw[^compress]* +1. `comp` + > print text=compress-ok +2. * + > print text=compress-fail + +*bl = mw[^body_limit]* +1. `bl` == 1048576 + > print text=body-limit-ok +2. * + > print text=body-limit-fail + +<!-- json routes --> + +*jmap = mw[^json_routes]* +*posts = jmap[^api/posts]* +*pok = posts[^method]* +1. `pok` == "GET" + > print text=json-get-ok +2. * + > print text=json-get-fail + +*ptable = posts[^table]* +1. `ptable` == "articles" + > print text=json-table-ok +2. * + > print text=json-table-fail + +*porder = posts[^order]* +1. `porder` == "-created_at" + > print text=json-order-ok +2. * + > print text=json-order-fail + +*plimit = posts[^limit]* +1. `plimit` == 20 + > print text=json-limit-ok +2. * + > print text=json-limit-fail + +*pub = jmap[^api/publish]* +*pmethod = pub[^method]* +1. `pmethod` == "POST" + > print text=json-post-ok +2. * + > print text=json-post-fail diff --git a/tests/ext/web-security-smoke.mq.md b/tests/ext/web-security-smoke.mq.md new file mode 100644 index 0000000..4d70dca --- /dev/null +++ b/tests/ext/web-security-smoke.mq.md @@ -0,0 +1,48 @@ +--- +title: web security smoke (W3) +description: Password hash ABI + legacy plaintext login (offline). +import web:ext/web/web.mq.md +import plugin:lib/plugin.mq.md +import sys:lib/sys.mq.md +--- + +# main + +<!-- load web plugin --> + +*p = > plugin.native_path name="web"* +1. `p` + > plugin.load path=`p` +2. * + > sys.exit code=1 + +<!-- argon2 hash must be non-empty PHC string --> + +*hash = > web_password_hash password="secret"* +*h = hash[^hash]* +1. `h` != "" + > print text=hash-ok +2. * + > print text=hash-fail + +<!-- legacy plaintext login still works for dev/gold --> + +`users` = + +| 行 | 用户 | 密码 | +|----|------|------| +| 1 | admin | secret | + +*login = > web_auth_login username="admin" password="secret" users=`users` session_ttl=120* +*lok = login[^ok]* +1. `lok` + > print text=auth-login-ok +2. * + > print text=auth-login-fail + +*bad = > web_auth_login username="admin" password="wrong" users=`users` session_ttl=120* +*bok = bad[^ok]* +1. `bok` + > print text=auth-bad-should-fail +2. * + > print text=auth-bad-reject-ok diff --git a/tests/gold.rs b/tests/gold.rs index d73722b..2b9e152 100644 --- a/tests/gold.rs +++ b/tests/gold.rs @@ -1542,6 +1542,219 @@ custom-ok", ); } +#[test] +fn ext_web_middleware_smoke() { + let status = Command::new("cargo") + .args(["build", "-p", "marqdo_plugin_web"]) + .current_dir(env!("CARGO_MANIFEST_DIR")) + .status() + .expect("build web plugin"); + assert!(status.success(), "failed to build marqdo_plugin_web"); + assert_out( + "tests/ext/web-middleware-smoke.mq.md", + "cors-origin-ok +cors-method-ok +cors-expose-ok +cors-cred-ok +security-frame-ok +security-csp-ok +compress-ok +body-limit-ok +json-get-ok +json-table-ok +json-order-ok +json-limit-ok +json-post-ok", + ); +} + +#[test] +fn ext_web_middleware_live() { + // Boots a real HTTP server and verifies CORS / security headers / + // gzip / JSON API / body limit over the wire. + let status = Command::new("cargo") + .args(["build", "-p", "marqdo_plugin_web"]) + .current_dir(env!("CARGO_MANIFEST_DIR")) + .status() + .expect("build web plugin"); + assert!(status.success(), "failed to build marqdo_plugin_web"); + + let script = "tests/ext/web-middleware-live-server.mq.md"; + let bin = env!("CARGO_BIN_EXE_marqdo"); + let mut child = Command::new(bin) + .args(["run", script]) + .current_dir(env!("CARGO_MANIFEST_DIR")) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("spawn live server"); + + // Wait for the listener to accept connections (up to ~10s). + let base = "http://127.0.0.1:18099"; + let ready = std::time::Instant::now(); + loop { + if ready.elapsed().as_secs() > 10 { + let _ = child.kill(); + panic!("live server did not start in time"); + } + let out = Command::new("curl") + .args(["-s", "-o", "/dev/null", "-w", "%{http_code}", &format!("{base}/")]) + .output() + .expect("curl probe"); + if String::from_utf8_lossy(&out.stdout) == "200" { + break; + } + std::thread::sleep(std::time::Duration::from_millis(300)); + } + + let code = |args: &[&str]| { + Command::new("curl") + .args(args) + .output() + .expect("curl") + }; + + // JSON API: rows are served as application/json. + let out = code(&["-s", &format!("{base}/api/posts")]); + let body = String::from_utf8_lossy(&out.stdout); + assert!( + body.contains("\"title\":\"Hello\"") && body.contains("\"rows\""), + "json api body={body}" + ); + + // CORS on the JSON endpoint (matching origin → allow headers). + let out = code(&[ + "-s", "-D", "-", "-o", "/dev/null", &format!("{base}/api/posts"), + "-H", "Origin: https://a.example", + ]); + let headers = String::from_utf8_lossy(&out.stdout); + assert!( + headers.contains("access-control-allow-origin: https://a.example"), + "cors allow-origin headers={headers}" + ); + assert!( + headers.contains("access-control-allow-credentials: true"), + "cors credentials headers={headers}" + ); + assert!( + headers.contains("access-control-expose-headers: x-total"), + "cors expose headers={headers}" + ); + + // OPTIONS preflight. + let out = code(&[ + "-s", "-D", "-", "-o", "/dev/null", "-X", "OPTIONS", + &format!("{base}/api/posts"), + "-H", "Origin: https://a.example", + "-H", "Access-Control-Request-Method: GET", + ]); + let headers = String::from_utf8_lossy(&out.stdout); + assert!( + headers.contains("access-control-allow-methods: GET"), + "cors preflight headers={headers}" + ); + assert!( + headers.contains("access-control-allow-origin"), + "cors preflight origin headers={headers}" + ); + + // Security response headers on a page render. + let out = code(&["-s", "-D", "-", "-o", "/dev/null", &format!("{base}/")]); + let headers = String::from_utf8_lossy(&out.stdout); + assert!( + headers.contains("x-frame-options: DENY"), + "security headers={headers}" + ); + assert!( + headers.contains("x-content-type-options: nosniff"), + "security headers={headers}" + ); + + // gzip compression. + let out = code(&[ + "-s", "-D", "-", "-o", "/dev/null", &format!("{base}/api/posts"), + "-H", "Accept-Encoding: gzip", + ]); + let headers = String::from_utf8_lossy(&out.stdout); + assert!( + headers.contains("content-encoding: gzip"), + "gzip headers={headers}" + ); + + // Body limit: small POST ok, large POST → 413. + let out = code(&[ + "-s", "-o", "/dev/null", "-w", "%{http_code}", "-X", "POST", + &format!("{base}/api/publish"), "-H", "Content-Type: application/json", + "-d", r#"{"title":"t1","body":"b1"}"#, + ]); + assert_eq!( + String::from_utf8_lossy(&out.stdout).trim(), + "200", + "small POST should pass body limit" + ); + let big = "x".repeat(2000); + let out = code(&[ + "-s", "-o", "/dev/null", "-w", "%{http_code}", "-X", "POST", + &format!("{base}/api/publish"), "-H", "Content-Type: application/json", + "-d", &big, + ]); + assert_eq!( + String::from_utf8_lossy(&out.stdout).trim(), + "413", + "large POST should exceed body limit" + ); + + let _ = child.kill(); + let _ = child.wait(); +} + +#[test] +fn ext_web_security_smoke() { + let status = Command::new("cargo") + .args(["build", "-p", "marqdo_plugin_web"]) + .current_dir(env!("CARGO_MANIFEST_DIR")) + .status() + .expect("build web plugin"); + assert!(status.success(), "failed to build marqdo_plugin_web"); + assert_out( + "tests/ext/web-security-smoke.mq.md", + "hash-ok +auth-login-ok +auth-bad-reject-ok", + ); +} + +#[test] +fn ext_web_db_w2_smoke() { + // W2 data layer: transactions, connection pooling, pagination, query + // expressiveness (IN/BETWEEN/OR), and row counting. + let status = Command::new("cargo") + .args(["build", "-p", "marqdo_plugin_web"]) + .current_dir(env!("CARGO_MANIFEST_DIR")) + .status() + .expect("build web plugin"); + assert!(status.success(), "failed to build marqdo_plugin_web"); + + let db = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/ext/web-fixtures/data/w2-smoke.db"); + for suffix in ["", "-wal", "-shm"] { + let _ = std::fs::remove_file(format!("{}{}", db.display(), suffix)); + } + assert_out( + "tests/ext/web-db-w2-smoke.mq.md", + "count-ok +count-where-ok +page-ok +page2-ok +in-ok +between-ok +or-ok +query-ok +txn-commit-ok +txn-rollback-ok", + ); +} + #[test] fn ext_quantum_bell_smoke() { let status = Command::new("cargo")