From 7c92ae36c9d4db947a1dcc7aaa358dcd9ace8a4b Mon Sep 17 00:00:00 2001 From: jack <212554440+jackjackbits@users.noreply.github.com> Date: Thu, 26 Nov 2026 20:08:10 -1000 Subject: [PATCH] Harden CI workflows, Arti provenance gate, build script, and Xcode SwiftPM pinning (#1887) --- .github/workflows/arti-provenance.yml | 33 ++-- .github/workflows/source-manifest.yml | 20 ++- .github/workflows/swift-tests.yml | 46 +++++- .gitignore | 7 +- .../xcshareddata/swiftpm/Package.resolved | 14 ++ docs/ARTI-BINARY-PROVENANCE.md | 2 +- localPackages/Arti/build-ios.sh | 22 +-- .../tests/test_ci_supply_chain_workflows.py | 156 ++++++++++++++++++ 8 files changed, 262 insertions(+), 38 deletions(-) create mode 100644 bitchat.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved create mode 100644 scripts/tests/test_ci_supply_chain_workflows.py diff --git a/.github/workflows/arti-provenance.yml b/.github/workflows/arti-provenance.yml index 5d50564e..17c1b141 100644 --- a/.github/workflows/arti-provenance.yml +++ b/.github/workflows/arti-provenance.yml @@ -3,8 +3,8 @@ name: Arti Binary Provenance # The Arti xcframework is a vendored binary; these checks turn the policy in # docs/ARTI-BINARY-PROVENANCE.md into an enforced gate: # 1. The checked-in binary must match the hash manifest in the provenance doc. -# 2. A PR that changes the binary must also change at least one provenance -# input (Rust source, lockfile, build script, or the doc itself). +# 2. A PR that changes the binary must also change at least one build input +# (Rust source, Cargo manifest/lockfile, or build script). on: push: @@ -13,10 +13,15 @@ on: paths: - "localPackages/Arti/**" - "docs/ARTI-BINARY-PROVENANCE.md" + - ".github/workflows/arti-provenance.yml" pull_request: paths: - "localPackages/Arti/**" - "docs/ARTI-BINARY-PROVENANCE.md" + - ".github/workflows/arti-provenance.yml" + +permissions: + contents: read jobs: verify-hashes: @@ -25,7 +30,9 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v5 + uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 + with: + persist-credentials: false - name: Compare artifact hashes with manifest run: | @@ -41,8 +48,9 @@ jobs: exit 1 fi - # Hash the same file set the doc documents. - find localPackages/Arti/Frameworks/arti.xcframework -maxdepth 3 -type f -print0 \ + # Hash every file in arti.xcframework so deeper subdirectories cannot + # hide unmanifested files. + find localPackages/Arti/Frameworks/arti.xcframework -type f -print0 \ | sort -z | xargs -0 sha256sum | sed 's/ \.\// /' | sort -k2 > actual.txt if ! diff -u expected.txt actual.txt; then @@ -58,15 +66,18 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v5 + uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 with: fetch-depth: 0 + persist-credentials: false - name: Check changed files + env: + BASE_REF: ${{ github.base_ref }} run: | set -euo pipefail - base="origin/${{ github.base_ref }}" - git fetch --no-tags --depth=1 origin "${{ github.base_ref }}" + base="origin/$BASE_REF" + git fetch --no-tags --depth=1 origin "$BASE_REF" changed=$(git diff --name-only "$base"...HEAD) echo "Changed files:" echo "$changed" @@ -76,10 +87,10 @@ jobs: exit 0 fi - if echo "$changed" | grep -Eq '^(localPackages/Arti/(Cargo\.(toml|lock)|build-ios\.sh|arti-bitchat/)|docs/ARTI-BINARY-PROVENANCE\.md)'; then - echo "Binary change is accompanied by provenance inputs." + if echo "$changed" | grep -Eq '^localPackages/Arti/(Cargo\.(toml|lock)|build-ios\.sh|arti-bitchat/)'; then + echo "Binary change is accompanied by build provenance inputs." exit 0 fi - echo "::error::arti.xcframework changed without matching source, lockfile, build-script, or provenance-doc changes. See docs/ARTI-BINARY-PROVENANCE.md (\"Do not accept an xcframework-only update\")." + echo "::error::arti.xcframework changed without matching source, lockfile, or build-script changes. See docs/ARTI-BINARY-PROVENANCE.md (\"Do not accept an xcframework-only update\")." exit 1 diff --git a/.github/workflows/source-manifest.yml b/.github/workflows/source-manifest.yml index 0131c066..ff2c60fd 100644 --- a/.github/workflows/source-manifest.yml +++ b/.github/workflows/source-manifest.yml @@ -32,17 +32,20 @@ jobs: id-token: write # provenance attestation attestations: write steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 with: ref: ${{ github.event.inputs.ref || github.ref }} # Full history so the commit the tag names can be recorded exactly. fetch-depth: 0 + persist-credentials: false - name: Build manifest id: build + env: + REF_NAME: ${{ github.event.inputs.ref || github.ref_name }} run: | set -euo pipefail - ref_name="${{ github.event.inputs.ref || github.ref_name }}" + ref_name="$REF_NAME" commit="$(git rev-parse HEAD)" tree="$(git rev-parse HEAD^{tree})" @@ -56,7 +59,7 @@ jobs: { echo "# bitchat source manifest" echo "#" - echo "# ref: ${ref_name}" + printf '# ref: %s\n' "${ref_name}" echo "# commit: ${commit}" echo "# tree: ${tree}" echo "# files: $(wc -l < files.sha256 | tr -d ' ')" @@ -88,11 +91,11 @@ jobs: echo "manifest validates against this checkout" - name: Attest the manifest - uses: actions/attest-build-provenance@v1 + uses: actions/attest-build-provenance@ef244123eb79f2f7a7e75d99086184180e6d0018 # v1 with: subject-path: SOURCE-MANIFEST.txt - - uses: actions/upload-artifact@v4 + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 with: name: source-manifest path: SOURCE-MANIFEST.txt @@ -101,12 +104,13 @@ jobs: if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') env: GH_TOKEN: ${{ github.token }} + TAG_NAME: ${{ github.ref_name }} run: | set -euo pipefail # A tag may be pushed before its release exists; only attach when # there is a release to attach to, and never fail the run over it. - if gh release view "${{ github.ref_name }}" >/dev/null 2>&1; then - gh release upload "${{ github.ref_name }}" SOURCE-MANIFEST.txt --clobber + if gh release view "$TAG_NAME" >/dev/null 2>&1; then + gh release upload "$TAG_NAME" SOURCE-MANIFEST.txt --clobber else - echo "no release for ${{ github.ref_name }} yet; manifest is available as a workflow artifact" + printf 'no release for %s yet; manifest is available as a workflow artifact\n' "$TAG_NAME" fi diff --git a/.github/workflows/swift-tests.yml b/.github/workflows/swift-tests.yml index e64683de..fac7f5d9 100644 --- a/.github/workflows/swift-tests.yml +++ b/.github/workflows/swift-tests.yml @@ -6,6 +6,9 @@ on: - main pull_request: +permissions: + contents: read + jobs: test: name: Run Swift Tests (${{ matrix.name }}) @@ -30,7 +33,9 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v5 + uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 + with: + persist-credentials: false # Use the Xcode-bundled Swift toolchain: it always matches the SDK on # the runner image. A standalone swift.org toolchain (setup-swift) broke @@ -41,7 +46,7 @@ jobs: run: echo "version=$(swift --version 2>/dev/null | head -1 | shasum | cut -c1-12)" >> "$GITHUB_OUTPUT" - name: Cache build artifacts - uses: actions/cache@v4 + uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4 with: path: ${{ matrix.path }}/.build key: ${{ runner.os }}-${{ steps.swift-version.outputs.version }}-${{ matrix.name }}-${{ hashFiles(format('{0}/**/*.swift', matrix.path), format('{0}/**/Package.resolved', matrix.path)) }} @@ -138,15 +143,35 @@ jobs: ios-build: name: Build Release apps (universal) runs-on: macos-latest - timeout-minutes: 15 + timeout-minutes: 20 steps: - name: Checkout code - uses: actions/checkout@v5 + uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 + with: + persist-credentials: false - name: Check clean recipe safety run: bash scripts/check-just-clean-safety.sh + - name: Run workflow and script unit tests + run: python3 -m unittest discover -s scripts/tests -v + + - name: Build iOS (device, no signing) + # Validate linking against the shipping ios-arm64 vendored Arti slice. + run: | + set -o pipefail + xcodebuild -project bitchat.xcodeproj \ + -scheme "bitchat (iOS)" \ + -configuration Release \ + -sdk iphoneos \ + -destination 'generic/platform=iOS' \ + ARCHS='arm64' \ + ONLY_ACTIVE_ARCH=NO \ + CODE_SIGNING_ALLOWED=NO \ + -onlyUsePackageVersionsFromResolvedFile \ + build + - name: Build iOS (simulator, no signing) # Build both simulator architectures so CI validates every vendored # Arti simulator slice and the configuration that ships. @@ -160,6 +185,7 @@ jobs: ARCHS='arm64 x86_64' \ ONLY_ACTIVE_ARCH=NO \ CODE_SIGNING_ALLOWED=NO \ + -onlyUsePackageVersionsFromResolvedFile \ build - name: Build macOS (universal, no signing) @@ -172,6 +198,7 @@ jobs: ARCHS='arm64 x86_64' \ ONLY_ACTIVE_ARCH=NO \ CODE_SIGNING_ALLOWED=NO \ + -onlyUsePackageVersionsFromResolvedFile \ build # The SwiftPM matrix runs on macOS and cannot execute UIKit/CoreBluetooth @@ -188,7 +215,9 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v5 + uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 + with: + persist-credentials: false # Some runner images list only placeholder destinations (rows carrying # "error:" or "unavailable") or ship the selected Xcode without a @@ -254,14 +283,17 @@ jobs: echo "id=$destination_id" >> "$GITHUB_OUTPUT" - name: Run iOS tests + env: + DESTINATION_ID: ${{ steps.destination.outputs.id }} run: | set -o pipefail xcodebuild -project bitchat.xcodeproj \ -scheme "bitchat (iOS)" \ -sdk iphonesimulator \ - -destination "platform=iOS Simulator,id=${{ steps.destination.outputs.id }}" \ + -destination "platform=iOS Simulator,id=$DESTINATION_ID" \ -parallel-testing-enabled NO \ CODE_SIGNING_ALLOWED=NO \ + -onlyUsePackageVersionsFromResolvedFile \ test # Blocking since the backlog reached zero (all violations fixed; rules the @@ -283,7 +315,7 @@ jobs: # Bump both together, deliberately — never a floating tag. image: ghcr.io/realm/swiftlint:0.65.0@sha256:a482729f4b58741875af1566f23397f3f6db300372756fc31606d0a4527fab9e steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 with: persist-credentials: false - name: Run SwiftLint diff --git a/.gitignore b/.gitignore index ac9e4d89..3b782cfa 100644 --- a/.gitignore +++ b/.gitignore @@ -54,7 +54,12 @@ fastlane/test_output iOSInjectionProject/ ## Xcode project -*.xcodeproj/project.xcworkspace/ +*.xcodeproj/project.xcworkspace/* +!*.xcodeproj/project.xcworkspace/xcshareddata/ +*.xcodeproj/project.xcworkspace/xcshareddata/* +!*.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/ +*.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/* +!*.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved ## Xcode User settings xcuserdata/ diff --git a/bitchat.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/bitchat.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved new file mode 100644 index 00000000..e2fe84fe --- /dev/null +++ b/bitchat.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -0,0 +1,14 @@ +{ + "pins" : [ + { + "identity" : "swift-secp256k1", + "kind" : "remoteSourceControl", + "location" : "https://github.com/21-DOT-DEV/swift-secp256k1", + "state" : { + "revision" : "bcd6dd8e615ce243eab7097f213c8c4b39305a2e", + "version" : "0.24.0" + } + } + ], + "version" : 2 +} diff --git a/docs/ARTI-BINARY-PROVENANCE.md b/docs/ARTI-BINARY-PROVENANCE.md index 9f44db3c..2d1d7a3b 100644 --- a/docs/ARTI-BINARY-PROVENANCE.md +++ b/docs/ARTI-BINARY-PROVENANCE.md @@ -70,7 +70,7 @@ The build script now normalizes static-archive metadata and writes a stable xcfr Run this from the repo root to verify the checked-in artifact: ```sh -find localPackages/Arti/Frameworks/arti.xcframework -maxdepth 3 -type f -print0 | sort -z | xargs -0 shasum -a 256 +find localPackages/Arti/Frameworks/arti.xcframework -type f -print0 | sort -z | xargs -0 shasum -a 256 ``` Current hashes: diff --git a/localPackages/Arti/build-ios.sh b/localPackages/Arti/build-ios.sh index 97278a48..5fb3109f 100755 --- a/localPackages/Arti/build-ios.sh +++ b/localPackages/Arti/build-ios.sh @@ -70,7 +70,7 @@ check_prerequisites() { # Check/install targets for target in "${TARGETS[@]}"; do - if ! rustup target list --toolchain "$RUST_TOOLCHAIN" --installed | grep -q "$target"; then + if ! rustup target list --toolchain "$RUST_TOOLCHAIN" --installed | grep -qx "$target"; then log_info "Installing target: $target" rustup target add --toolchain "$RUST_TOOLCHAIN" "$target" fi @@ -122,8 +122,8 @@ build_target() { setup_rustflags "$target" - # Build release - rustup run "$RUST_TOOLCHAIN" cargo build --release --target "$target" -p "$CRATE_NAME" + # Build release with locked Cargo.lock for reproducible dependency resolution + rustup run "$RUST_TOOLCHAIN" cargo build --release --locked --target "$target" -p "$CRATE_NAME" # Check output local lib_path="target/$target/release/$LIB_NAME" @@ -192,14 +192,16 @@ create_xcframework() { mv "$normalized_macos" "$macos_lib" local header_dir="$OUTPUT_DIR/include" - local cmd="xcodebuild -create-xcframework" - cmd="$cmd -library $SCRIPT_DIR/target/aarch64-apple-ios/release/$LIB_NAME -headers $header_dir" - cmd="$cmd -library $simulator_lib -headers $header_dir" - cmd="$cmd -library $macos_lib -headers $header_dir" - cmd="$cmd -output $xcframework_path" + local xcframework_args=( + -create-xcframework + -library "$SCRIPT_DIR/target/aarch64-apple-ios/release/$LIB_NAME" -headers "$header_dir" + -library "$simulator_lib" -headers "$header_dir" + -library "$macos_lib" -headers "$header_dir" + -output "$xcframework_path" + ) - log_info "Running: $cmd" - eval "$cmd" + log_info "Running: xcodebuild ${xcframework_args[*]}" + xcodebuild "${xcframework_args[@]}" if [[ -d "$xcframework_path" ]]; then cat > "$xcframework_path/Info.plist" << 'EOF' diff --git a/scripts/tests/test_ci_supply_chain_workflows.py b/scripts/tests/test_ci_supply_chain_workflows.py new file mode 100644 index 00000000..2f78bf95 --- /dev/null +++ b/scripts/tests/test_ci_supply_chain_workflows.py @@ -0,0 +1,156 @@ +import re +from pathlib import Path +import unittest + + +REPOSITORY_ROOT = Path(__file__).resolve().parents[2] +SOURCE_MANIFEST_WORKFLOW = REPOSITORY_ROOT / ".github/workflows/source-manifest.yml" +ARTI_PROVENANCE_WORKFLOW = REPOSITORY_ROOT / ".github/workflows/arti-provenance.yml" +SWIFT_TESTS_WORKFLOW = REPOSITORY_ROOT / ".github/workflows/swift-tests.yml" +BUILD_IOS_SCRIPT = REPOSITORY_ROOT / "localPackages/Arti/build-ios.sh" +ROOT_PACKAGE_RESOLVED = REPOSITORY_ROOT / "Package.resolved" +XCODE_PACKAGE_RESOLVED = ( + REPOSITORY_ROOT + / "bitchat.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved" +) + +SHA_PINNED_USES = re.compile(r"uses:\s*[\w.-]+/[\w.-]+@[0-9a-f]{40}\b") +ANY_USES = re.compile(r"uses:\s*([^\s#]+)") + + +def strip_comments(text: str) -> str: + return "\n".join( + line for line in text.splitlines() if not line.lstrip().startswith("#") + ) + + +def extract_run_blocks(text: str) -> list[str]: + blocks: list[str] = [] + lines = text.splitlines() + i = 0 + while i < len(lines): + line = lines[i] + match = re.match(r"^(\s*)run:\s*(.*)$", line) + if not match: + i += 1 + continue + indent = len(match.group(1)) + rest = match.group(2).strip() + if rest in ("|", ">", "|-", ">-"): + block_lines: list[str] = [] + i += 1 + while i < len(lines): + current = lines[i] + if current.strip() == "": + block_lines.append("") + i += 1 + continue + current_indent = len(current) - len(current.lstrip(" ")) + if current_indent <= indent: + break + block_lines.append(current) + i += 1 + blocks.append("\n".join(block_lines)) + else: + blocks.append(rest) + i += 1 + return blocks + + +class SourceManifestWorkflowTests(unittest.TestCase): + @classmethod + def setUpClass(cls) -> None: + cls.workflow = SOURCE_MANIFEST_WORKFLOW.read_text(encoding="utf-8") + cls.directives = strip_comments(cls.workflow) + + def test_no_expression_interpolation_in_run_blocks(self) -> None: + for block in extract_run_blocks(self.directives): + self.assertNotIn( + "${{", + block, + "source-manifest.yml must pass untrusted inputs via env vars, never inline ${{ }} in run:", + ) + + def test_all_actions_are_pinned_by_commit_sha(self) -> None: + uses_matches = ANY_USES.findall(self.directives) + self.assertGreaterEqual(len(uses_matches), 3) + for line in self.directives.splitlines(): + if "uses:" in line: + self.assertRegex(line, SHA_PINNED_USES) + + def test_checkout_disables_persisted_credentials(self) -> None: + self.assertIn("persist-credentials: false", self.directives) + + +class ArtiProvenanceWorkflowTests(unittest.TestCase): + @classmethod + def setUpClass(cls) -> None: + cls.workflow = ARTI_PROVENANCE_WORKFLOW.read_text(encoding="utf-8") + cls.directives = strip_comments(cls.workflow) + + def test_workflow_declares_read_only_permissions(self) -> None: + self.assertIn("permissions:\n contents: read", self.directives) + + def test_workflow_triggers_on_self_modification(self) -> None: + self.assertEqual( + 2, + self.directives.count(".github/workflows/arti-provenance.yml"), + "Both push and pull_request path filters must include arti-provenance.yml", + ) + + def test_hashes_entire_xcframework_tree_without_maxdepth(self) -> None: + self.assertNotIn("-maxdepth", self.directives) + self.assertIn( + "find localPackages/Arti/Frameworks/arti.xcframework -type f -print0", + self.directives, + ) + + def test_binary_change_requires_build_input_not_doc_only(self) -> None: + self.assertIn( + "grep -Eq '^localPackages/Arti/(Cargo\\.(toml|lock)|build-ios\\.sh|arti-bitchat/)'", + self.directives, + ) + self.assertNotIn( + "|docs/ARTI-BINARY-PROVENANCE\\.md)", + self.directives, + ) + + def test_no_expression_interpolation_in_run_blocks(self) -> None: + for block in extract_run_blocks(self.directives): + self.assertNotIn("${{", block) + + def test_all_actions_are_pinned_by_commit_sha(self) -> None: + for line in self.directives.splitlines(): + if "uses:" in line: + self.assertRegex(line, SHA_PINNED_USES) + + +class BuildIosAndSwiftTestsHardeningTests(unittest.TestCase): + def test_build_ios_uses_locked_exact_target_match_and_no_eval(self) -> None: + script = strip_comments(BUILD_IOS_SCRIPT.read_text(encoding="utf-8")) + self.assertIn("grep -qx \"$target\"", script) + self.assertIn("cargo build --release --locked", script) + self.assertNotRegex(script, r"\beval\b") + + def test_xcode_package_resolved_matches_root_package_resolved(self) -> None: + self.assertTrue( + XCODE_PACKAGE_RESOLVED.exists(), + "Xcode SwiftPM Package.resolved must be committed to pin remote package revisions", + ) + self.assertEqual( + ROOT_PACKAGE_RESOLVED.read_text(encoding="utf-8"), + XCODE_PACKAGE_RESOLVED.read_text(encoding="utf-8"), + ) + + def test_swift_tests_workflow_checks_ios_device_and_pins_actions(self) -> None: + directives = strip_comments(SWIFT_TESTS_WORKFLOW.read_text(encoding="utf-8")) + self.assertIn("permissions:\n contents: read", directives) + self.assertIn("-sdk iphoneos", directives) + self.assertIn("-onlyUsePackageVersionsFromResolvedFile", directives) + for line in directives.splitlines(): + if "uses:" in line: + self.assertRegex(line, SHA_PINNED_USES) + + +if __name__ == "__main__": + unittest.main()