Fix authenticated writes behind fnOS gateway and record completed backup
这个提交包含在:
1 父节点
12df14ebf0
当前提交
1827628a93
8 个文件已更改
+80
-11
没有与搜索条件相匹配的文件
+10
-4
@@ -1,6 +1,8 @@
|
||||
"""Unprivileged HTTP UI, reachable only through the authenticated fnOS gateway."""
|
||||
import http.server
|
||||
import grp
|
||||
import hmac
|
||||
import secrets
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
@@ -11,6 +13,7 @@ import urllib.parse
|
||||
ROOT = Path(__file__).resolve().parent
|
||||
PREFIX = '/app/dayu-diskcopy'
|
||||
VAR = Path(os.environ['TRIM_PKGVAR'])
|
||||
CSRF_TOKEN = secrets.token_urlsafe(32)
|
||||
|
||||
|
||||
class Handler(http.server.BaseHTTPRequestHandler):
|
||||
@@ -34,7 +37,8 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||
return self.reply(403, {'error': '请从飞牛桌面使用管理员账号打开此应用'})
|
||||
path = urllib.parse.urlsplit(self.path).path
|
||||
if path in (PREFIX, PREFIX + '/') and not post:
|
||||
return self.reply(200, (ROOT / 'index.html').read_bytes(), 'text/html; charset=utf-8')
|
||||
page = (ROOT / 'index.html').read_bytes().replace(b'__DAYU_CSRF__', CSRF_TOKEN.encode())
|
||||
return self.reply(200, page, 'text/html; charset=utf-8')
|
||||
if not path.startswith(PREFIX + '/api/'):
|
||||
return self.reply(404, {'error': '页面不存在'})
|
||||
action = path.removeprefix(PREFIX + '/api/')
|
||||
@@ -44,11 +48,13 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||
if post:
|
||||
if self.headers.get('X-Dayu-Request') != '1':
|
||||
return self.reply(403, {'error': '无效请求来源'})
|
||||
origin = self.headers.get('Origin')
|
||||
if origin and urllib.parse.urlsplit(origin).netloc != self.headers.get('Host'):
|
||||
# fnOS rewrites upstream Host; validate the private page nonce instead.
|
||||
if not hmac.compare_digest(self.headers.get('X-Dayu-Token', ''), CSRF_TOKEN):
|
||||
return self.reply(403, {'error': '页面验证已失效,请刷新应用后重试'})
|
||||
if self.headers.get('Sec-Fetch-Site', '') not in ('', 'none', 'same-origin'):
|
||||
return self.reply(403, {'error': '请求来源不匹配'})
|
||||
length = int(self.headers.get('Content-Length', '0'))
|
||||
if length > 262144:
|
||||
if not 0 <= length <= 262144:
|
||||
return self.reply(413, {'error': '请求过大'})
|
||||
args = json.loads(self.rfile.read(length) or '{}')
|
||||
else:
|
||||
|
||||
在新工单中引用
屏蔽一个用户