dsh-skill-english-reading 1.6.2 → 1.6.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.
Files changed (2) hide show
  1. package/index.js +52 -2
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -217,6 +217,7 @@ export function apply(ctx) {
217
217
  const modelService = () => getService('agentDefaultModel')
218
218
 
219
219
  const analyze = async (req, res) => {
220
+ if (!guard(req, res)) return
220
221
  if (req.method !== 'POST') return sendJson(res, 405, { error: 'method not allowed' })
221
222
 
222
223
  let payload
@@ -413,6 +414,7 @@ export function apply(ctx) {
413
414
  }
414
415
 
415
416
  const renderPdf = async (req, res) => {
417
+ if (!guard(req, res)) return
416
418
  if (req.method !== 'POST') return sendJson(res, 405, { error: 'method not allowed' })
417
419
 
418
420
  let payload
@@ -468,7 +470,8 @@ export function apply(ctx) {
468
470
  }
469
471
  }
470
472
 
471
- const health = async (_req, res) => {
473
+ const health = async (req, res) => {
474
+ if (!guard(req, res)) return
472
475
  const model = modelService()
473
476
  let selection = null
474
477
  try {
@@ -501,12 +504,58 @@ export function apply(ctx) {
501
504
 
502
505
  // Read per request, not at apply time: editing the stylesheet then only needs a
503
506
  // page refresh instead of a host restart.
504
- const style = (_req, res) => {
507
+ const style = (req, res) => {
508
+ if (!guard(req, res)) return
505
509
  res.writeHead(200, { 'content-type': 'text/css; charset=utf-8', 'cache-control': 'no-store' })
506
510
  res.end(readTemplateCss())
507
511
  }
508
512
 
513
+ /**
514
+ * These routes run on the same HTTP server as the GUI, so without a check any
515
+ * web page could POST to 127.0.0.1 and spend the user's model quota. Browsers
516
+ * mark the initiator: same-origin requests come from the panel, cross-site ones
517
+ * from any other page. Reject anything that is not ours.
518
+ * (Reported by the awesome-dsh-plugin reviewer when the entry was merged.)
519
+ */
520
+ // Set once the web server hands over its origin (see registerAll). Used to pin
521
+ // the exact Origin header the panel sends.
522
+ let expectedOrigin = null
523
+
524
+ const LOOPBACK = new Set(['127.0.0.1', 'localhost', '::1', '[::1]'])
525
+
526
+ const isSameOrigin = (req) => {
527
+ const headers = req.headers || {}
528
+
529
+ // 1. A browser marks who initiated the request.
530
+ const site = headers['sec-fetch-site']
531
+ if (site && site !== 'same-origin' && site !== 'none') return false
532
+
533
+ // 2. The Host must be loopback. Without this, a DNS-rebinding page is served
534
+ // from the attacker's domain and sends Origin/Host of that same domain,
535
+ // which would otherwise compare equal.
536
+ const host = String(headers.host || '')
537
+ const hostname = host.replace(/:\d+$/, '')
538
+ if (!LOOPBACK.has(hostname)) return false
539
+
540
+ // 3. If an Origin is present it must be the server's own origin.
541
+ const origin = headers.origin
542
+ if (!origin) return true
543
+ if (expectedOrigin) return origin === expectedOrigin
544
+ try {
545
+ return new URL(origin).host === host
546
+ } catch {
547
+ return false
548
+ }
549
+ }
550
+
551
+ const guard = (req, res) => {
552
+ if (isSameOrigin(req)) return true
553
+ sendJson(res, 403, { error: 'cross-site request blocked' })
554
+ return false
555
+ }
556
+
509
557
  const extract = async (req, res) => {
558
+ if (!guard(req, res)) return
510
559
  if (req.method !== 'POST') return sendJson(res, 405, { error: 'method not allowed' })
511
560
 
512
561
  let payload
@@ -568,6 +617,7 @@ export function apply(ctx) {
568
617
 
569
618
  const registerAll = (webServer, via) => {
570
619
  try {
620
+ expectedOrigin = webServer.getOrigin()
571
621
  for (const route of ROUTES) {
572
622
  ctx.effect(() => webServer.register({ kind: 'exact', path: route.path, handler: route.handler }))
573
623
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-skill-english-reading",
3
- "version": "1.6.2",
3
+ "version": "1.6.3",
4
4
  "type": "module",
5
5
  "description": "English reading analysis as a DSH plugin: turns English passages and exam papers into Chinese analysis with a print-ready A4 PDF. Registers the skill into the Harness skill catalog on install.",
6
6
  "exports": {