Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion .github/scripts/build-signed-android.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,31 @@ set -euo pipefail
: "${RUNNER_TEMP:?RUNNER_TEMP must be set}"
umask 077
signing_dir=$(mktemp -d "$RUNNER_TEMP/wheelplay-signing.XXXXXX")
trap 'rm -rf -- "$signing_dir"' EXIT
ca_dir=''
cleanup() {
rm -rf -- "$signing_dir"
[[ -z $ca_dir ]] || rm -rf -- "$ca_dir"
}
trap cleanup EXIT
export ANDROID_KEYSTORE_PATH="$signing_dir/wheelplay.jks"
printf '%s' "$ANDROID_KEYSTORE_BASE64" | base64 --decode > "$ANDROID_KEYSTORE_PATH"
unset ANDROID_KEYSTORE_BASE64

# Optional bundled HTTPS signing CA. A caller may already have provisioned one
# (for example the Debug workflow exports WHEELPLAY_HTTPS_CA_PEM into GITHUB_ENV);
# otherwise this helper validates the secret or the fallback and reports a
# temporary path that the trap above deletes together with the keystore.
want_ca=0
if [[ -n ${WHEELPLAY_HTTPS_CA_PEM_BASE64:-} ]]; then want_ca=1; fi
if [[ ${WHEELPLAY_HTTPS_CA_REQUIRED:-0} == 1 ]]; then want_ca=1; fi
if [[ ${WHEELPLAY_HTTPS_CA_FALLBACK:-} == mitmproxy ]]; then want_ca=1; fi
if [[ -z ${WHEELPLAY_HTTPS_CA_PEM:-} && $want_ca == 1 ]]; then
ca_dir=$(mktemp -d "$RUNNER_TEMP/wheelplay-https-ca.XXXXXX")
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
WHEELPLAY_HTTPS_CA_PEM=$(bash "$script_dir/provision-https-ca.sh" --output-dir "$ca_dir")
export WHEELPLAY_HTTPS_CA_PEM
unset WHEELPLAY_HTTPS_CA_PEM_BASE64
fi

# Configuration cache can serialize signing passwords; never save it for signed builds.
./gradlew --no-daemon --no-configuration-cache "$@"
41 changes: 41 additions & 0 deletions .github/scripts/provision-https-ca-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
set -euo pipefail

# Provision the bundled HTTPS signing CA for a build step that is not
# build-signed-android.sh (the Debug workflow), and export its path to later steps.
#
# Writes WHEELPLAY_HTTPS_CA_PEM into $GITHUB_ENV and leaves the material in
# $RUNNER_TEMP; the workflow must delete it afterwards (see the cleanup step in
# debug-build.yml). No CA is exported when none was requested.
#
# WHEELPLAY_HTTPS_CA_PEM_BASE64 / _REQUIRED / _FALLBACK behave exactly as in
# provision-https-ca.sh; see docs/HTTPS_SIGNING_CA.md.

: "${RUNNER_TEMP:?RUNNER_TEMP must be set}"
: "${GITHUB_ENV:?GITHUB_ENV must be set}"

announce() {
if [[ -n ${GITHUB_ACTIONS:-} ]]; then
printf '::%s::%s\n' "$1" "$2" >&2
else
printf '%s\n' "$2" >&2
fi
}

script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
umask 077
ca_dir=$(mktemp -d "$RUNNER_TEMP/wheelplay-https-ca.XXXXXX")

if ! pem=$(bash "$script_dir/provision-https-ca.sh" --output-dir "$ca_dir"); then
rm -rf -- "$ca_dir"
exit 1
fi

if [[ -z $pem ]]; then
rm -rf -- "$ca_dir"
announce notice 'No bundled HTTPS CA for this build.'
exit 0
fi

# Only the path is exported; the key stays in the runner's temporary directory.
printf 'WHEELPLAY_HTTPS_CA_PEM=%s\n' "$pem" >> "$GITHUB_ENV"
130 changes: 130 additions & 0 deletions .github/scripts/provision-https-ca.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/usr/bin/env bash
set -euo pipefail

# Restore the optional bundled HTTPS signing CA for a CI build.
#
# Prints the absolute PEM path on success and nothing when no CA is requested.
# The caller owns --output-dir and must delete it; this helper only writes there.
# Combined format: one unencrypted RSA/EC private key, then the ordered CA path
# (see docs/HTTPS_SIGNING_CA.md). The checks below mirror BundleHttpsCa in
# mobile/build.gradle.kts so bad material fails before Gradle starts instead of
# after a full toolchain setup.
#
# WHEELPLAY_HTTPS_CA_PEM_BASE64 Base64 of the combined PEM (repository secret).
# WHEELPLAY_HTTPS_CA_REQUIRED 1 requires some bundled CA: the secret or the fallback.
# WHEELPLAY_HTTPS_CA_FALLBACK Only the literal "mitmproxy" enables the pinned
# mitmproxy source; the Release workflow sets it by default.

readonly max_bytes=$((2 * 1024 * 1024))
readonly usage='usage: provision-https-ca.sh --output-dir DIR'

# Pinned so the fallback cannot silently follow upstream changes.
readonly fallback_commit=82416906e3ac55b88d215500adf40959e237589d
readonly fallback_url="https://raw.githubusercontent.com/mhils/mitmproxy/$fallback_commit/mitmproxy/certs.py"
readonly fallback_marker='EMUDHRA_CHAIN_PEM = b"""'

output_dir=''
while (($# > 0)); do
case $1 in
--output-dir)
(($# >= 2)) || { printf '%s\n' "provision-https-ca.sh: --output-dir needs a value" >&2; exit 2; }
output_dir=$2
shift 2
;;
-h|--help)
printf '%s\n' "$usage"
exit 0
;;
*)
printf '%s\n' "provision-https-ca.sh: unknown argument: $1" "$usage" >&2
exit 2
;;
esac
done
[[ -n $output_dir ]] || { printf '%s\n' "provision-https-ca.sh: --output-dir is required" "$usage" >&2; exit 2; }

# Actions annotations when running in Actions, plain diagnostics otherwise.
announce() {
if [[ -n ${GITHUB_ACTIONS:-} ]]; then
printf '::%s::%s\n' "$1" "$2" >&2
else
printf '%s\n' "$2" >&2
fi
}
fail() { announce error "$1"; exit 1; }

# Bundled CA from the fallback is public material, so the log records its source
# without printing any key bytes.
notify_source() {
announce notice 'Bundled HTTPS CA comes from the pinned mitmproxy source (public key material, no identity guarantee).'
}

encoded=${WHEELPLAY_HTTPS_CA_PEM_BASE64:-}
required=${WHEELPLAY_HTTPS_CA_REQUIRED:-0}
fallback=${WHEELPLAY_HTTPS_CA_FALLBACK:-}

ca_source=''
if [[ -n $encoded ]]; then
ca_source=secret
elif [[ $fallback == mitmproxy ]]; then
ca_source=mitmproxy
elif [[ $required == 1 ]]; then
fail 'WHEELPLAY_HTTPS_CA_REQUIRED=1 but there is no WHEELPLAY_HTTPS_CA_PEM_BASE64 secret and no mitmproxy fallback.'
else
announce notice 'No WHEELPLAY_HTTPS_CA_PEM_BASE64 secret; building without a bundled HTTPS signing CA.'
exit 0
fi

umask 077
mkdir -p -- "$output_dir"
pem_dir=$(cd -- "$output_dir" && pwd -P)
pem=$pem_dir/emudhra-ca.pem

if [[ $ca_source == secret ]]; then
# Base64 only encodes bytes; GitHub Secrets provides the confidentiality, so the
# value must never be echoed or written into the checkout.
if ! printf '%s' "${encoded//[[:space:]]/}" | base64 --decode > "$pem" 2>/dev/null; then
rm -f -- "$pem"
fail 'WHEELPLAY_HTTPS_CA_PEM_BASE64 is not valid Base64. Re-encode the combined PEM file and update the secret.'
fi
else
notify_source
command -v curl > /dev/null || fail 'WHEELPLAY_HTTPS_CA_FALLBACK=mitmproxy needs curl to read the pinned mitmproxy source.'
download=$pem_dir/certs.py
if ! curl --fail --silent --show-error --location --max-time 60 \
--max-filesize 1048576 --output "$download" "$fallback_url"; then
rm -f -- "$download"
fail "Could not download $fallback_url for the mitmproxy CA fallback."
fi
# Take only the combined PEM constant; the rest of certs.py is irrelevant here.
awk -v marker="$fallback_marker" '
index($0, marker) == 1 { capture = 1; next }
capture && $0 == "\"\"\"" { exit }
capture { sub(/\r$/, ""); print }
' "$download" > "$pem"
rm -f -- "$download"
if ! grep -q -- '-----BEGIN PRIVATE KEY-----' "$pem"; then
rm -f -- "$pem"
fail "The pinned mitmproxy source no longer contains $fallback_marker at $fallback_commit."
fi
fi

size=$(wc -c < "$pem")
if ((size == 0)); then
rm -f -- "$pem"
fail "Bundled HTTPS CA from the $ca_source is empty."
fi
if ((size > max_bytes)); then
rm -f -- "$pem"
fail "HTTPS signing CA PEM is ${size} bytes; the bundled CA is limited to ${max_bytes} bytes (2 MiB)."
fi

key_count=$(grep -c -E '^-----BEGIN (RSA |EC )?PRIVATE KEY-----' "$pem" || true)
cert_count=$(grep -c -E '^-----BEGIN CERTIFICATE-----' "$pem" || true)
if [[ $key_count != 1 ]] || [[ $cert_count == 0 ]] || grep -q 'ENCRYPTED' "$pem"; then
rm -f -- "$pem"
fail "HTTPS signing CA PEM from the $ca_source needs exactly one unencrypted RSA/EC private key and at least one certificate."
fi

# Only the path reaches the build; the key stays in the runner's temporary directory.
printf '%s\n' "$pem"
16 changes: 16 additions & 0 deletions .github/workflows/debug-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ jobs:
- name: Verify SDK installation helper
run: node --test test/ci-android-*.test.js

- name: Verify HTTPS CA provisioning helper
run: node --test test/ci-https-ca.test.js

- name: Install missing Android packages
run: |
bash .github/scripts/install-android-packages.sh \
Expand All @@ -79,6 +82,14 @@ jobs:
"ndk;$ANDROID_NDK_VERSION" \
"cmake;$ANDROID_CMAKE_VERSION"

- name: Provision bundled HTTPS signing CA
env:
WHEELPLAY_HTTPS_CA_PEM_BASE64: ${{ secrets.WHEELPLAY_HTTPS_CA_PEM_BASE64 }}
# Same defaults as the Release build: always bundle a CA, secret first.
WHEELPLAY_HTTPS_CA_REQUIRED: ${{ vars.WHEELPLAY_HTTPS_CA_REQUIRED || '1' }}
WHEELPLAY_HTTPS_CA_FALLBACK: ${{ vars.WHEELPLAY_HTTPS_CA_FALLBACK || 'mitmproxy' }}
run: bash .github/scripts/provision-https-ca-env.sh

- name: Build and lint Debug artifacts
env:
USE_RELEASE_SIGNING: ${{ github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')) }}
Expand Down Expand Up @@ -151,6 +162,11 @@ jobs:
path: mobile/build/reports/lint-results-debug.*
if-no-files-found: warn

# The provisioned CA is job-scoped; remove it once every artifact is uploaded.
- name: Remove provisioned HTTPS signing CA
if: always()
run: rm -rf -- "$RUNNER_TEMP"/wheelplay-https-ca.*

browser-runtime:
name: Browser HTTP on Android ${{ matrix.api-level }}
needs: build
Expand Down
26 changes: 25 additions & 1 deletion .github/workflows/release-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,36 @@ jobs:
ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}
# Optional bundled HTTPS signing CA; see docs/HTTPS_SIGNING_CA.md.
WHEELPLAY_HTTPS_CA_PEM_BASE64: ${{ secrets.WHEELPLAY_HTTPS_CA_PEM_BASE64 }}
# Defaults: a CA is always bundled, taken from the pinned mitmproxy source
# when the secret is absent. Override with 0 / none to disable.
WHEELPLAY_HTTPS_CA_REQUIRED: ${{ vars.WHEELPLAY_HTTPS_CA_REQUIRED || '1' }}
WHEELPLAY_HTTPS_CA_FALLBACK: ${{ vars.WHEELPLAY_HTTPS_CA_FALLBACK || 'mitmproxy' }}
run: bash .github/scripts/build-signed-android.sh :mobile:assembleRelease :mobile:bundleRelease
- name: Verify APK signature
run: '"$ANDROID_HOME/build-tools/36.0.0/apksigner" verify --verbose mobile/build/outputs/apk/release/mobile-release.apk'
- name: Verify bundled HTTPS signing CA
id: bundled-ca
env:
WHEELPLAY_HTTPS_CA_PEM_BASE64: ${{ secrets.WHEELPLAY_HTTPS_CA_PEM_BASE64 }}
WHEELPLAY_HTTPS_CA_FALLBACK: ${{ vars.WHEELPLAY_HTTPS_CA_FALLBACK || 'mitmproxy' }}
run: |
if [[ -z $WHEELPLAY_HTTPS_CA_PEM_BASE64 && $WHEELPLAY_HTTPS_CA_FALLBACK != mitmproxy ]]; then
echo 'No HTTPS CA secret or fallback; this Release has no bundled HTTPS signing CA.'
exit 0
fi
apk=mobile/build/outputs/apk/release/mobile-release.apk
unzip -p "$apk" assets/web-tls/emudhra-ca.pem > /dev/null
if [[ -z $WHEELPLAY_HTTPS_CA_PEM_BASE64 ]]; then
echo 'fallback=true' >> "$GITHUB_OUTPUT"
echo '::notice::Bundled CA comes from the mitmproxy source; see docs/HTTPS_SIGNING_CA.md.'
else
echo 'Bundled HTTPS signing CA is present in the Release APK.'
fi
- uses: actions/upload-artifact@v4
with:
name: wheelplay-release
name: wheelplay-release${{ steps.bundled-ca.outputs.fallback == 'true' && '-MITMPROXY-CA' || '' }}
path: |
mobile/build/outputs/apk/release/mobile-release.apk
mobile/build/outputs/bundle/release/mobile-release.aab
Expand Down
Loading
Loading