@linxin666/dsh-remote-bridge 0.4.20 → 0.4.22

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.
@@ -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
  /**