|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Ubuntu restricts user namespaces for downloaded Chromium. Keep Chromium's |
| 5 | +# sandbox enabled and allow only the two executables installed for this CI job. |
| 6 | +: "${RUNNER_TEMP:?RUNNER_TEMP is required}" |
| 7 | +: "${PLAYWRIGHT_BROWSERS_PATH:?PLAYWRIGHT_BROWSERS_PATH is required}" |
| 8 | +expected_root="$RUNNER_TEMP/openai-python-playwright" |
| 9 | +if [[ "$PLAYWRIGHT_BROWSERS_PATH" != "$expected_root" || ! -d "$expected_root" ]]; then |
| 10 | + echo "Playwright must be installed inside this job's RUNNER_TEMP." >&2 |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | +runner_root="$(realpath "$RUNNER_TEMP")" |
| 14 | +install_root="$(realpath "$expected_root")" |
| 15 | +# AppArmor uses these paths in its policy syntax, without globs or expansions. |
| 16 | +if [[ "$install_root" != "$runner_root/openai-python-playwright" || ! "$install_root" =~ ^/[a-zA-Z0-9_./-]+$ ]]; then |
| 17 | + echo "Playwright installation escaped RUNNER_TEMP or contains unexpected path characters." >&2 |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | +shopt -s nullglob |
| 21 | +chromiums=("$install_root"/chromium-*/chrome-linux64/chrome) |
| 22 | +headless_shells=("$install_root"/chromium_headless_shell-*/chrome-headless-shell-linux64/chrome-headless-shell) |
| 23 | +if [[ "${#chromiums[@]}" -ne 1 || "${#headless_shells[@]}" -ne 1 ]]; then |
| 24 | + echo "Expected one installed Chromium and one installed headless shell." >&2 |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | +binaries=("${chromiums[0]}" "${headless_shells[0]}") |
| 28 | +for index in "${!binaries[@]}"; do |
| 29 | + binary="$(realpath "${binaries[index]}")" |
| 30 | + if [[ ! -x "$binary" || "$binary" != "$install_root/"* || ! "$binary" =~ ^/[a-zA-Z0-9_./-]+$ ]]; then |
| 31 | + echo "Chromium executable is absent or outside this job's installation." >&2 |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | + binaries[index]="$binary" |
| 35 | +done |
| 36 | +{ |
| 37 | + printf 'abi <abi/4.0>,\ninclude <tunables/global>\n' |
| 38 | + for index in "${!binaries[@]}"; do |
| 39 | + printf 'profile openai-python-playwright-%s "%s" flags=(unconfined) {\n userns,\n}\n' "$index" "${binaries[index]}" |
| 40 | + done |
| 41 | +} | sudo tee /etc/apparmor.d/openai-python-playwright >/dev/null |
| 42 | +sudo apparmor_parser -r /etc/apparmor.d/openai-python-playwright |
0 commit comments