Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(javdb): 校验匿名签名入口与时间戳表达式

longmeidaobef67c2936e1
diff --git a/src/peach/javdb_public.py b/src/peach/javdb_public.py
index 6e572a46a..78037f7d4 100644
--- a/src/peach/javdb_public.py
+++ b/src/peach/javdb_public.py
@@ -33,17 +33,20 @@ _CACHE_READS = ContextVar('jav_public_cache_reads', default=None)
def public_signature_parts(script: str) -> tuple[str, str] | None:
"""从公开请求头的签名计算式读取常量,不执行来源脚本。"""
- call = re.search(r'\bjdsignature\s*:\s*(' + _JS_IDENTIFIER + r')\s*\(\s*\)', script)
- if not call:
+ calls = set(re.findall(r'\bjdsignature\s*:\s*(' + _JS_IDENTIFIER + r')\s*\(\s*\)', script))
+ if len(calls) != 1:
return None
- recipe = re.search(
- r'\bfunction\s+' + re.escape(call[1]) + r'\s*\(\s*\)\s*\{[^{}]{0,512}?\breturn\s*'
- r'`\$\{(?P<stamp>' + _JS_IDENTIFIER + r')\}\.\$\{(?P<middle>' + _JS_IDENTIFIER + r')\}\.'
+ recipes = list(re.finditer(
+ r'\bfunction\s+' + re.escape(calls.pop()) + r'\s*\(\s*\)\s*\{\s*(?:const|let|var)\s+'
+ r'(?P<stamp>' + _JS_IDENTIFIER + r')\s*=\s*Math\.floor\(Date\.now\(\)\s*/\s*'
+ r'(?:1e3|1000)\)\.toString\(\)\s*;\s*return\s*'
+ r'`\$\{(?P=stamp)\}\.\$\{(?P<middle>' + _JS_IDENTIFIER + r')\}\.'
r'\$\{' + _JS_IDENTIFIER + r'\(\s*(?P=stamp)\s*\+\s*(?P<suffix>' + _JS_IDENTIFIER + r')\s*\)\}`\s*;?\s*\}',
script,
- )
- if not recipe:
+ ))
+ if len(recipes) != 1:
return None
+ recipe = recipes[0]
values = []
for name in (recipe['middle'], recipe['suffix']):
assignments = re.findall(r'(?<![\w$])' + re.escape(name) + r'\s*=\s*("(?:\\.|[^"\\]){1,512}")\s*[,;]', script)
@@ -53,7 +56,8 @@ def public_signature_parts(script: str) -> tuple[str, str] | None:
value = json.loads(assignments[0])
except ValueError:
return None
- if not isinstance(value, str) or not 1 <= len(value) <= 256:
+ if (not isinstance(value, str) or not 1 <= len(value) <= 256
+ or any(ord(char) < 32 for char in value)):
return None
values.append(value)
return values[0], values[1]
diff --git a/tests/test_javdb_public.py b/tests/test_javdb_public.py
index 849b25bf3..437198a72 100644
--- a/tests/test_javdb_public.py
+++ b/tests/test_javdb_public.py
@@ -22,6 +22,19 @@ from support.ledger import fresh_ledger
class PublicProtocolTests(unittest.TestCase):
+ def test_signature_rejects_multiple_recipes_unknown_time_and_control_literals(self):
+ script = ('const AA="sample-middle",BB="sample-suffix";'
+ 'function makeHeader(){const now=Math.floor(Date.now()/1e3).toString();'
+ 'return`${now}.${AA}.${digest(now+BB)}`}const headers={jdsignature:makeHeader()};')
+ function=script[script.index('function'):script.index('const headers')]
+ for invalid in (script+'const other={jdsignature:otherHeader()};', script+function,
+ script.replace('Date.now()','clock()'),
+ script.replace('sample-suffix', r'bad\nvalue')):
+ with self.subTest(script=invalid):
+ self.assertIsNone(public_signature_parts(invalid))
+ self.assertEqual(public_signature_parts(script.replace('1e3','1000')),
+ ('sample-middle','sample-suffix'))
+
def test_signature_follows_header_recipe_with_minified_identifiers(self):
script = ('const AA="sample-middle",BB="sample-suffix";'
'function makeHeader(){const now=Math.floor(Date.now()/1e3).toString();'
@@ -147,7 +160,7 @@ class PublicProtocolTests(unittest.TestCase):
if request.url.endswith('/'):
body = b'<script src="/assets/index-test.js"></script>'
elif '/assets/' in request.url:
- body = (b'const S1="test-middle",E1="test-suffix";function signature(){const n=String(Date.now());'
+ body = (b'const S1="test-middle",E1="test-suffix";function signature(){const n=Math.floor(Date.now()/1e3).toString();'
b'return`${n}.${S1}.${digest(n+E1)}`}const headers={jdsignature:signature()};')
else:
if blocked:
@@ -351,7 +364,7 @@ class DetailProjectionTests(unittest.TestCase):
elif request.url.endswith('/'):
body = b'<script src="/assets/index-test.js"></script>'
elif '/assets/' in request.url:
- body = (b'const S1="test-middle",E1="test-suffix";function signature(){const n=String(Date.now());'
+ body = (b'const S1="test-middle",E1="test-suffix";function signature(){const n=Math.floor(Date.now()/1e3).toString();'
b'return`${n}.${S1}.${digest(n+E1)}`}const headers={jdsignature:signature()};')
else:
body = json.dumps({'success': 1, 'data': {'movie': {'id': 'Exact', 'number': 'EXM-001',