esa-pcg-cli 0.1.17-beta.astro.2 → 0.1.17-beta.astro.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -56,8 +56,9 @@ function signHeaders(cfg) {
56
56
  };
57
57
  }
58
58
 
59
- // ---- Gateway fetch(带超时) ----
60
- async function gwFetch(cfg, path, { method, body, timeoutMs = 5000 } = {}) {
59
+ // ---- Gateway fetch(带超时)----
60
+ // url 必须是 buildStorageUrl 产出的经校验绝对地址。
61
+ async function gwFetch(cfg, url, { method, body, timeoutMs = 5000 } = {}) {
61
62
  const headers = new Headers(signHeaders(cfg));
62
63
  if (body) {
63
64
  headers.set('Content-Type', 'application/json');
@@ -65,7 +66,7 @@ async function gwFetch(cfg, path, { method, body, timeoutMs = 5000 } = {}) {
65
66
  const controller = new AbortController();
66
67
  const timer = setTimeout(() => controller.abort(), timeoutMs);
67
68
  try {
68
- return await fetch(`${cfg.gatewayUrl}${path}`, {
69
+ return await fetch(url, {
69
70
  method,
70
71
  headers,
71
72
  body,
@@ -76,18 +77,57 @@ async function gwFetch(cfg, path, { method, body, timeoutMs = 5000 } = {}) {
76
77
  }
77
78
  }
78
79
 
79
- // ---- OSS key 构建 ----
80
- // 内联 @pcg/protocol buildOssKey,appId 对齐 gateway.ts buildKey:
81
- // appId = `${aliuid}/${routinename}`
82
- // 普通: ${appId}/${version}/${key}.cache
83
- // fetch: ${appId}/__fetch/${version}/${key}
84
- function buildOssKey(cfg, key, isFetch) {
85
- const appId = `${cfg.aliuid}/${cfg.routinename}`;
86
- const k = String(key).replace(/^\/+/, '');
87
- if (isFetch) {
88
- return `${appId}/__fetch/${cfg.version}/${k}`;
80
+ // ---- 存储地址构建 ----
81
+ // OSS key 形式内联 @pcg/protocol buildOssKey 的普通缓存分支,appId 对齐
82
+ // gateway.ts buildKey:`${aliuid}/${routinename}/${version}/${key}.cache`。
83
+ // 不处理 __fetch/ 命名空间:那是 OpenNext 区分 page/fetch 缓存的产物,
84
+ // unstorage 只有单一 key 命名空间,Nitro 缓存全部落在 .cache 路径。
85
+ //
86
+ // Gateway 直接拿 ctx.path 去掉 /storage/ 前缀当 OSS key,不做任何规范化,
87
+ // 因此 key 到 URL 的转换安全完全由客户端负责。两层防御:
88
+ //
89
+ // 1. 逐段 encodeURIComponent:Nitro/unstorage 的 key 常带 `:` 分隔符,也可能
90
+ // 带路径和 query。直接拼接时 `?` / `#` 会被当成 query/fragment 分隔符,
91
+ // 使 .cache 后缀与 key 后半段丢失,不同 key 归并到同一对象。
92
+ // 保留 `/` 作分隔符,使 OSS 上的层级与 Next.js 缓存布局一致。
93
+ // 2. 归一化围栏:编码不能阻止路径穿越——`..` 不在 encodeURIComponent
94
+ // 的转义集内。`../v002/x` 这类 key 会跳到另一个 version 命名空间,
95
+ // 且仍能通过 Gateway 的 `startsWith(appId + '/')` 租户校验,造成跳版本
96
+ // 缓存污染;`a/../b` 则会归一为 `b`,与 key `b` 撞到同一对象。
97
+ // 因此要求更强的不变量:构造出的地址必须在 URL 归一化后保持原样,
98
+ // 否则拒绝本次操作。这一条同时抵住穿越、key 归并,以及未预料的
99
+ // URL 归一化行为。
100
+ function encodeKeyPath(key) {
101
+ return String(key)
102
+ .replace(/^\/+/, '')
103
+ .split('/')
104
+ .map(segment => encodeURIComponent(segment))
105
+ .join('/');
106
+ }
107
+
108
+ function buildStorageUrl(cfg, key) {
109
+ const basePath = `/storage/${cfg.aliuid}/${cfg.routinename}/${cfg.version}/`;
110
+ const encoded = `${encodeKeyPath(key)}.cache`;
111
+ let base;
112
+ let target;
113
+ try {
114
+ base = new URL(`${cfg.gatewayUrl}${basePath}`);
115
+ target = new URL(`${base.href}${encoded}`);
116
+ } catch {
117
+ return null;
118
+ }
119
+ if (
120
+ target.origin !== base.origin ||
121
+ target.pathname !== `${base.pathname}${encoded}` ||
122
+ target.search !== '' ||
123
+ target.hash !== ''
124
+ ) {
125
+ return null;
89
126
  }
90
- return `${appId}/${cfg.version}/${k}.cache`;
127
+ return {
128
+ href: target.href,
129
+ ossKey: target.pathname.slice('/storage/'.length)
130
+ };
91
131
  }
92
132
 
93
133
  const GET_TIMEOUT = 5000;
@@ -118,9 +158,14 @@ export default function esaCacheDriver() {
118
158
  async getItem(key) {
119
159
  const cfg = getConfig();
120
160
  if (!cfg) return null;
121
- const ossKey = buildOssKey(cfg, key);
161
+ const target = buildStorageUrl(cfg, key);
162
+ if (!target) {
163
+ console.warn(`[esa-cache-driver] GET rejected unsafe key="${key}"`);
164
+ return null;
165
+ }
166
+ const { href, ossKey } = target;
122
167
  try {
123
- const res = await gwFetch(cfg, `/storage/${ossKey}`, {
168
+ const res = await gwFetch(cfg, href, {
124
169
  method: 'GET',
125
170
  timeoutMs: GET_TIMEOUT
126
171
  });
@@ -166,9 +211,14 @@ export default function esaCacheDriver() {
166
211
  async setItem(key, value) {
167
212
  const cfg = getConfig();
168
213
  if (!cfg) return;
169
- const ossKey = buildOssKey(cfg, key);
214
+ const target = buildStorageUrl(cfg, key);
215
+ if (!target) {
216
+ console.warn(`[esa-cache-driver] PUT rejected unsafe key="${key}"`);
217
+ return;
218
+ }
219
+ const { href, ossKey } = target;
170
220
  try {
171
- const res = await gwFetch(cfg, `/storage/${ossKey}`, {
221
+ const res = await gwFetch(cfg, href, {
172
222
  method: 'PUT',
173
223
  body: JSON.stringify({ lastModified: Date.now(), value }),
174
224
  timeoutMs: SET_TIMEOUT
@@ -200,9 +250,14 @@ export default function esaCacheDriver() {
200
250
  async removeItem(key) {
201
251
  const cfg = getConfig();
202
252
  if (!cfg) return;
203
- const ossKey = buildOssKey(cfg, key);
253
+ const target = buildStorageUrl(cfg, key);
254
+ if (!target) {
255
+ console.warn(`[esa-cache-driver] DELETE rejected unsafe key="${key}"`);
256
+ return;
257
+ }
258
+ const { href, ossKey } = target;
204
259
  try {
205
- const res = await gwFetch(cfg, `/storage/${ossKey}`, {
260
+ const res = await gwFetch(cfg, href, {
206
261
  method: 'DELETE',
207
262
  timeoutMs: DELETE_TIMEOUT
208
263
  });
@@ -15,11 +15,20 @@
15
15
  * NITRO_PRESET=esa-nitro-preset nuxt build
16
16
  * 或 nuxt.config: { nitro: { preset: 'esa-nitro-preset' } }
17
17
  */
18
- import { fileURLToPath } from 'node:url';
19
- import { dirname, resolve } from 'node:path';
20
-
21
- const __dirname = dirname(fileURLToPath(import.meta.url));
22
- const driverPath = resolve(__dirname, 'driver.mjs');
18
+ /**
19
+ * driver 的模块说明符。
20
+ *
21
+ * 必须用包说明符而不是 resolve(__dirname, 'driver.mjs') 的绝对路径:
22
+ * Nitro 会把这个值同时当作 import 说明符和 codegen 时的字面量写进
23
+ * .output/server 产物。用绝对路径时,import 虽然会被 Nitro 归一回
24
+ * 包说明符(因为包已在 node_modules 下),但构建机的绝对路径仍会以
25
+ * options 字面量的形式泄进产物(影响构建可复现性,且一旦今后 driver
26
+ * 开始读 options.driver 就会在容器里指向不存在的路径)。
27
+ *
28
+ * pcg 总是把 preset 物化到 <project>/node_modules/esa-nitro-preset/,
29
+ * 配合 package.json 的 exports,此说明符在构建期和运行期都能解析。
30
+ */
31
+ const DRIVER_SPECIFIER = 'esa-nitro-preset/driver.mjs';
23
32
 
24
33
  const esaPreset = {
25
34
  extends: 'node-server',
@@ -29,7 +38,7 @@ const esaPreset = {
29
38
  // 仅当用户未自定义 cache driver 时注入,尊重用户显式配置。
30
39
  nitro.options.storage = nitro.options.storage || {};
31
40
  if (!nitro.options.storage.cache?.driver) {
32
- nitro.options.storage.cache = { driver: driverPath };
41
+ nitro.options.storage.cache = { driver: DRIVER_SPECIFIER };
33
42
  }
34
43
  },
35
44
  // 预留钩子:路径 B 不需要写 config.json,留空
@@ -12,6 +12,9 @@
12
12
  "index.mjs",
13
13
  "driver.mjs"
14
14
  ],
15
+ "scripts": {
16
+ "test": "node smoke.mjs"
17
+ },
15
18
  "keywords": [
16
19
  "nitro",
17
20
  "nuxt",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esa-pcg-cli",
3
- "version": "0.1.17-beta.astro.2",
3
+ "version": "0.1.17-beta.astro.3",
4
4
  "description": "CLI for building and deploying Next.js and Astro apps onto pages-cache-gateway (PCG)",
5
5
  "keywords": [
6
6
  "next",