File size: 4,851 Bytes
caf8059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ca3c16
 
caf8059
 
5ca3c16
caf8059
 
 
5ca3c16
 
caf8059
 
 
 
5ca3c16
 
 
caf8059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ca3c16
 
caf8059
 
 
 
 
 
5ca3c16
 
 
 
 
 
 
 
caf8059
 
 
 
 
 
 
 
 
 
 
 
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
"""Server-owned local Java policy. Model text is never an authority."""
import re
from pathlib import PurePosixPath
from src.modules.skill.domain.models import SkillError, digest


def deny(code):
    raise SkillError(code, code.replace('_', ' ').lower(), 409)


class Harness:
    def __init__(self, *, enabled=False, allow_simulated=False, allow_qemu=False):
        self.enabled = enabled
        self.allow_simulated = allow_simulated
        self.allow_qemu = allow_qemu

    def framework(self, framework):
        if not self.enabled:
            deny('EXECUTION_DISABLED')
        from src.modules.execution.pdf_dependency import FRAMEWORK, COORDINATE, VERSION
        from src.modules.execution.scenarios.webapp import PROFILE
        from src.modules.execution.scenarios.business_app import PROFILE as BUSINESS_PROFILE
        dependencies = framework['dependencies']
        pdf_profile = framework.get('profile_id') == FRAMEWORK and framework.get('version') == '0.1.0'
        browser_profile = framework.get('profile_id') in {PROFILE,BUSINESS_PROFILE} and framework.get('version') == '0.1.0'
        allowed_dependencies = {COORDINATE: VERSION} if pdf_profile else {}
        if (framework['state'] != 'approved' or framework['language'] != 'java'
                or framework['java_release'] != 17 or framework['build_tool'] != 'jdk'
                or dependencies != allowed_dependencies
                or ('website/' in framework['allowed_paths']) != browser_profile):
            deny('UNSUPPORTED_RUNTIME_PROFILE')

    def files(self, patch, framework):
        self.framework(framework)
        from src.modules.execution.scenarios.webapp import PROFILE, PAGE, JAVA, JAVA_SOURCE
        from src.modules.execution.scenarios.business_app import PROFILE as BUSINESS_PROFILE
        browser_profile = framework['profile_id'] in {PROFILE,BUSINESS_PROFILE}
        if not isinstance(patch, dict) or set(patch) != {'files'} or not isinstance(patch['files'], dict):
            deny('INVALID_PATCH')
        files = patch['files']
        if not 1 <= len(files) <= 32:
            deny('FILE_LIMIT')
        folded = set()
        for name, content in files.items():
            if not isinstance(name, str) or not isinstance(content, str):
                deny('INVALID_FILE')
            path = PurePosixPath(name)
            parts = name.split('/')
            reserved = {'con', 'prn', 'aux', 'nul', *(f'com{i}' for i in range(10)), *(f'lpt{i}' for i in range(10))}
            if (len(name) > 200 or path.is_absolute() or not re.fullmatch(r'[A-Za-z0-9_/.-]+', name)
                    or any(p in {'', '.', '..'} or p.startswith('.') or p.endswith('.') or p.split('.')[0].lower() in reserved for p in parts)
                    or name.lower() in folded or not any(name.startswith(p) for p in framework['allowed_paths'])
                    or not ((name.startswith('src/main/java/') and name.endswith('.java'))
                            or (name.startswith('docs/') and name.endswith('.md'))
                            or (browser_profile and name == PAGE))):
                deny('PATH_NOT_ALLOWED')
            if len(content.encode()) > 65536 or '\0' in content:
                deny('FILE_TOO_LARGE')
            folded.add(name.lower())
        if not any(p.endswith('.java') for p in files) or sum(len(v.encode()) for v in files.values()) > 262144:
            deny('SOURCE_LIMIT')
        if browser_profile:
            if set(files) != {JAVA, PAGE} or (framework['profile_id']==PROFILE and files[JAVA] != JAVA_SOURCE):
                deny('WEBAPP_SOURCE_INVALID')
            page = files[PAGE]
            if (len(page.encode()) > 65536 or '<html' not in page.lower() or '<script' not in page.lower()
                    or '<style' not in page.lower() or '</html>' not in page.lower()
                    or re.search(r'https?://|<\s*(?:iframe|object|embed|base|link)\b|\b(?:fetch|XMLHttpRequest|WebSocket|EventSource)\s*\(', page, re.I)):
                deny('WEBAPP_PAGE_INVALID')
        # JDK-only profile; no build descriptor, dependency download or executable scripts.
        return {'files': dict(sorted(files.items())), 'source_digest': digest(files)}

    def evidence(self, evidence, candidate, stage):
        expected = ('source_digest', 'image', 'classes_digest', 'suite_digest', 'config_digest', 'execution_mode')
        if evidence.get('stage') != stage or evidence.get('passed') is not True:
            deny('QUALITY_GATE_FAILED')
        if any(evidence.get(k) != candidate.get(k) or not candidate.get(k) for k in expected):
            deny('EVIDENCE_BINDING_MISMATCH')
        if candidate['execution_mode'] != 'docker' and not (candidate['execution_mode'] == 'qemu' and self.allow_qemu) and not self.allow_simulated:
            deny('SIMULATED_EVIDENCE_REJECTED')
        return evidence