Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

7.4 KB
Raw Blame
"""Commit-bound source builds. Does not execute or download repository code."""
import re
import hashlib
from urllib.parse import urlsplit

from extension_github import repository_identity


def verify_source_runtime(candidate, service):
    """Enforce the source sandbox at proposal, publication and re-enable time.

    Defaults in the generator alone are insufficient: a proposed Dockerfile
    can omit USER and a saved recipe may be edited before a restart.
    """
    target = candidate.get('manifest', {}).get('service', {}).get('id', '')
    network = target + '-sandbox'
    uid = str(service.get('user', ''))
    if not re.fullmatch(r'[1-9][0-9]*(?::[1-9][0-9]*)?', uid):
        raise ValueError('Source runtime requires an explicit non-root numeric user')
    if (service.get('cap_drop') != ['ALL'] or service.get('cap_add')
            or service.get('security_opt') != ['no-new-privileges:true']
            or service.get('read_only') is not True):
        raise ValueError('Source runtime requires read-only, capability-free confinement')
    if (service.get('networks') != [network] or 'network_mode' in service
            or candidate['compose'].get('networks', {}).get(network) != {'internal': True}):
        raise ValueError('Source runtime requires its own internal sandbox network')
    if service.get('ports'):
        # An internal network cannot publish ports, so this service would be
        # unreachable. Web-service source recipes are refused at generation.
        raise ValueError('Source runtime cannot publish ports: its sandbox network is internal')
    cpus, pids = service.get('cpus'), service.get('pids_limit')
    memory = service.get('mem_limit')
    if (isinstance(cpus, bool) or not isinstance(cpus, (int, float)) or not 0 < cpus <= 32
            or isinstance(pids, bool) or not isinstance(pids, int) or not 0 < pids <= 4096
            or not isinstance(memory, str) or not re.fullmatch(r'[1-9][0-9]*(?:m|g)', memory.lower())):
        raise ValueError('Source runtime requires bounded CPU, memory and PID limits')
    memory_mib = int(memory[:-1]) * (1024 if memory[-1].lower() == 'g' else 1)
    if memory_mib > 32768:
        raise ValueError('Source runtime memory limit exceeds 32 GiB')


def source_builds(candidate):
    """Bind upstream or proposed Dockerfiles to the immutable source context."""
    repository = repository_identity(candidate['repository']).lower()
    commit = candidate['commit']
    if not isinstance(commit, str) or not re.fullmatch(r'[a-f0-9]{40}', commit):
        raise ValueError('Immutable source revision required')
    result = []
    services = candidate['compose']['services']
    if any(isinstance(service, dict) and 'build' in service for service in services.values()):
        for service in services.values():
            if not isinstance(service, dict):
                raise ValueError('Source runtime service must be a mapping')
            verify_source_runtime(candidate, service)
    for name, service in services.items():
        if not isinstance(service, dict) or 'build' not in service:
            continue
        build = service['build']
        if (not isinstance(build, dict) or set(build) - {'context', 'dockerfile', 'dockerfile_inline', 'target'}
                or not isinstance(build.get('context'), str)):
            raise ValueError('Unsupported source build settings')
        context = build['context']
        parsed = urlsplit(context)
        source = context.split('#', 1)[0]
        revision, separator, directory = parsed.fragment.partition(':')
        if (repository_identity(source).lower() != repository or revision != commit
                or not parsed.path.endswith('.git') or (separator and not directory)):
            raise ValueError('Build must use the selected repository and revision')
        dockerfile = build.get('dockerfile', 'Dockerfile')
        for path, required in ((directory, False), (dockerfile, True)):
            if (not isinstance(path, str) or len(path) > 256
                    or (required and not path)
                    or (path and any(not re.fullmatch(r'[A-Za-z0-9_.-]+', part)
                                     or part in {'.', '..'} for part in path.split('/')))):
                raise ValueError('Build file must stay within the repository context')
        target = build.get('target')
        if target is not None and (not isinstance(target, str) or not re.fullmatch(r'[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}', target)):
            raise ValueError('Invalid Dockerfile target')
        if (service.get('image') != f'ods-source-{name}:{commit}'
                or service.get('pull_policy') != 'never'):
            raise ValueError('Source builds require an owned local image')
        if 'dockerfile_inline' in build:
            inline = build['dockerfile_inline']
            if ('dockerfile' in build or not isinstance(inline, str) or not inline.strip()
                    or len(inline.encode('utf-8')) > 24576
                    or any(ord(char) < 32 and char not in '\n\r\t' for char in inline)
                    or '$' in re.findall(r'\$\$|\$', inline)):
                raise ValueError('Inline Dockerfile must be bounded and escape Compose dollars')
            result.append({'service': name, 'kind': 'proposed-dockerfile',
                           'sha256': hashlib.sha256(inline.encode('utf-8')).hexdigest()})
        else:
            result.append({'service': name, 'path': '/'.join(filter(None, (directory, dockerfile)))})
    return result


async def inspect_source_builds(candidate, inspect_file):
    receipts, cached = [], {}
    for spec in source_builds(candidate):
        if spec.get('kind') == 'proposed-dockerfile':
            # These bytes belong to the reviewed recipe, not to an upstream
            # file. Never fabricate a Git blob or claim upstream authorship.
            receipts.append(spec)
            continue
        path = spec['path']
        if path not in cached:
            file = await inspect_file(candidate['repository'], candidate['commit'], path)
            if (repository_identity(file.get('repository')).lower() != repository_identity(candidate['repository']).lower()
                    or file.get('commit') != candidate['commit'] or file.get('path') != path
                    or file.get('evidenceScope') != 'repository-file-at-commit'
                    or not isinstance(file.get('content'), str) or not file['content'].strip()
                    or not isinstance(file.get('blob'), str)
                    or not re.fullmatch(r'[a-f0-9]{40}', file.get('blob', ''))):
                raise ValueError('Matching source file evidence required')
            cached[path] = file['blob']
        receipts.append({**spec, 'blob': cached[path]})
    return receipts


def verify_source_receipts(candidate, receipts):
    specs = source_builds(candidate)
    if not specs and receipts is None:
        return
    if not isinstance(receipts, list) or len(receipts) != len(specs):
        raise ValueError('Source build evidence required')
    for spec, receipt in zip(specs, receipts):
        if spec.get('kind') == 'proposed-dockerfile':
            if receipt != spec:
                raise ValueError('Proposed Dockerfile changed')
            continue
        if (not isinstance(receipt, dict) or set(receipt) != {'service', 'path', 'blob'}
                or {key: receipt[key] for key in spec} != spec
                or not isinstance(receipt['blob'], str) or not re.fullmatch(r'[a-f0-9]{40}', receipt['blob'])):
            raise ValueError('Source build evidence changed')