dsh-zju-lecture-note 2.2.6 → 2.2.8

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.
@@ -5,6 +5,7 @@ Pure Python standard library implementation with zero external dependencies.
5
5
  """
6
6
 
7
7
  import http.client
8
+ import ipaddress
8
9
  import json
9
10
  import os
10
11
  import random
@@ -15,7 +16,7 @@ import time
15
16
  import urllib.error
16
17
  import urllib.parse
17
18
  import urllib.request
18
- from typing import Any, Dict, List, Optional
19
+ from typing import Any, Dict, List, Optional, Tuple
19
20
 
20
21
  from .security import REDACTOR, sanitize_cookie_header
21
22
 
@@ -210,6 +211,7 @@ def _fetch_bytes(
210
211
  attempts: int = _RETRY_ATTEMPTS,
211
212
  max_bytes: Optional[int] = None,
212
213
  allow_cross_host_redirect: bool = False,
214
+ diagnostics: Optional[List[str]] = None,
213
215
  ) -> bytes:
214
216
  """GET ``url`` and read the **whole** body, retrying transient failures.
215
217
 
@@ -229,7 +231,11 @@ def _fetch_bytes(
229
231
  try:
230
232
  if allow_cross_host_redirect:
231
233
  context = _open_request(
232
- url, headers, timeout, allow_cross_host_redirect=True
234
+ url,
235
+ headers,
236
+ timeout,
237
+ allow_cross_host_redirect=True,
238
+ diagnostics=diagnostics,
233
239
  )
234
240
  else:
235
241
  context = _open_request(url, headers, timeout)
@@ -252,22 +258,77 @@ def _fetch_bytes(
252
258
  raise last
253
259
 
254
260
 
261
+ _DEFAULT_PORTS = {"http": 80, "https": 443}
262
+
263
+
264
+ def _origin(url: str) -> Tuple[str, str, int]:
265
+ """``(scheme, host, port)`` with default ports filled in.
266
+
267
+ hostname+scheme is not enough: ``https://a:443`` and ``https://a:8443`` are
268
+ different origins, and credentials must not cross that boundary.
269
+ """
270
+ parsed = urllib.parse.urlparse(url or "")
271
+ scheme = (parsed.scheme or "").lower()
272
+ host = (parsed.hostname or "").lower().rstrip(".")
273
+ try:
274
+ port = parsed.port
275
+ except ValueError:
276
+ port = None
277
+ if port is None:
278
+ port = _DEFAULT_PORTS.get(scheme, 0)
279
+ return scheme, host, port
280
+
281
+
282
+ def _is_forbidden_redirect_host(host: str) -> bool:
283
+ """IP literals, loopback and private names are never valid redirect targets."""
284
+ value = (host or "").strip().lower().rstrip(".")
285
+ if not value:
286
+ return True
287
+ if value == "localhost" or value.endswith(".localhost"):
288
+ return True
289
+ try:
290
+ ipaddress.ip_address(value)
291
+ except ValueError:
292
+ return False
293
+ # Any IP literal (loopback, link-local, RFC1918, ::1, ...) is refused:
294
+ # a media CDN never needs one, and a redirect to one is the classic SSRF.
295
+ return True
296
+
297
+
298
+ def _redirect_block_reason(old_url: str, new_url: str) -> str:
299
+ """Why a redirect must not be followed, or an empty string to allow it."""
300
+ old_scheme, _, _ = _origin(old_url)
301
+ new_scheme, new_host, new_port = _origin(new_url)
302
+ parsed = urllib.parse.urlparse(new_url or "")
303
+ if parsed.username or parsed.password:
304
+ return "已阻止跳转到含凭据的 URL(保护登录态)"
305
+ if old_scheme == "https" and new_scheme != "https":
306
+ return "已阻止 HTTPS 降级重定向(保护登录凭据)"
307
+ if new_scheme not in ("http", "https"):
308
+ return f"已阻止跳转到非 HTTP(S) 地址: {new_scheme or '(无协议)'}"
309
+ if _is_forbidden_redirect_host(new_host):
310
+ return f"已阻止跳转到受限主机: {new_host or '(未知主机)'}"
311
+ return ""
312
+
313
+
255
314
  class _SameHostRedirectHandler(urllib.request.HTTPRedirectHandler):
256
- """Follow redirects only on the classroom host.
315
+ """Follow redirects only on the classroom **origin**.
257
316
 
258
317
  ``urllib`` re-sends request headers on a redirect, so a compromised or
259
318
  misconfigured endpoint that answers ``302`` to another origin would leak
260
- the ``Cookie`` header. Cross-origin redirects are refused instead.
319
+ the ``Cookie`` header. Scheme, host and port must all match — a downgrade
320
+ to HTTP or a different port is refused as well.
261
321
  """
262
322
 
263
323
  def redirect_request(self, req, fp, code, msg, headers, newurl): # noqa: D401
264
- parsed = urllib.parse.urlparse(newurl)
265
- target = (parsed.hostname or "").lower()
266
- if target != BASE_HOST or parsed.scheme != "https":
324
+ reason = _redirect_block_reason(req.full_url, newurl)
325
+ if not reason and _origin(newurl) != _origin(BASE_URL):
326
+ reason = f"拒绝跨域重定向到 {_origin(newurl)[1] or '(未知主机)'}(保护登录凭据)"
327
+ if reason:
267
328
  raise urllib.error.HTTPError(
268
329
  req.full_url,
269
330
  code,
270
- f"拒绝跨域或非 HTTPS 重定向到 {target or '(未知主机)'}(保护登录凭据)",
331
+ reason,
271
332
  headers,
272
333
  fp,
273
334
  )
@@ -277,21 +338,36 @@ class _SameHostRedirectHandler(urllib.request.HTTPRedirectHandler):
277
338
  class _StripAuthOnCrossHostRedirectHandler(urllib.request.HTTPRedirectHandler):
278
339
  """Allow slide-CDN redirects, but never carry credentials across origins.
279
340
 
280
- Slide screenshots are static files and may redirect to a media host. The
281
- classroom API keeps the stricter same-host rule; this handler is only used
282
- for image downloads, and it drops ``Cookie`` / ``Authorization`` whenever
283
- the redirect changes the host.
341
+ Slide screenshots are static files and may redirect to a media host. Only
342
+ ``*.zju.edu.cn`` targets are followed (one hop at a time, re-validated by
343
+ urllib for every hop); everything else is blocked and recorded. Whenever
344
+ the redirect changes scheme/host/port, ``Cookie`` / ``Authorization`` /
345
+ ``Proxy-Authorization`` are dropped.
284
346
  """
285
347
 
348
+ def __init__(self, diagnostics: Optional[List[str]] = None) -> None:
349
+ super().__init__()
350
+ self.diagnostics = diagnostics
351
+
352
+ def _blocked(self, reason: str) -> None:
353
+ if self.diagnostics is not None:
354
+ self.diagnostics.append(reason)
355
+
286
356
  def redirect_request(self, req, fp, code, msg, headers, newurl): # noqa: D401
357
+ reason = _redirect_block_reason(req.full_url, newurl)
358
+ if not reason:
359
+ _, new_host, _ = _origin(newurl)
360
+ if not is_zju_host(new_host):
361
+ reason = f"已阻止跳转到非浙大域: {new_host or '(未知主机)'}"
362
+ if reason:
363
+ self._blocked(reason)
364
+ return None
287
365
  new_request = super().redirect_request(req, fp, code, msg, headers, newurl)
288
366
  if new_request is None:
289
367
  return None
290
- old_host = (urllib.parse.urlparse(req.full_url).hostname or "").lower()
291
- new_host = (urllib.parse.urlparse(newurl).hostname or "").lower()
292
- if old_host != new_host:
293
- new_request.remove_header("Cookie")
294
- new_request.remove_header("Authorization")
368
+ if _origin(req.full_url) != _origin(newurl):
369
+ for header in ("Cookie", "Authorization", "Proxy-Authorization"):
370
+ new_request.remove_header(header)
295
371
  return new_request
296
372
 
297
373
 
@@ -300,10 +376,11 @@ def _open_request(
300
376
  headers: Dict[str, str],
301
377
  timeout: int,
302
378
  allow_cross_host_redirect: bool = False,
379
+ diagnostics: Optional[List[str]] = None,
303
380
  ):
304
381
  """Open ``url`` with certificate verification and a redirect policy."""
305
382
  redirect_handler = (
306
- _StripAuthOnCrossHostRedirectHandler()
383
+ _StripAuthOnCrossHostRedirectHandler(diagnostics)
307
384
  if allow_cross_host_redirect
308
385
  else _SameHostRedirectHandler()
309
386
  )
@@ -15,9 +15,10 @@ import sqlite3
15
15
  import subprocess
16
16
  import sys
17
17
  import tempfile
18
+ import time
18
19
  from typing import List, Optional, Tuple
19
20
 
20
- from .security import sanitize_cookie_header
21
+ from .security import REDACTOR, sanitize_cookie_header
21
22
 
22
23
  IS_WIN = sys.platform == "win32"
23
24
  IS_MAC = sys.platform == "darwin"
@@ -378,6 +379,37 @@ def _secure_remove_tree(tmp_dir: str) -> None:
378
379
  pass
379
380
 
380
381
 
382
+ def _sweep_stale_cookie_copies(max_age_seconds: int = 3600) -> int:
383
+ """Shred private cookie-DB copies left behind by a killed process.
384
+
385
+ The normal path overwrites and deletes the copy immediately. A power loss
386
+ or a ``kill -9`` between the copy and the cleanup can leave a plaintext
387
+ cookie database in the temp directory, which is exactly the "明文落盘" this
388
+ plugin promises not to create. Anything older than an hour cannot belong to
389
+ a live call (a read takes milliseconds), so it is removed on the next run.
390
+ """
391
+ root = tempfile.gettempdir()
392
+ try:
393
+ names = os.listdir(root)
394
+ except OSError:
395
+ return 0
396
+ removed = 0
397
+ now = time.time()
398
+ for name in names:
399
+ if not name.startswith("zju-lecture-cookie-"):
400
+ continue
401
+ path = os.path.join(root, name)
402
+ try:
403
+ age = now - os.path.getmtime(path)
404
+ except OSError:
405
+ continue
406
+ if age < max_age_seconds:
407
+ continue
408
+ _secure_remove_tree(path)
409
+ removed += 1
410
+ return removed
411
+
412
+
381
413
  def _copy_cookie_db(db_path: str) -> str:
382
414
  """Copy the live Edge cookie DB and its WAL sidecars into a private dir.
383
415
 
@@ -388,6 +420,7 @@ def _copy_cookie_db(db_path: str) -> str:
388
420
  which lets SQLite replay and checkpoint the WAL in *our* private copy; the
389
421
  browser's own files are never touched.
390
422
  """
423
+ _sweep_stale_cookie_copies()
391
424
  tmp_dir = tempfile.mkdtemp(prefix="zju-lecture-cookie-")
392
425
  try:
393
426
  os.chmod(tmp_dir, 0o700)
@@ -595,10 +628,16 @@ def read_edge_cookies(
595
628
  )
596
629
 
597
630
  if decrypt_failures:
598
- print(
599
- f"[zju-lecture] {decrypt_failures} 条 Edge Cookie 解密失败,已跳过。",
600
- file=sys.stderr,
631
+ # Route every diagnostic through the redactor, even a bare count: one
632
+ # code path writing to stderr directly is how a future message that
633
+ # *does* contain a value slips past the guarantee in SECURITY.md.
634
+ sys.stderr.write(
635
+ REDACTOR.redact(
636
+ f"[zju-lecture] {decrypt_failures} 条 Edge Cookie 解密失败,已跳过。"
637
+ )
638
+ + "\n"
601
639
  )
640
+ sys.stderr.flush()
602
641
 
603
642
  try:
604
643
  return sanitize_cookie_header("; ".join(pairs)), None
@@ -19,6 +19,37 @@ class CoursewareError(Exception):
19
19
  pass
20
20
 
21
21
 
22
+ #: 课件是 zip 容器:解压前先按声明的 file_size 累计,避免 zip bomb。
23
+ MAX_ARCHIVE_TOTAL_BYTES = 64 * 1024 * 1024
24
+ MAX_ARCHIVE_MEMBER_BYTES = 16 * 1024 * 1024
25
+
26
+
27
+ def _validate_zip_members(archive: zipfile.ZipFile) -> None:
28
+ """Reject an archive whose declared uncompressed size exceeds the budget."""
29
+ total = 0
30
+ for info in archive.infolist():
31
+ if info.file_size > MAX_ARCHIVE_MEMBER_BYTES:
32
+ raise CoursewareError(
33
+ f"课件包含超过 {MAX_ARCHIVE_MEMBER_BYTES // (1024 * 1024)} MiB "
34
+ f"的成员({info.filename}),已拒绝解压"
35
+ )
36
+ total += info.file_size
37
+ if total > MAX_ARCHIVE_TOTAL_BYTES:
38
+ raise CoursewareError(
39
+ f"课件解压后超过 {MAX_ARCHIVE_TOTAL_BYTES // (1024 * 1024)} MiB "
40
+ "上限,已拒绝解压"
41
+ )
42
+
43
+
44
+ def _read_zip_member(archive: zipfile.ZipFile, name: str) -> bytes:
45
+ info = archive.getinfo(name)
46
+ if info.file_size > MAX_ARCHIVE_MEMBER_BYTES:
47
+ raise CoursewareError(
48
+ f"课件成员 {name} 超过 {MAX_ARCHIVE_MEMBER_BYTES // (1024 * 1024)} MiB 上限"
49
+ )
50
+ return archive.read(info)
51
+
52
+
22
53
  def extract_courseware_text(path: str, max_chars: Optional[int] = None) -> str:
23
54
  """Extract plain text from local courseware files.
24
55
 
@@ -73,6 +104,7 @@ def _extract_pptx(path: str) -> str:
73
104
  """Extract text from slides inside PPTX zip package."""
74
105
  try:
75
106
  with zipfile.ZipFile(path, "r") as z:
107
+ _validate_zip_members(z)
76
108
  slide_names = [
77
109
  n for n in z.namelist()
78
110
  if re.match(r"^ppt/slides/slide\d+\.xml$", n)
@@ -84,7 +116,7 @@ def _extract_pptx(path: str) -> str:
84
116
 
85
117
  parts = []
86
118
  for idx, name in enumerate(slide_names, start=1):
87
- xml_content = z.read(name).decode("utf-8", "ignore")
119
+ xml_content = _read_zip_member(z, name).decode("utf-8", "ignore")
88
120
  texts = re.findall(r"<a:t>([^<]*)</a:t>", xml_content)
89
121
  cleaned = " ".join(t.strip() for t in texts if t.strip())
90
122
  if cleaned:
@@ -115,7 +147,10 @@ def _extract_docx(path: str) -> str:
115
147
  """
116
148
  try:
117
149
  with zipfile.ZipFile(path, "r") as z:
118
- xml_content = z.read("word/document.xml").decode("utf-8", "ignore")
150
+ _validate_zip_members(z)
151
+ xml_content = _read_zip_member(z, "word/document.xml").decode(
152
+ "utf-8", "ignore"
153
+ )
119
154
  except Exception as e:
120
155
  raise CoursewareError(f"读取 DOCX 课件失败: {e}")
121
156