@linxin666/dsh-remote-bridge 0.4.20 → 0.4.21

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.
package/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createRequire } from "node:module";
2
2
  import { basename, dirname, isAbsolute, join, resolve, sep } from "node:path";
3
3
  import { createHash, createPrivateKey, createPublicKey, randomBytes, sign, timingSafeEqual, verify } from "node:crypto";
4
- import { chmod, mkdir, open, readFile, realpath, rename, stat } from "node:fs/promises";
4
+ import { chmod, lstat, mkdir, open, readFile, rename } from "node:fs/promises";
5
5
  import { deflateRawSync } from "node:zlib";
6
6
  import { homedir, networkInterfaces } from "node:os";
7
7
  import { isAbsolute as isAbsolute$1, join as join$1 } from "node:path/posix";
@@ -1298,8 +1298,9 @@ const CODE_UI_LIST_UNAVAILABLE = "ui-list-unavailable";
1298
1298
  const CODE_SESSION_NOT_FOLLOWABLE = "session-not-followable";
1299
1299
  /**
1300
1300
  * 该路径不在这次会话自己的持久 transcript 里:白名单只有两处来源,客户端自选的路径
1301
- * 一律不在其中。`realpath()` 解析结果不再等于该字符串时也回这一码——那个路径已经
1302
- * 指向别处,就不再是这次会话点过的那个文件。
1301
+ * 一律不在其中。该名字本身是个链接时也回这一码——它已被换成指向别处的一层门面,就
1302
+ * 不再是这次会话点过的那个文件。判据看的是条目自身(`lstat`),不比较 `realpath()`
1303
+ * 的解析结果:同一个文件的两种写法(Windows 8.3 短名、macOS `/var` 前缀)不等于换目标。
1303
1304
  */
1304
1305
  const CODE_PATH_NOT_IN_SESSION = "path-not-in-session";
1305
1306
  /**
@@ -2334,10 +2335,13 @@ function mediaTypeOfSignature(head) {
2334
2335
  *
2335
2336
  * The whitelist is the first decision and it is not this function's: a caller
2336
2337
  * hands over a path it already matched byte for byte. What is left is whether the
2337
- * file at that path is still the one the session named — `realpath()` has to
2338
- * return the same string, so a symlink pointing anywhere else is refused rather
2339
- * than followed — whether it is a regular file at all, and whether its own first
2340
- * bytes are one of the four image signatures. The extension is never consulted: a
2338
+ * name at that path is still the file the session named — the name itself must not
2339
+ * be a link, so a symlink pointing anywhere else is refused rather than followed —
2340
+ * whether it is a regular file at all, and whether its own first bytes are one of
2341
+ * the four image signatures. The link test reads the entry itself (`lstat`) rather
2342
+ * than resolving it: comparing `realpath()` against the stored string rejects every
2343
+ * 8.3 short name (Windows temp directories are `RUNNER~1`) and every macOS `/var`
2344
+ * path, where two spellings of one file are not a substitution attack. The extension is never consulted: a
2341
2345
  * `.png` that is not a PNG is refused, and a PNG named `.txt` is served.
2342
2346
  *
2343
2347
  * The answer's `image` is the reference the session's transcript named; this
@@ -2347,24 +2351,14 @@ function mediaTypeOfSignature(head) {
2347
2351
  */
2348
2352
  async function readAttachment(facts) {
2349
2353
  const path = facts.path;
2350
- let resolved;
2354
+ let size;
2351
2355
  try {
2352
- resolved = await realpath(path);
2353
- } catch {
2354
- return {
2356
+ const info = await lstat(path);
2357
+ if (info.isSymbolicLink()) return {
2355
2358
  ok: false,
2356
- code: CODE_ATTACHMENT_NOT_FOUND,
2357
- msg: "no file at " + path
2359
+ code: CODE_PATH_NOT_IN_SESSION,
2360
+ msg: path + " is a link, not the file this session named"
2358
2361
  };
2359
- }
2360
- if (resolved !== path) return {
2361
- ok: false,
2362
- code: CODE_PATH_NOT_IN_SESSION,
2363
- msg: path + " resolves to " + resolved + ", which this session never named"
2364
- };
2365
- let size;
2366
- try {
2367
- const info = await stat(path);
2368
2362
  if (!info.isFile()) return {
2369
2363
  ok: false,
2370
2364
  code: CODE_ATTACHMENT_NOT_FOUND,
@@ -75,10 +75,13 @@ export declare function mediaTypeOfSignature(head: Buffer): string | undefined;
75
75
  *
76
76
  * The whitelist is the first decision and it is not this function's: a caller
77
77
  * hands over a path it already matched byte for byte. What is left is whether the
78
- * file at that path is still the one the session named — `realpath()` has to
79
- * return the same string, so a symlink pointing anywhere else is refused rather
80
- * than followed — whether it is a regular file at all, and whether its own first
81
- * bytes are one of the four image signatures. The extension is never consulted: a
78
+ * name at that path is still the file the session named — the name itself must not
79
+ * be a link, so a symlink pointing anywhere else is refused rather than followed —
80
+ * whether it is a regular file at all, and whether its own first bytes are one of
81
+ * the four image signatures. The link test reads the entry itself (`lstat`) rather
82
+ * than resolving it: comparing `realpath()` against the stored string rejects every
83
+ * 8.3 short name (Windows temp directories are `RUNNER~1`) and every macOS `/var`
84
+ * path, where two spellings of one file are not a substitution attack. The extension is never consulted: a
82
85
  * `.png` that is not a PNG is refused, and a PNG named `.txt` is served.
83
86
  *
84
87
  * The answer's `image` is the reference the session's transcript named; this
@@ -501,8 +501,9 @@ export declare const CODE_UI_LIST_UNAVAILABLE = "ui-list-unavailable";
501
501
  export declare const CODE_SESSION_NOT_FOLLOWABLE = "session-not-followable";
502
502
  /**
503
503
  * 该路径不在这次会话自己的持久 transcript 里:白名单只有两处来源,客户端自选的路径
504
- * 一律不在其中。`realpath()` 解析结果不再等于该字符串时也回这一码——那个路径已经
505
- * 指向别处,就不再是这次会话点过的那个文件。
504
+ * 一律不在其中。该名字本身是个链接时也回这一码——它已被换成指向别处的一层门面,就
505
+ * 不再是这次会话点过的那个文件。判据看的是条目自身(`lstat`),不比较 `realpath()`
506
+ * 的解析结果:同一个文件的两种写法(Windows 8.3 短名、macOS `/var` 前缀)不等于换目标。
506
507
  */
507
508
  export declare const CODE_PATH_NOT_IN_SESSION = "path-not-in-session";
508
509
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@linxin666/dsh-remote-bridge",
3
3
  "description": "DSH remote bridge: the host half serves protocol v1 over one exact /ios/v1 WebSocket route — session roster, history baseline, prompting, live turn increments with tool activity, interrupt, and in-process agent approval — to a device paired by a one-shot bootstrap token and a P-256 challenge-response, with the token endpoint admitted only from this machine; the browser half adds one sidebar-foot panel that shows that same one-shot token as a QR symbol and draws an interactive card inside the tool row without answering it, and adds no wire surface",
4
- "version": "0.4.20",
4
+ "version": "0.4.21",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",
7
7
  "engines": {
@@ -28,7 +28,7 @@
28
28
  */
29
29
 
30
30
  import { isAbsolute } from 'node:path'
31
- import { readFile, realpath, stat } from 'node:fs/promises'
31
+ import { lstat, readFile } from 'node:fs/promises'
32
32
  import { FRAME_TOO_LARGE, MAX_FRAME_BYTES } from './envelope.ts'
33
33
  import { CODE_ATTACHMENT_NOT_FOUND, CODE_NOT_AN_IMAGE, CODE_PATH_NOT_IN_SESSION } from './protocol.ts'
34
34
 
@@ -238,10 +238,13 @@ export function mediaTypeOfSignature(head: Buffer): string | undefined {
238
238
  *
239
239
  * The whitelist is the first decision and it is not this function's: a caller
240
240
  * hands over a path it already matched byte for byte. What is left is whether the
241
- * file at that path is still the one the session named — `realpath()` has to
242
- * return the same string, so a symlink pointing anywhere else is refused rather
243
- * than followed — whether it is a regular file at all, and whether its own first
244
- * bytes are one of the four image signatures. The extension is never consulted: a
241
+ * name at that path is still the file the session named — the name itself must not
242
+ * be a link, so a symlink pointing anywhere else is refused rather than followed —
243
+ * whether it is a regular file at all, and whether its own first bytes are one of
244
+ * the four image signatures. The link test reads the entry itself (`lstat`) rather
245
+ * than resolving it: comparing `realpath()` against the stored string rejects every
246
+ * 8.3 short name (Windows temp directories are `RUNNER~1`) and every macOS `/var`
247
+ * path, where two spellings of one file are not a substitution attack. The extension is never consulted: a
245
248
  * `.png` that is not a PNG is refused, and a PNG named `.txt` is served.
246
249
  *
247
250
  * The answer's `image` is the reference the session's transcript named; this
@@ -251,23 +254,19 @@ export function mediaTypeOfSignature(head: Buffer): string | undefined {
251
254
  */
252
255
  export async function readAttachment(facts: AttachmentFacts): Promise<AttachmentRead> {
253
256
  const path = facts.path
254
- let resolved: string
255
- try {
256
- resolved = await realpath(path)
257
- } catch {
258
- // Gone, unreadable, or a parent that is not a directory: either way there is
259
- // no file at this path, which is the one thing `not-found` states.
260
- return { ok: false, code: CODE_ATTACHMENT_NOT_FOUND, msg: 'no file at ' + path }
261
- }
262
- if (resolved !== path) {
263
- return { ok: false, code: CODE_PATH_NOT_IN_SESSION, msg: path + ' resolves to ' + resolved + ', which this session never named' }
264
- }
265
257
  let size: number
266
258
  try {
267
- const info = await stat(path)
259
+ const info = await lstat(path)
260
+ // One syscall answers both questions: the entry is the file (not a link that
261
+ // stands in for one), and it carries the size the frame cap needs.
262
+ if (info.isSymbolicLink()) {
263
+ return { ok: false, code: CODE_PATH_NOT_IN_SESSION, msg: path + ' is a link, not the file this session named' }
264
+ }
268
265
  if (!info.isFile()) return { ok: false, code: CODE_ATTACHMENT_NOT_FOUND, msg: path + ' is not a regular file' }
269
266
  size = info.size
270
267
  } catch {
268
+ // Gone, unreadable, or a parent that is not a directory: either way there is
269
+ // no file at this path, which is the one thing `not-found` states.
271
270
  return { ok: false, code: CODE_ATTACHMENT_NOT_FOUND, msg: 'no file at ' + path }
272
271
  }
273
272
  // Base64 inflates by a third, so a file at or past the frame cap cannot fit one
@@ -582,8 +582,9 @@ export const CODE_UI_LIST_UNAVAILABLE = 'ui-list-unavailable'
582
582
  export const CODE_SESSION_NOT_FOLLOWABLE = 'session-not-followable'
583
583
  /**
584
584
  * 该路径不在这次会话自己的持久 transcript 里:白名单只有两处来源,客户端自选的路径
585
- * 一律不在其中。`realpath()` 解析结果不再等于该字符串时也回这一码——那个路径已经
586
- * 指向别处,就不再是这次会话点过的那个文件。
585
+ * 一律不在其中。该名字本身是个链接时也回这一码——它已被换成指向别处的一层门面,就
586
+ * 不再是这次会话点过的那个文件。判据看的是条目自身(`lstat`),不比较 `realpath()`
587
+ * 的解析结果:同一个文件的两种写法(Windows 8.3 短名、macOS `/var` 前缀)不等于换目标。
587
588
  */
588
589
  export const CODE_PATH_NOT_IN_SESSION = 'path-not-in-session'
589
590
  /**