ext/web W1–W3: middleware, data layer, and security hardening
Deliver the interrupted network extension backlog: declarative middleware (CORS, security headers, gzip, JSON API), SQLite pool/transactions/pagination, and W3 security (argon2 passwords, CSRF on forms/login, persistent sessions, CSPRNG ids, login rate limiting). Adds gold smokes and author-facing hash_password on ext/web. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
da9fc75b1e
commit
be6223f1fb
22 files changed
+3081
-343
No files matched your search
+153
-7
@@ -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
|
||||
|
||||
|
||||
+155
-12
@@ -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
|
||||
|
||||
|
||||
Reference in new issue
Block a user