dsh-think-skin 1.0.1 → 1.0.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.
Files changed (3) hide show
  1. package/README.md +4 -0
  2. package/index.js +104 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -7,6 +7,8 @@ more than the answer. This replaces its icon and its copy, and keeps the timer.
7
7
 
8
8
  ![The row, reskinned](assets/cover.gif)
9
9
 
10
+ ▶ **[宣传片 · B 站](https://www.bilibili.com/video/BV1Layaz2Em7)** (1 分 45 秒,三套皮肤 + 安装全过程)
11
+
10
12
  ---
11
13
 
12
14
  ## What it does
@@ -35,6 +37,8 @@ more than the answer. This replaces its icon and its copy, and keeps the timer.
35
37
 
36
38
  ```bash
37
39
  dsh plugin add github:Jestions-dev/dsh-think-skin
40
+ # 或从 npm:
41
+ dsh plugin add dsh-think-skin
38
42
  ```
39
43
 
40
44
  Then restart DSH once.
package/index.js CHANGED
@@ -41,6 +41,72 @@ const MAX_IMAGE = 32 * 1024 * 1024
41
41
  /** 套组文件(含 base64 图片)可以有几十 MB。 */
42
42
  const MAX_PACK_FILE = 192 * 1024 * 1024
43
43
 
44
+ // ---------------------------------------------------------------- access gate
45
+
46
+ /**
47
+ * 这些路由挂在 DSH 自己的 web server 上,任何网页都能向它发请求——所以必须
48
+ * 自己确认请求来自 DSH 的页面,而不是别的标签页。
49
+ *
50
+ * 判断顺序:
51
+ * 1. Sec-Fetch-Site 由浏览器附加,页面 JS 改不了:cross-site / same-site 直接拒。
52
+ * 2. 有 Origin 时,它的 host 必须和 Host 头一致(同源)。
53
+ * 3. 两个头都没有,说明不是浏览器(curl、脚本),那是本机主人,放行。
54
+ */
55
+ function originAllowed(get) {
56
+ const site = String(get('sec-fetch-site') || '').toLowerCase()
57
+ if (site === 'cross-site' || site === 'same-site') return false
58
+
59
+ const origin = get('origin')
60
+ if (origin !== undefined && origin !== null && origin !== '') {
61
+ try {
62
+ return new URL(String(origin)).host === String(get('host') || '')
63
+ } catch {
64
+ return false
65
+ }
66
+ }
67
+ return true
68
+ }
69
+
70
+ /** 本机回环、内网、云元数据地址——导入下载器一律不碰。 */
71
+ function isPrivateHost(hostname) {
72
+ const host = String(hostname).toLowerCase().replace(/^\[|\]$/g, '')
73
+ if (host === '' || host === 'localhost' || host.endsWith('.localhost') || host.endsWith('.local')) {
74
+ return true
75
+ }
76
+
77
+ const v4 = host.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
78
+ if (v4 !== null) {
79
+ const a = Number(v4[1])
80
+ const b = Number(v4[2])
81
+ if (a === 0 || a === 127 || a === 10) return true
82
+ if (a === 169 && b === 254) return true // link-local / 云元数据
83
+ if (a === 172 && b >= 16 && b <= 31) return true
84
+ if (a === 192 && b === 168) return true
85
+ if (a === 100 && b >= 64 && b <= 127) return true // CGNAT
86
+ return false
87
+ }
88
+
89
+ if (host.includes(':')) {
90
+ if (host === '::1' || host === '::') return true
91
+ if (host.startsWith('fc') || host.startsWith('fd')) return true // 唯一本地
92
+ if (host.startsWith('fe80')) return true // 链路本地
93
+ }
94
+ return false
95
+ }
96
+
97
+ /** 只有 http/https、且不指向本机/内网的 URL 才能交给主机去下载。 */
98
+ function safeRemoteUrl(value) {
99
+ let url
100
+ try {
101
+ url = new URL(String(value))
102
+ } catch {
103
+ return null
104
+ }
105
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') return null
106
+ if (isPrivateHost(url.hostname)) return null
107
+ return url
108
+ }
109
+
44
110
  // ---------------------------------------------------------------- zip helpers
45
111
 
46
112
  const CRC_TABLE = (() => {
@@ -291,6 +357,12 @@ export function apply(ctx) {
291
357
  kind: 'prefix',
292
358
  path: ROUTE,
293
359
  async resource(request) {
360
+ if (!originAllowed((name) => request.headers.get(name))) {
361
+ return new Response('forbidden', {
362
+ status: 403,
363
+ headers: { 'content-type': 'text/plain; charset=utf-8' },
364
+ })
365
+ }
294
366
  let pathname
295
367
  try {
296
368
  pathname = new URL(request.url).pathname
@@ -336,6 +408,7 @@ export function apply(ctx) {
336
408
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
337
409
  res.end(message)
338
410
  }
411
+ if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
339
412
  if (req.method !== 'POST') return fail(405, 'method not allowed')
340
413
  try {
341
414
  const patch = JSON.parse((await readBody(req, MAX_BODY)).toString('utf8'))
@@ -424,6 +497,7 @@ export function apply(ctx) {
424
497
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
425
498
  res.end(message)
426
499
  }
500
+ if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
427
501
  if (req.method !== 'POST') return fail(405, 'method not allowed')
428
502
  try {
429
503
  const body = JSON.parse((await readBody(req, MAX_BODY)).toString('utf8'))
@@ -475,6 +549,7 @@ export function apply(ctx) {
475
549
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
476
550
  res.end(message)
477
551
  }
552
+ if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
478
553
  if (req.method !== 'POST') return fail(405, 'method not allowed')
479
554
  try {
480
555
  const url = new URL(req.url, 'http://local')
@@ -562,6 +637,7 @@ export function apply(ctx) {
562
637
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
563
638
  res.end(message)
564
639
  }
640
+ if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
565
641
  if (req.method !== 'POST') return fail(405, 'method not allowed')
566
642
  try {
567
643
  const body = JSON.parse((await readBody(req, MAX_BODY)).toString('utf8'))
@@ -652,6 +728,7 @@ export function apply(ctx) {
652
728
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
653
729
  res.end(message)
654
730
  }
731
+ if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
655
732
  if (req.method !== 'POST') return fail(405, 'method not allowed')
656
733
  let dir = null
657
734
  try {
@@ -672,14 +749,40 @@ export function apply(ctx) {
672
749
  if (request.file !== undefined) {
673
750
  document = request.file
674
751
  } else if (typeof request.url === 'string') {
752
+ // 主机替浏览器去下载:只允许 http/https,且不能落到本机或内网,
753
+ // 否则这个接口就成了「让别人的网页指挥我的机器访问内网」的通道。
754
+ const target = safeRemoteUrl(request.url)
755
+ if (target === null) {
756
+ return fail(400, '这个链接不行:只支持 http/https,且不能指向本机或内网地址。')
757
+ }
675
758
  let response
676
759
  try {
677
- response = await fetch(request.url, { redirect: 'follow' })
760
+ response = await fetch(target, {
761
+ redirect: 'follow',
762
+ signal: AbortSignal.timeout(30000),
763
+ })
678
764
  } catch (error) {
679
765
  return fail(400, '下载失败:' + String((error && error.message) || error))
680
766
  }
767
+ // 跟随重定向之后可能已经跳到内网,再验一次
768
+ let finalUrl = null
769
+ try {
770
+ finalUrl = new URL(response.url)
771
+ } catch {
772
+ finalUrl = null
773
+ }
774
+ if (finalUrl !== null && (finalUrl.protocol === 'http:' || finalUrl.protocol === 'https:')) {
775
+ if (isPrivateHost(finalUrl.hostname)) {
776
+ return fail(400, '这个链接不行:重定向到了本机或内网地址。')
777
+ }
778
+ }
681
779
  if (!response.ok) return fail(400, '下载失败:HTTP ' + response.status)
780
+ const declared = Number(response.headers.get('content-length') || '0')
781
+ if (Number.isFinite(declared) && declared > MAX_PACK_FILE) {
782
+ return fail(413, '文件太大,超过 192 MB。')
783
+ }
682
784
  const downloaded = Buffer.from(await response.arrayBuffer())
785
+ if (downloaded.length > MAX_PACK_FILE) return fail(413, '文件太大,超过 192 MB。')
683
786
 
684
787
  // 网盘分享页、登录页、错误页都会返回 HTML;直接说清楚,别让用户猜
685
788
  const head = downloaded.subarray(0, 512).toString('utf8').trim().toLowerCase()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-think-skin",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "把 DSH 的思考状态行换成你自己的图、自己的话:每张图带自己的文案池,逐字打出、高光扫过、渐变可调。支持套组导入导出与字符转圈。",
5
5
  "type": "module",
6
6
  "license": "MIT",