@maiyunnet/kebab 9.26.1 → 9.26.2

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.
@@ -0,0 +1 @@
1
+ export declare function createPathGuard(exampleRoot: string): (candidate: string) => Promise<boolean>;
@@ -0,0 +1,17 @@
1
+ import * as fs from 'fs/promises';
2
+ import * as path from 'path';
3
+ export function createPathGuard(exampleRoot) {
4
+ const normalizedRoot = path.resolve(exampleRoot);
5
+ let root;
6
+ return async (candidate) => {
7
+ root ??= fs.realpath(normalizedRoot).catch(() => normalizedRoot);
8
+ try {
9
+ const relative = path.relative(await root, await fs.realpath(candidate));
10
+ return relative === '' || relative !== '..' &&
11
+ !relative.startsWith('..' + path.sep) && !path.isAbsolute(relative);
12
+ }
13
+ catch {
14
+ return true;
15
+ }
16
+ };
17
+ }
package/doc/kebab-rag.md CHANGED
@@ -10,6 +10,10 @@ quick-start.md
10
10
 
11
11
  也可以使用 pm2 启动项目,执行 `pm2 start npx --name "kebab" -- kebab` 即可。
12
12
 
13
+ 内置示例仅在全局 `conf/config.json` 的 `debug` 为布尔值 `true` 时开放。为 `false` 或未设置时,示例的 HTTP 请求返回 403,WebSocket 连接关闭;检查早于示例中间件、代理及静态资源处理。普通业务项目不受此限制。
14
+
15
+ 初始化命令默认设置 `debug: true`。生产环境应改为 `false` 并重载配置;开发环境开启示例时,仍应通过本机或受控开发网络访问,Host 限制不能替代身份认证。
16
+
13
17
  ## 目录
14
18
 
15
19
  项目初始化后,会生成一些基础文件和目录,介绍如下:
@@ -1759,7 +1763,7 @@ index/variables/VER.md
1759
1763
 
1760
1764
  # Variable: VER
1761
1765
 
1762
- > `const` **VER**: `"9.26.1"` = `'9.26.1'`
1766
+ > `const` **VER**: `"9.26.2"` = `'9.26.2'`
1763
1767
 
1764
1768
  Defined in: [index.ts:9](https://github.com/maiyunnet/kebab/blob/master/index.ts#L9)
1765
1769
 
@@ -138278,7 +138282,7 @@ sys/route/functions/getFormData.md
138278
138282
 
138279
138283
  > **getFormData**(`req`, `events?`, `limits?`): `Promise`\<`false` \| \{ `files`: `Record`\<`string`, [`IPostFile`](../../../index/interfaces/IPostFile.md) \| [`IPostFile`](../../../index/interfaces/IPostFile.md)[]\>; `post`: `Record`\<`string`, [`Json`](../../../index/type-aliases/Json.md)\>; \}\>
138280
138284
 
138281
- Defined in: [sys/route.ts:1104](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1104)
138285
+ Defined in: [sys/route.ts:1103](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1103)
138282
138286
 
138283
138287
  获取 formdata 的 post
138284
138288
 
@@ -138321,7 +138325,7 @@ sys/route/functions/getPost.md
138321
138325
 
138322
138326
  > **getPost**(`req`): `Promise`\<`false` \| \{ `input`: `string`; `post`: `Record`\<`string`, `any`\>; `raw`: `Record`\<`string`, `any`\>; \}\>
138323
138327
 
138324
- Defined in: [sys/route.ts:939](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L939)
138328
+ Defined in: [sys/route.ts:938](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L938)
138325
138329
 
138326
138330
  获取 post 对象(通常已自动获取),如果是文件上传(formdata)的情况则不获取
138327
138331
 
@@ -138439,7 +138443,7 @@ sys/route/functions/unlinkUploadFiles.md
138439
138443
 
138440
138444
  > **unlinkUploadFiles**(`cctr`): `Promise`\<`void`\>
138441
138445
 
138442
- Defined in: [sys/route.ts:897](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L897)
138446
+ Defined in: [sys/route.ts:896](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L896)
138443
138447
 
138444
138448
  删除本次请求所有已上传的临时文件
138445
138449
 
@@ -138468,7 +138472,7 @@ sys/route/functions/waitCtr.md
138468
138472
 
138469
138473
  > **waitCtr**(`cctr`): `Promise`\<`void`\>
138470
138474
 
138471
- Defined in: [sys/route.ts:917](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L917)
138475
+ Defined in: [sys/route.ts:916](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L916)
138472
138476
 
138473
138477
  等待异步任务结束,并删除临时文件,如果结束后还有事务没关闭,则会在本函数中打印控制台并且写入 log 文件
138474
138478
  此时其实已经给客户端返回了,此处等待不消耗客户端的等待时间
@@ -138522,7 +138526,7 @@ sys/route/interfaces/IFormDataError.md
138522
138526
 
138523
138527
  # Interface: IFormDataError
138524
138528
 
138525
- Defined in: [sys/route.ts:1059](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1059)
138529
+ Defined in: [sys/route.ts:1058](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1058)
138526
138530
 
138527
138531
  解析上传数据时的失败原因,只提供错误码和限制值,不带临时路径或异常对象
138528
138532
 
@@ -138532,7 +138536,7 @@ Defined in: [sys/route.ts:1059](https://github.com/maiyunnet/kebab/blob/master/s
138532
138536
 
138533
138537
  > **code**: `"INVALID_BOUNDARY"` \| `"FILE_TOO_LARGE"` \| `"TOTAL_TOO_LARGE"` \| `"FIELD_TOO_LARGE"` \| `"HEADER_TOO_LARGE"` \| `"TOO_MANY_PARTS"` \| `"FILE_TYPE"` \| `"TIMEOUT"` \| `"TEMP_FILE_ERROR"` \| `"REQUEST_ERROR"` \| `"REQUEST_ABORTED"`
138534
138538
 
138535
- Defined in: [sys/route.ts:1060](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1060)
138539
+ Defined in: [sys/route.ts:1059](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1059)
138536
138540
 
138537
138541
  ***
138538
138542
 
@@ -138540,7 +138544,7 @@ Defined in: [sys/route.ts:1060](https://github.com/maiyunnet/kebab/blob/master/s
138540
138544
 
138541
138545
  > `optional` **limit?**: `number`
138542
138546
 
138543
- Defined in: [sys/route.ts:1064](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1064)
138547
+ Defined in: [sys/route.ts:1063](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1063)
138544
138548
 
138545
138549
  超出的限制值:大小用字节,超时用毫秒,字段和文件数量用个
138546
138550
 
@@ -138555,7 +138559,7 @@ sys/route/interfaces/IFormDataEvents.md
138555
138559
 
138556
138560
  # Interface: IFormDataEvents
138557
138561
 
138558
- Defined in: [sys/route.ts:1068](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1068)
138562
+ Defined in: [sys/route.ts:1067](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1067)
138559
138563
 
138560
138564
  multipart 文件处理及失败通知
138561
138565
 
@@ -138565,7 +138569,7 @@ multipart 文件处理及失败通知
138565
138569
 
138566
138570
  > `optional` **onerror?**: (`error`) => `void`
138567
138571
 
138568
- Defined in: [sys/route.ts:1076](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1076)
138572
+ Defined in: [sys/route.ts:1075](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1075)
138569
138573
 
138570
138574
  首次失败时调用一次,getFormData 返回 false;若连接已断开,可能无法回复客户端
138571
138575
 
@@ -138585,7 +138589,7 @@ Defined in: [sys/route.ts:1076](https://github.com/maiyunnet/kebab/blob/master/s
138585
138589
 
138586
138590
  > `optional` **onfiledata?**: (`chunk`) => `void`
138587
138591
 
138588
- Defined in: [sys/route.ts:1072](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1072)
138592
+ Defined in: [sys/route.ts:1071](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1071)
138589
138593
 
138590
138594
  文件上传时触发,仅 start 返回 true 时触发
138591
138595
 
@@ -138605,7 +138609,7 @@ Defined in: [sys/route.ts:1072](https://github.com/maiyunnet/kebab/blob/master/s
138605
138609
 
138606
138610
  > `optional` **onfileend?**: () => `void`
138607
138611
 
138608
- Defined in: [sys/route.ts:1074](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1074)
138612
+ Defined in: [sys/route.ts:1073](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1073)
138609
138613
 
138610
138614
  文件上传结束时触发,仅 start 返回 true 时触发
138611
138615
 
@@ -138619,7 +138623,7 @@ Defined in: [sys/route.ts:1074](https://github.com/maiyunnet/kebab/blob/master/s
138619
138623
 
138620
138624
  > `optional` **onfilestart?**: (`name`) => `boolean` \| `undefined`
138621
138625
 
138622
- Defined in: [sys/route.ts:1070](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1070)
138626
+ Defined in: [sys/route.ts:1069](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1069)
138623
138627
 
138624
138628
  文件开始上传时触发,返回 true 则跳过该文件的保存
138625
138629
 
@@ -138644,7 +138648,7 @@ sys/route/interfaces/IFormDataLimits.md
138644
138648
 
138645
138649
  # Interface: IFormDataLimits
138646
138650
 
138647
- Defined in: [sys/route.ts:1080](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1080)
138651
+ Defined in: [sys/route.ts:1079](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1079)
138648
138652
 
138649
138653
  multipart 上传限制
138650
138654
 
@@ -138654,7 +138658,7 @@ multipart 上传限制
138654
138658
 
138655
138659
  > `optional` **allowedExts?**: `string`[]
138656
138660
 
138657
- Defined in: [sys/route.ts:1084](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1084)
138661
+ Defined in: [sys/route.ts:1083](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1083)
138658
138662
 
138659
138663
  允许的文件扩展名(含点号),如 ['.jpg', '.png', '.pdf']
138660
138664
 
@@ -138664,7 +138668,7 @@ Defined in: [sys/route.ts:1084](https://github.com/maiyunnet/kebab/blob/master/s
138664
138668
 
138665
138669
  > `optional` **maxFieldSize?**: `number`
138666
138670
 
138667
- Defined in: [sys/route.ts:1086](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1086)
138671
+ Defined in: [sys/route.ts:1085](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1085)
138668
138672
 
138669
138673
  单个字段(非文件)最大字节数,默认 1 MB
138670
138674
 
@@ -138674,7 +138678,7 @@ Defined in: [sys/route.ts:1086](https://github.com/maiyunnet/kebab/blob/master/s
138674
138678
 
138675
138679
  > `optional` **maxFileSize?**: `number`
138676
138680
 
138677
- Defined in: [sys/route.ts:1082](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1082)
138681
+ Defined in: [sys/route.ts:1081](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1081)
138678
138682
 
138679
138683
  单个文件最大字节数
138680
138684
 
@@ -138684,7 +138688,7 @@ Defined in: [sys/route.ts:1082](https://github.com/maiyunnet/kebab/blob/master/s
138684
138688
 
138685
138689
  > `optional` **maxHeaderSize?**: `number`
138686
138690
 
138687
- Defined in: [sys/route.ts:1090](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1090)
138691
+ Defined in: [sys/route.ts:1089](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1089)
138688
138692
 
138689
138693
  每个字段或文件的 multipart 头部最多多少字节,默认 16 KB
138690
138694
 
@@ -138694,7 +138698,7 @@ Defined in: [sys/route.ts:1090](https://github.com/maiyunnet/kebab/blob/master/s
138694
138698
 
138695
138699
  > `optional` **maxParts?**: `number`
138696
138700
 
138697
- Defined in: [sys/route.ts:1092](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1092)
138701
+ Defined in: [sys/route.ts:1091](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1091)
138698
138702
 
138699
138703
  multipart 字段与文件总数量,默认 1000
138700
138704
 
@@ -138704,7 +138708,7 @@ multipart 字段与文件总数量,默认 1000
138704
138708
 
138705
138709
  > `optional` **maxTotalSize?**: `number`
138706
138710
 
138707
- Defined in: [sys/route.ts:1088](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1088)
138711
+ Defined in: [sys/route.ts:1087](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1087)
138708
138712
 
138709
138713
  整体请求最大字节数,不设置或设为 0 则不限制
138710
138714
 
@@ -138714,6 +138718,6 @@ Defined in: [sys/route.ts:1088](https://github.com/maiyunnet/kebab/blob/master/s
138714
138718
 
138715
138719
  > `optional` **timeout?**: `number`
138716
138720
 
138717
- Defined in: [sys/route.ts:1094](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1094)
138721
+ Defined in: [sys/route.ts:1093](https://github.com/maiyunnet/kebab/blob/master/sys/route.ts#L1093)
138718
138722
 
138719
138723
  整体请求超时时间(毫秒),默认 5 分钟,设为 0 禁用超时
package/index.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * --- 本文件用来定义每个目录实体地址的常量 ---
5
5
  */
6
6
  /** --- 当前系统版本号 --- */
7
- export declare const VER = "9.26.1";
7
+ export declare const VER = "9.26.2";
8
8
  /** --- 框架根目录,以 / 结尾 --- */
9
9
  export declare const ROOT_PATH: string;
10
10
  /** --- 框架的 LIB,以 / 结尾 --- */
package/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export const VER = '9.26.1';
8
+ export const VER = '9.26.2';
9
9
  // --- 服务端用的路径 ---
10
10
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
11
11
  /** --- /xxx/xxx --- */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "9.26.1",
3
+ "version": "9.26.2",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [
package/sys/child.js CHANGED
@@ -16,6 +16,22 @@ import * as sCtr from '#kebab/sys/ctr.js';
16
16
  import * as kebab from '#kebab/index.js';
17
17
  // --- 初始化 ---
18
18
  import * as sRoute from '#kebab/sys/route.js';
19
+ import { createPathGuard } from '#kebab/abox/example.js';
20
+ /**
21
+ * --- 判断现有路径是否属于内置示例,符号链接按实际目标检查 ---
22
+ * @param candidate 已存在的目录或符号链接路径
23
+ * @returns 属于示例或无法确认目标时返回 true,由入口拒绝访问
24
+ */
25
+ const isExamplePath = createPathGuard(kebab.ROOT_PATH + 'www/example/');
26
+ /**
27
+ * --- 拒绝生产环境中的示例 HTTP 请求 ---
28
+ * @param res 响应对象
29
+ * @returns 无返回值
30
+ */
31
+ function rejectExample(res) {
32
+ lCore.writeHead(res, 403);
33
+ res.end('403 Forbidden');
34
+ }
19
35
  /** --- 10 秒往主线程发送一次心跳的 Timer --- */
20
36
  const hbTimer = setInterval(function () {
21
37
  if (!process.connected || !process.send) {
@@ -245,6 +261,12 @@ async function requestHandler(req, res, https) {
245
261
  res.end('403 Forbidden');
246
262
  return;
247
263
  }
264
+ // --- 配置来自 JSON,仅布尔值 true 开放内置示例 ---
265
+ const debug = lCore.globalConfig.debug;
266
+ if (debug !== true && await isExamplePath(vhost.real)) {
267
+ rejectExample(res);
268
+ return;
269
+ }
248
270
  /** --- 请求的路径部分,前导带 / 末尾不一定,用户怎么请求就是什么 --- */
249
271
  let path = uri.pathname ?? '/';
250
272
  if (path !== '/') {
@@ -269,6 +291,11 @@ async function requestHandler(req, res, https) {
269
291
  res.end(content);
270
292
  return;
271
293
  }
294
+ if (debug !== true && (stat.isDirectory() || stat.isSymbolicLink()) &&
295
+ await isExamplePath(vhost.real + now + item)) {
296
+ rejectExample(res);
297
+ return;
298
+ }
272
299
  if (stat.isDirectory()) {
273
300
  now += item + '/';
274
301
  // --- 判断是不是动态层 ---
@@ -387,6 +414,11 @@ async function upgradeHandler(req, socket, https, head) {
387
414
  socket.end();
388
415
  return;
389
416
  }
417
+ const debug = lCore.globalConfig.debug;
418
+ if (debug !== true && await isExamplePath(vhost.real)) {
419
+ socket.end();
420
+ return;
421
+ }
390
422
  /** --- 请求的路径部分,前导带 / 末尾不一定,用户怎么请求就是什么 --- */
391
423
  let path = uri.pathname ?? '/';
392
424
  if (path !== '/') {
@@ -407,6 +439,11 @@ async function upgradeHandler(req, socket, https, head) {
407
439
  socket.end();
408
440
  return;
409
441
  }
442
+ if (debug !== true && (stat.isDirectory() || stat.isSymbolicLink()) &&
443
+ await isExamplePath(vhost.real + now + item)) {
444
+ socket.end();
445
+ return;
446
+ }
410
447
  if (stat.isDirectory()) {
411
448
  now += item + '/';
412
449
  // --- 判断是不是动态层 ---
package/sys/ctr.js CHANGED
@@ -983,12 +983,9 @@ export class Ctr {
983
983
  if (!rtn) {
984
984
  return false;
985
985
  }
986
- for (const key in rtn.post) {
987
- this._post[key] = rtn.post[key];
988
- }
989
- for (const key in rtn.files) {
990
- this._files[key] = rtn.files[key];
991
- }
986
+ // --- 保留集合引用,特殊字段名也只写成自有属性 ---
987
+ Object.defineProperties(this._post, Object.getOwnPropertyDescriptors(rtn.post));
988
+ Object.defineProperties(this._files, Object.getOwnPropertyDescriptors(rtn.files));
992
989
  return true;
993
990
  }
994
991
  }
package/sys/route.js CHANGED
@@ -548,15 +548,14 @@ export async function run(data) {
548
548
  data.res.end('');
549
549
  return true;
550
550
  }
551
- // --- 检测 action 是否存在,以及排除内部方法 ---
552
- if (pathRight.startsWith('_') || pathRight === 'onUpgrade' || pathRight === 'onLoad' || pathRight === 'onData' || pathRight === 'onDrain' || pathRight === 'onEnd' || pathRight === 'onClose' || pathRight === 'setPrototype' || pathRight === 'getPrototype' || pathRight === 'getAuthorization') {
553
- // --- _ 开头的 action 是内部方法,不允许访问 ---
554
- respond404(data.res, config, path);
555
- return true;
556
- }
557
551
  pathRight = pathRight.replace(/-([a-zA-Z0-9])/g, function (t, t1) {
558
552
  return t1.toUpperCase();
559
553
  });
554
+ // --- 按实际调用名检查,框架基类及 Object 原型的成员不能作为 action ---
555
+ if (pathRight.startsWith('_') || pathRight in sCtr.Ctr.prototype) {
556
+ respond404(data.res, config, path);
557
+ return true;
558
+ }
560
559
  if ((cctr[pathRight] === undefined) || (typeof cctr[pathRight] !== 'function')) {
561
560
  respond404(data.res, config, path);
562
561
  return true;
@@ -837,7 +836,7 @@ function getWsCtrName(path) {
837
836
  */
838
837
  export async function unlinkUploadFiles(cctr) {
839
838
  const cfiles = cctr instanceof sCtr.Ctr ? cctr.getPrototype('_files') : cctr;
840
- for (const name in cfiles) {
839
+ for (const name of Object.keys(cfiles)) {
841
840
  let files = cfiles[name];
842
841
  if (!Array.isArray(files)) {
843
842
  files = [files];