93 lines
3.7 KiB
Diff
93 lines
3.7 KiB
Diff
--- a/gitea-api
|
|
+++ b/gitea-api
|
|
@@ -13,0 +14,5 @@
|
|
+if [[ ! "$GITEA_TOKEN" =~ ^[A-Za-z0-9_-]+$ ]]; then
|
|
+ echo "gitea-api: GITEA_TOKEN contains unsupported characters" >&2
|
|
+ exit 1
|
|
+fi
|
|
+
|
|
@@ -30,0 +36,78 @@
|
|
+if [ "$method" = "DOWNLOAD" ]; then
|
|
+ output="$body"
|
|
+ max_bytes=10485760
|
|
+ if [ -z "$output" ] || [ ! -d "$(dirname "$output")" ] || [ -e "$output" ] || [ -L "$output" ]; then
|
|
+ echo "Usage: gitea-api DOWNLOAD /path /new/output-file" >&2
|
|
+ exit 2
|
|
+ fi
|
|
+ case "$GITEA_URL" in
|
|
+ https://*) gitea_host="${GITEA_URL#https://}"; gitea_host="${gitea_host%%/*}"; gitea_origin="https://${gitea_host}" ;;
|
|
+ *) echo "gitea-api: binary downloads require HTTPS" >&2; exit 2 ;;
|
|
+ esac
|
|
+ if [[ ! "$gitea_origin" =~ ^https://[A-Za-z0-9.-]+(:[0-9]+)?$ ]]; then
|
|
+ echo "gitea-api: invalid Gitea origin for binary download" >&2
|
|
+ exit 2
|
|
+ fi
|
|
+ headers="$(mktemp)"
|
|
+ download="$(mktemp "$(dirname "$output")/.gitea-api-download.XXXXXX")"
|
|
+ chmod 600 "$download"
|
|
+ cleanup() { rm -f "$tmp" "$headers" "$download"; }
|
|
+ trap cleanup EXIT
|
|
+
|
|
+ http_code="$(curl -sS -D "$headers" -o /dev/null -w "%{http_code}" --max-filesize 1048576 \
|
|
+ -H "Accept: application/octet-stream" --config - "${base}${path}" <<< "header = \"Authorization: token ${GITEA_TOKEN}\"")" || { echo "gitea-api: artifact metadata request failed" >&2; exit 1; }
|
|
+ if [[ "$http_code" == 2?? ]]; then
|
|
+ set +e
|
|
+ curl -sS --fail --proto '=https' --max-filesize "$max_bytes" \
|
|
+ --config - "$base$path" <<< "header = \"Authorization: token ${GITEA_TOKEN}\"" | head -c "$((max_bytes + 1))" > "$download"
|
|
+ statuses=("${PIPESTATUS[@]}")
|
|
+ set -e
|
|
+ size="$(wc -c < "$download")"
|
|
+ if (( size > max_bytes )); then
|
|
+ echo "gitea-api: artifact exceeds the 10 MiB download limit" >&2
|
|
+ exit 1
|
|
+ fi
|
|
+ if (( statuses[0] != 0 || statuses[1] != 0 )); then
|
|
+ echo "gitea-api: artifact download failed" >&2
|
|
+ exit 1
|
|
+ fi
|
|
+ elif [[ "$http_code" == 302 ]]; then
|
|
+ location="$(awk 'tolower(substr($0,1,9)) == "location:" { sub(/^[^:]*:[[:space:]]*/, ""); sub(/\r$/, ""); value=$0 } END { print value }' "$headers")"
|
|
+ if [[ ! "$location" =~ ^https://([A-Za-z0-9.-]+(:[0-9]+)?)(/[^#]*)?$ ]]; then
|
|
+ echo "gitea-api: artifact redirect is not an absolute HTTPS URL" >&2
|
|
+ exit 1
|
|
+ fi
|
|
+ redirect_origin="https://${BASH_REMATCH[1],,}"
|
|
+ gitea_origin="${gitea_origin,,}"
|
|
+ if [[ "$redirect_origin" != "$gitea_origin" && ",${GITEA_DOWNLOAD_ORIGINS:-}," != *",${redirect_origin},"* ]]; then
|
|
+ echo "gitea-api: artifact redirect origin is not allowlisted" >&2
|
|
+ exit 1
|
|
+ fi
|
|
+ set +e
|
|
+ curl -sS --proto '=https' --max-filesize "$max_bytes" -o - "$location" | head -c "$((max_bytes + 1))" > "$download"
|
|
+ statuses=("${PIPESTATUS[@]}")
|
|
+ set -e
|
|
+ size="$(wc -c < "$download")"
|
|
+ if (( size > max_bytes )); then
|
|
+ echo "gitea-api: artifact exceeds the 10 MiB download limit" >&2
|
|
+ exit 1
|
|
+ fi
|
|
+ if (( statuses[0] != 0 )); then
|
|
+ echo "gitea-api: artifact download failed" >&2
|
|
+ exit 1
|
|
+ fi
|
|
+ else
|
|
+ echo "gitea-api: artifact endpoint returned HTTP $http_code" >&2
|
|
+ exit 1
|
|
+ fi
|
|
+ size="$(wc -c < "$download")"
|
|
+ if (( size > max_bytes )); then
|
|
+ echo "gitea-api: artifact exceeds the 10 MiB download limit" >&2
|
|
+ exit 1
|
|
+ fi
|
|
+ chmod 600 "$download"
|
|
+ ln -- "$download" "$output" || { echo "gitea-api: output file already exists" >&2; exit 1; }
|
|
+ echo "PASS downloaded Gitea artifact (${size} bytes)"
|
|
+ exit 0
|
|
+fi
|
|
+
|
|
@@ -36 +118,0 @@
|
|
- -H "Authorization: token ${GITEA_TOKEN}"
|
|
@@ -47 +129 @@
|
|
-http_code="$(curl "${args[@]}" "${base}${path}")"
|
|
+http_code="$(curl "${args[@]}" --config - "${base}${path}" <<< "header = \"Authorization: token ${GITEA_TOKEN}\"")"
|