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:
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ const states={preflight:'准备中',copying:'正在拷贝',verifying_existing:'
|
||||
function bytes(n){if(!n)return'0 B';const u=['B','KB','MB','GB','TB'],i=Math.min(4,Math.floor(Math.log(n)/Math.log(1000)));return(n/1000**i).toFixed(i>1?1:0)+' '+u[i]}
|
||||
function duration(s){if(s==null)return'估算中';if(s<60)return'不足 1 分钟';if(s<3600)return Math.ceil(s/60)+' 分钟';return Math.floor(s/3600)+' 小时 '+Math.ceil(s%3600/60)+' 分钟'}
|
||||
function error(message){$('error').textContent=message||'';$('error').style.display=message?'block':'none'}
|
||||
async function api(action,args={},post=false){const url=API+action+(post?'':'?'+new URLSearchParams(args));const r=await fetch(url,{method:post?'POST':'GET',headers:post?{'Content-Type':'application/json','X-Dayu-Request':'1'}:{},body:post?JSON.stringify(args):undefined});const d=await r.json();if(!r.ok)throw Error(d.error||'操作失败');return d}
|
||||
async function api(action,args={},post=false){const url=API+action+(post?'':'?'+new URLSearchParams(args));const r=await fetch(url,{method:post?'POST':'GET',headers:post?{'Content-Type':'application/json','X-Dayu-Request':'1','X-Dayu-Token':'__DAYU_CSRF__'}:{},body:post?JSON.stringify(args):undefined});const d=await r.json();if(!r.ok)throw Error(d.error||'操作失败');return d}
|
||||
function button(name,fn){const b=document.createElement('button');b.textContent=name;b.onclick=fn;return b}
|
||||
function folderRows(container,entries,source){container.replaceChildren();if(!entries.length){const e=document.createElement('div');e.className='empty';e.textContent=source?'当前目录下没有可选择的文件夹':'当前目录下没有子文件夹,可以直接选作目标';container.append(e);return}for(const entry of entries){const row=document.createElement('div');row.className='row';if(source){const check=document.createElement('input');check.type='checkbox';check.setAttribute('aria-label','选择 '+entry.name);check.checked=selected.has(entry.path);check.onchange=()=>{check.checked?selected.add(entry.path):selected.delete(entry.path);updateSelection()};row.append(check)}const icon=document.createElement('span');icon.className='foldericon';icon.textContent='▰';row.append(icon,button(entry.name,()=>source?browseSource(entry.path):browseDest(entry.path)));const arrow=document.createElement('span');arrow.className='arrow';arrow.textContent='›';row.append(arrow);container.append(row)}}
|
||||
function updateSelection(){$('selection').textContent=selected.size?'已选 '+selected.size+' 个目录。同名文件会先保留,并在复制后校验。':'尚未选择源目录。同名文件会先保留,并在复制后校验。';$('start').disabled=!selected.size||!destPath||busy||loading}
|
||||
|
||||
在新工单中引用
屏蔽一个用户