~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/vuln-notif

0
0
main
123 lines · 5.1 KB Raw History
#!/usr/bin/env bash
# Create the local e2e account, mint a full-access app password, verify it via
# createSession and the PLC DID document, then record it in
# .state/e2e-account.json. Prints shell exports for ATP_PDS_URL, ATP_HANDLE and
# ATP_APP_PASSWORD on stdout.
set -euo pipefail

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
REPO_DIR=$(cd "$SCRIPT_DIR/.." && pwd)

PDS_URL=${PDS_URL:-${BASE:-http://127.0.0.1:4800}}
PDS_URL=${PDS_URL%/}
PLC_URL=${PLC_URL:-http://127.0.0.1:2587}
PLC_URL=${PLC_URL%/}
HANDLE=${HANDLE:-cve.test}
EMAIL=${EMAIL:-cve@example.com}
PASSWORD=${PASSWORD:-test-password}
APP_PASSWORD_NAME=${APP_PASSWORD_NAME:-vuln-notif-e2e-$$}
STATE_ROOT=${STATE_ROOT:-$REPO_DIR/.state}
STATE_DIR=${STATE_DIR:-$STATE_ROOT}
ACCOUNT_FILE=${ACCOUNT_FILE:-$STATE_DIR/e2e-account.json}

HTTP_STATUS=
HTTP_BODY=

log() { printf '%s\n' "$*" >&2; }
die() { printf 'error: %s\n' "$*" >&2; exit 1; }

http_post() {
  local path=$1 payload=$2 token=${3:-}
  local -a headers=(-H 'Content-Type: application/json')
  if [ -n "$token" ]; then
    headers+=(-H "Authorization: Bearer $token")
  fi
  local response
  response=$(curl -sS --max-time 20 -X POST "${headers[@]}" -d "$payload" \
    -w '\n%{http_code}' "$PDS_URL$path") || return 1
  HTTP_STATUS=${response##*$'\n'}
  HTTP_BODY=${response%$'\n'*}
  case "$HTTP_STATUS" in
  2[0-9][0-9]) return 0 ;;
  *) return 1 ;;
  esac
}

account_payload=$(jq -nc --arg handle "$HANDLE" --arg email "$EMAIL" --arg password "$PASSWORD" \
  '{handle:$handle,email:$email,password:$password}')
account_json=
if http_post /xrpc/com.atproto.server.createAccount "$account_payload"; then
  account_json=$HTTP_BODY
  access_jwt=$(jq -er '.accessJwt' <<<"$account_json") || die "createAccount response has no accessJwt"
  did=$(jq -er '.did' <<<"$account_json") || die "createAccount response has no did"
  log "created account $HANDLE ($did)"
else
  error=$(jq -r '.error // empty' <<<"$HTTP_BODY" 2>/dev/null) || true
  [ "$error" = "AccountAlreadyExists" ] || [ "$error" = "HandleNotAvailable" ] ||
    die "createAccount HTTP $HTTP_STATUS: $HTTP_BODY"
  log "account $HANDLE already exists; logging in with the account password"
  login_payload=$(jq -nc --arg identifier "$HANDLE" --arg password "$PASSWORD" \
    '{identifier:$identifier,password:$password}')
  http_post /xrpc/com.atproto.server.createSession "$login_payload" ||
    die "createSession HTTP $HTTP_STATUS: $HTTP_BODY"
  access_jwt=$(jq -er '.accessJwt' <<<"$HTTP_BODY") || die "createSession response has no accessJwt"
  did=$(jq -er '.did' <<<"$HTTP_BODY") || die "createSession response has no did"
fi

app_payload=$(jq -nc --arg name "$APP_PASSWORD_NAME" '{name:$name,privileged:true}')
http_post /xrpc/com.atproto.server.createAppPassword "$app_payload" "$access_jwt" ||
  die "createAppPassword HTTP $HTTP_STATUS: $HTTP_BODY"
app_json=$HTTP_BODY
app_password=$(jq -er '.password' <<<"$app_json") || die "createAppPassword response has no password"
jq -e '.privileged == true' <<<"$app_json" >/dev/null ||
  die "createAppPassword did not return a privileged password: $app_json"

session_payload=$(jq -nc --arg identifier "$HANDLE" --arg password "$app_password" \
  '{identifier:$identifier,password:$password}')
http_post /xrpc/com.atproto.server.createSession "$session_payload" ||
  die "createSession with app password HTTP $HTTP_STATUS: $HTTP_BODY"
session_json=$HTTP_BODY
session_did=$(jq -er '.did' <<<"$session_json") || die "app-password session response has no did"
[ "$session_did" = "$did" ] ||
  die "app-password session did $session_did does not match account did $did"
jq -e '.accessJwt' <<<"$session_json" >/dev/null ||
  die "app-password session response has no accessJwt"

verify_plc_endpoint() {
  local doc= endpoint= attempt
  for attempt in $(seq 1 40); do
    if doc=$(curl -fsS --max-time 5 "$PLC_URL/$did" 2>/dev/null); then
      endpoint=$(jq -r '[.service[]? | select((.type // "") == "AtprotoPersonalDataServer") | .serviceEndpoint] | first // empty' \
        <<<"$doc" 2>/dev/null) || endpoint=
      if [ "${endpoint%/}" = "$PDS_URL" ]; then
        log "PLC service endpoint verified: $endpoint"
        return 0
      fi
    fi
    sleep 0.25
  done
  die "PLC $PLC_URL/$did does not publish an atproto_pds endpoint of $PDS_URL (last endpoint: ${endpoint:-none})"
}

verify_plc_endpoint

mkdir -p "$(dirname "$ACCOUNT_FILE")"
jq -n \
  --arg pds_url "$PDS_URL" \
  --arg plc_url "$PLC_URL" \
  --arg handle "$HANDLE" \
  --arg email "$EMAIL" \
  --arg did "$did" \
  --arg app_password "$app_password" \
  --arg app_password_name "$APP_PASSWORD_NAME" \
  --argjson account "${account_json:-null}" \
  --argjson app_password_response "$app_json" \
  --argjson session "$session_json" \
  '{pds_url:$pds_url,plc_url:$plc_url,handle:$handle,email:$email,did:$did,app_password:$app_password,app_password_name:$app_password_name,account:$account,app_password_response:$app_password_response,session:$session}' \
  >"$ACCOUNT_FILE"
chmod 600 "$ACCOUNT_FILE"
log "wrote $ACCOUNT_FILE"

printf 'export ATP_PDS_URL=%q\n' "$PDS_URL"
printf 'export ATP_HANDLE=%q\n' "$HANDLE"
printf 'export ATP_APP_PASSWORD=%q\n' "$app_password"