File size: 12,327 Bytes
fc92530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ca3c16
fc92530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ca3c16
fc92530
5ca3c16
fc92530
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""One-use grants create lease-scoped preview access, never a workbench login."""
import asyncio
import hashlib
import hmac
import json
import re
import secrets
import threading
import time
from http.cookies import SimpleCookie
from pathlib import Path
from urllib.parse import parse_qs
from starlette.responses import JSONResponse,Response
from src.modules.skill.domain.models import SkillError

ASSETS=Path(__file__).parent
SITE=ASSETS.parents[1]/'src/apps/workbench/preview_site'
NONCE=re.compile(r'[a-f0-9]{64}')
LEASE=re.compile(r'/preview-site/([a-f0-9]{32})(/api(?:/.*)?)')


class PreviewBridge:
    def __init__(self,engine,*,clock=time.monotonic,capacity=128):
        self.engine,self.clock,self.capacity=engine,clock,capacity
        self.lock=threading.Lock();self.tickets={};self.used={};self.access={}

    @staticmethod
    def cookie(scope,name):
        cookies=SimpleCookie();cookies.load(dict(scope['headers']).get(b'cookie',b'').decode())
        value=cookies.get(name)
        return value.value if value else ''

    def session(self,scope):
        token=self.cookie(scope,'zk_session')
        actor,csrf,_=self.engine.identity.session(token)
        return actor,csrf,token

    @staticmethod
    def patch_runtime(data):
        text=data.decode('utf-8');old="const previewWindow=window.open(url,'_blank');";stage="let state=await api('/api/preview',{kind,reference});"
        if text.count(old)!=1 or text.count(stage)!=1:raise RuntimeError('SPACE_PREVIEW_ADAPTER_MISMATCH')
        text="import {openPreview} from '/space/preview-parent.js';\n"+text.replace(old,'const bridge=openPreview(url,{kind,reference});let previewWindow=bridge.window;')
        return text.replace(stage,'await bridge.ready;previewWindow=bridge.window;'+stage).encode()

    def prune(self):
        now=self.clock()
        self.tickets={k:v for k,v in self.tickets.items() if v['expires']>now}
        self.used={k:v for k,v in self.used.items() if v['expires']>now}
        self.access={k:v for k,v in self.access.items() if v['expires']>now}

    def source(self,actor,kind,reference):
        return self.engine.evidence(actor,kind,reference)[2]['manifest']['source']['source_digest']

    def checked_nonce(self,data):
        nonce=data.get('nonce')
        if not isinstance(nonce,str) or not NONCE.fullmatch(nonce):raise SkillError('INVALID_INPUT','打开请求不完整。',422)
        return hashlib.sha256(nonce.encode()).hexdigest()

    def authorize(self,scope,data):
        actor,csrf,token=self.session(scope)
        if not hmac.compare_digest(dict(scope['headers']).get(b'x-csrf-token',b'').decode(),csrf):raise SkillError('CSRF_DENIED','请刷新工作台后重试。',403)
        key=self.checked_nonce(data);proof=data.get('proof')
        if proof is not None and (not isinstance(proof,str) or not NONCE.fullmatch(proof)):raise SkillError('INVALID_INPUT','打开请求不完整。',422)
        if data.get('lease'):
            state=self.engine.preview_state(actor,data['lease']);kind,reference=state['kind'],state['reference']
        else:kind,reference=data.get('kind'),data.get('reference')
        if kind not in {'run','example','skill'} or not isinstance(reference,str) or not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',reference):raise SkillError('INVALID_INPUT','打开请求不完整。',422)
        source=self.source(actor,kind,reference)
        with self.lock:
            self.prune()
            if key in self.used:raise SkillError('PREVIEW_HANDOFF_USED','打开请求已使用。',409)
            proof_hash=hashlib.sha256(proof.encode()).hexdigest() if proof else None
            old=self.tickets.get(key)
            if old and (old['token'],old['kind'],old['reference'],old['proof_hash'])!=(token,kind,reference,proof_hash):raise SkillError('PREVIEW_HANDOFF_CONFLICT','打开请求不符。',409)
            if not old:
                if len(self.tickets)+len(self.used)+len(self.access)>=self.capacity:raise SkillError('PREVIEW_HANDOFF_CAPACITY','打开请求较多,请稍后重试。',429)
                self.tickets[key]={'token':token,'owner':actor.user_id,'kind':kind,'reference':reference,'source':source,'proof_hash':proof_hash,'expires':self.clock()+60}
        return JSONResponse({'authorized':True,'scope':'single-preview-only'})

    def status(self,scope,data):
        actor,csrf,_=self.session(scope)
        if not hmac.compare_digest(dict(scope['headers']).get(b'x-csrf-token',b'').decode(),csrf):raise SkillError('CSRF_DENIED','请刷新工作台。',403)
        key=self.checked_nonce(data)
        with self.lock:
            self.prune();item=self.used.get(key) or self.tickets.get(key)
            if not item or item['owner']!=actor.user_id:raise SkillError('PREVIEW_NOT_FOUND','打开请求不可访问。',404)
            return JSONResponse({'consumed':key in self.used})

    def consume(self,data):
        key=self.checked_nonce(data);proof=data.get('proof')
        if not isinstance(proof,str) or not NONCE.fullmatch(proof):raise SkillError('INVALID_INPUT','打开请求不完整。',422)
        with self.lock:
            self.prune()
            if key in self.used:raise SkillError('PREVIEW_HANDOFF_USED','打开请求已使用。',409)
            item=self.tickets.get(key)
            if not item:return JSONResponse({'pending':True},status_code=202)
            if item['proof_hash'] and not hmac.compare_digest(item['proof_hash'],hashlib.sha256(proof.encode()).hexdigest()):raise SkillError('PREVIEW_HANDOFF_PROOF','请从原窗口打开。',403)
            actor,_,_=self.engine.identity.session(item['token'])
            if self.source(actor,item['kind'],item['reference'])!=item['source']:raise SkillError('SOURCE_DIGEST_MISMATCH','制品版本已变化。',409)
            state=self.engine.start_preview(actor,item['kind'],item['reference'])
            self.tickets.pop(key);self.used[key]={'owner':item['owner'],'expires':self.clock()+60}
            access=secrets.token_urlsafe(32);self.access[hashlib.sha256(access.encode()).hexdigest()]={**item,'lease':state['id'],'csrf':secrets.token_urlsafe(32),'expires':self.clock()+900}
        response=JSONResponse({'authenticated':True,'lease':state['id'],'scope':'single-preview-only'})
        response.set_cookie('zk_preview',access,max_age=900,path='/preview-site/'+state['id']+'/',httponly=True,secure=True,samesite='none')
        response.headers['set-cookie']+='; Partitioned'
        return response

    async def read_json(self,receive,limit=2048):
        raw=bytearray()
        while True:
            message=await receive()
            if message['type']!='http.request':raise SkillError('INVALID_INPUT','请求中断。',400)
            raw.extend(message.get('body',b''))
            if len(raw)>limit:raise SkillError('PAYLOAD_TOO_LARGE','请求过大。',413)
            if not message.get('more_body'):break
        data=json.loads(raw)
        if not isinstance(data,dict):raise ValueError()
        return data

    async def scoped(self,scope,receive,send,match):
        lease,path=match.groups();method=scope['method']
        with self.lock:
            self.prune();item=self.access.get(hashlib.sha256(self.cookie(scope,'zk_preview').encode()).hexdigest())
        if not item or item['lease']!=lease:raise SkillError('PREVIEW_NOT_FOUND','请从对应工程打开应用。',401)
        actor,master_csrf,_=self.engine.identity.session(item['token'])
        if self.source(actor,item['kind'],item['reference'])!=item['source']:raise SkillError('SOURCE_DIGEST_MISMATCH','制品版本已变化。',409)
        if method not in {'GET','HEAD'} and not hmac.compare_digest(dict(scope['headers']).get(b'x-csrf-token',b'').decode(),item['csrf']):raise SkillError('CSRF_DENIED','应用请求来源不符。',403)
        if path=='/api/session' and method=='GET':
            await JSONResponse({'authenticated':True,'csrf':item['csrf'],'actor':{'user_id':actor.user_id},'scope':'single-preview-only'},headers={'Cache-Control':'no-store','Referrer-Policy':'no-referrer','X-Content-Type-Options':'nosniff'})(scope,receive,send);return
        allowed={('GET','/api/preview'),('GET','/api/preview/pdf-sample'),('POST','/api/preview/request'),('POST','/api/preview/client'),('DELETE','/api/preview')}
        if (method,path) not in allowed:raise SkillError('PREVIEW_ROUTE_DENIED','此凭证只允许当前应用预览。',403)
        query=parse_qs(scope['query_string'].decode(),keep_blank_values=True)
        if set(query)-{'preview_id'} or query.get('preview_id',[lease])!=[lease]:raise SkillError('PREVIEW_NOT_FOUND','预览不符。',404)
        data=await self.read_json(receive,190000) if method=='POST' else None
        if data is not None and data.get('preview_id',lease)!=lease:raise SkillError('PREVIEW_NOT_FOUND','预览不符。',404)
        if data is not None:data['preview_id']=lease
        headers=[(k,v) for k,v in scope['headers'] if k not in {b'host',b'origin',b'cookie',b'x-csrf-token',b'content-length'}]
        headers.extend([(b'host',self.internal_host.encode()),(b'origin',self.internal.encode()),(b'cookie',('zk_session='+item['token']).encode()),(b'x-csrf-token',master_csrf.encode())])
        body=json.dumps(data).encode() if data is not None else b'';headers.append((b'content-length',str(len(body)).encode()));sent=False
        async def replay():
            nonlocal sent
            if not sent:sent=True;return {'type':'http.request','body':body,'more_body':False}
            return await receive()
        inner={**scope,'path':path,'raw_path':path.encode(),'query_string':('preview_id='+lease).encode(),'headers':headers,'scheme':'http','server':('127.0.0.1',self.internal_port)}
        await self.app(inner,replay,send)

    async def handle(self,scope,receive,send):
        path,method=scope['path'],scope['method'];response=None
        try:
            match=LEASE.fullmatch(path)
            if match:
                await self.scoped(scope,receive,send,match);return True
            if method=='GET' and path in {'/space/preview-parent.js','/space/preview-bridge.js'}:
                response=Response((ASSETS/path.rsplit('/',1)[1]).read_bytes(),media_type='text/javascript')
            elif method=='GET' and path in {'/preview-launch/','/preview-site/'}:
                query=parse_qs(scope['query_string'].decode(),keep_blank_values=True)
                name='index.html' if path=='/preview-site/' and set(query)=={'lease'} else None
                response=Response((SITE/'index.html').read_bytes() if name else (ASSETS/'preview-bridge.html').read_bytes(),media_type='text/html')
            elif method=='GET' and path in {'/preview-site/site.js','/preview-site/site.css','/preview-site/lifecycle.js'}:
                content=(SITE/path.rsplit('/',1)[1]).read_bytes()
                if path.endswith('/site.js'):
                    text=content.decode().replace("'/api/session'","SCOPED_API+'session'").replace("'/api/preview","SCOPED_API+'preview")
                    content=("const SCOPED_API='/preview-site/'+new URLSearchParams(location.search).get('lease')+'/api/';\n"+text).encode()
                response=Response(content,media_type='text/javascript' if path.endswith('.js') else 'text/css')
            elif method=='POST' and path in {'/space/preview-authorize','/space/preview-login','/space/preview-status'}:
                data=await self.read_json(receive)
                if path.endswith('authorize'):response=await asyncio.to_thread(self.authorize,scope,data)
                elif path.endswith('status'):response=self.status(scope,data)
                else:response=await asyncio.to_thread(self.consume,data)
        except (ValueError,UnicodeError):response=JSONResponse({'code':'INVALID_INPUT'},status_code=422)
        except SkillError as error:response=JSONResponse({'code':error.code,'message':error.message},status_code=error.status)
        if response is None:return False
        response.headers.update({'Cache-Control':'no-store','Referrer-Policy':'no-referrer','X-Content-Type-Options':'nosniff','Content-Security-Policy':"default-src 'self'; script-src 'self'; style-src 'self'; connect-src 'self'; frame-ancestors 'self' https://huggingface.co; base-uri 'none'; form-action 'self'"})
        await response(scope,receive,send);return True