| 1 | 1 | new file mode 100644 |
| index 0000000..e0e2f7e | ||
| --- /dev/null | ||
| +++ b/.gitignore | ||
| @@ -0,0 +1,16 @@ | ||
| 1 | +# build artifacts | |
| 2 | +/plugin_codex_login | |
| 3 | +/plugin_codex_login.exe | |
| 4 | +/plugin_codex_login.musl | |
| 5 | +/plugin_codex_login.arm64 | |
| 6 | +/plugin_codex_login.macos-arm64 | |
| 7 | +/plugin_codex_login.macos-x86_64 | |
| 8 | +bin/ | |
| 9 | + | |
| 10 | +# test scratch | |
| 11 | +*.tmp | |
| 12 | +/tests/fixtures-out/ | |
| 13 | + | |
| 14 | +# python tooling | |
| 15 | +__pycache__/ | |
| 16 | +*.pyc |
| 1 | 1 | new file mode 100644 |
| index 0000000..96648e9 | ||
| --- /dev/null | ||
| +++ b/Makefile | ||
| @@ -0,0 +1,76 @@ | ||
| 1 | +# plugin_codex_login - SCORPIOX BOT open-source OpenAI Codex login for your node | |
| 2 | +# | |
| 3 | +# Targets: | |
| 4 | +# make native build -> ./plugin_codex_login | |
| 5 | +# make musl static musl x86_64 -> ./plugin_codex_login.musl | |
| 6 | +# make arm64 static arm64 musl (aarch64 cross) -> ./plugin_codex_login.arm64 | |
| 7 | +# make win64 MinGW-w64 static -> ./plugin_codex_login.exe | |
| 8 | +# make macos Apple clang -> ./plugin_codex_login (run this on macOS) | |
| 9 | +# make ui regenerate src/ui_html.h from src/ui.html | |
| 10 | +# make test build + run tests/run.sh | |
| 11 | +# make clean | |
| 12 | + | |
| 13 | +CC ?= cc | |
| 14 | +CFLAGS ?= -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings -Wno-format-truncation -I src | |
| 15 | +MUSL_CC ?= musl-gcc | |
| 16 | +MUSL_CFLAGS := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -static | |
| 17 | +ARM_CC ?= aarch64-linux-gnu-gcc | |
| 18 | +ARM_CFLAGS := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -static | |
| 19 | +WIN_CC ?= x86_64-w64-mingw32-gcc | |
| 20 | +WIN_CFLAGS := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -static | |
| 21 | +SRC := src/plugin_codex_login.c | |
| 22 | +UI_SRC := src/ui.html | |
| 23 | +UI_HDR := src/ui_html.h | |
| 24 | +BIN := plugin_codex_login | |
| 25 | +EXE := plugin_codex_login.exe | |
| 26 | + | |
| 27 | +.PHONY: all native musl arm64 win64 macos ui test clean help | |
| 28 | + | |
| 29 | +all: native | |
| 30 | + | |
| 31 | +$(UI_HDR): $(UI_SRC) tools/gen_ui.py | |
| 32 | + @echo "==> regenerating $(UI_HDR)" | |
| 33 | + python3 tools/gen_ui.py | |
| 34 | + | |
| 35 | +ui: $(UI_HDR) | |
| 36 | + | |
| 37 | +native: $(BIN) | |
| 38 | + | |
| 39 | +$(BIN): $(SRC) $(UI_HDR) | |
| 40 | + @echo "==> native build ($(CC))" | |
| 41 | + $(CC) $(CFLAGS) -o $(BIN) $(SRC) | |
| 42 | + | |
| 43 | +# Static musl build - the universal Linux artifact (Alpine, Debian, Arch, Omarchy) | |
| 44 | +musl: $(SRC) $(UI_HDR) | |
| 45 | + @echo "==> static musl build ($(MUSL_CC))" | |
| 46 | + $(MUSL_CC) $(MUSL_CFLAGS) -o $(BIN).musl $(SRC) | |
| 47 | + | |
| 48 | +# Static aarch64 build - Raspberry Pi 5 / ARM64 servers | |
| 49 | +arm64: $(SRC) $(UI_HDR) | |
| 50 | + @echo "==> static arm64 build ($(ARM_CC))" | |
| 51 | + $(ARM_CC) $(ARM_CFLAGS) -o $(BIN).arm64 $(SRC) | |
| 52 | + | |
| 53 | +# Native Win64 static PE - runs on any Windows 10/11 x64 node | |
| 54 | +win64: $(SRC) $(UI_HDR) | |
| 55 | + @echo "==> MinGW-w64 build ($(WIN_CC))" | |
| 56 | + $(WIN_CC) $(WIN_CFLAGS) -o $(EXE) $(SRC) | |
| 57 | + | |
| 58 | +# Apple clang build (run this on macOS) | |
| 59 | +macos: $(SRC) $(UI_HDR) | |
| 60 | + @echo "==> Apple clang build" | |
| 61 | + clang -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -o $(BIN) $(SRC) | |
| 62 | + | |
| 63 | +install: $(BIN) | |
| 64 | + @dest="$${HOME}/.scorpiox/plugins"; [ -d /workspace ] && dest="/workspace/.scorpiox/plugins"; \ | |
| 65 | + mkdir -p "$$dest" && cp $(BIN) "$$dest/$(BIN)" && chmod 0755 "$$dest/$(BIN)" && \ | |
| 66 | + echo "==> installed $$dest/$(BIN)" | |
| 67 | + | |
| 68 | +test: $(BIN) | |
| 69 | + sh tests/run.sh | |
| 70 | + | |
| 71 | +clean: | |
| 72 | + rm -f $(BIN) $(EXE) $(BIN).musl $(BIN).arm64 | |
| 73 | + rm -rf tests/__pycache__ | |
| 74 | + | |
| 75 | +help: | |
| 76 | + @sed -n '2,14p' $(MAKEFILE_LIST) |
| 1 | 1 | new file mode 100644 |
| index 0000000..5a3ca14 | ||
| --- /dev/null | ||
| +++ b/README.md | ||
| @@ -0,0 +1,143 @@ | ||
| 1 | +# plugin_codex_login | |
| 2 | + | |
| 3 | +**OpenAI Codex Login** - a SCORPIOX BOT open-source plugin that manages the | |
| 4 | +OpenAI Codex OAuth session on your node: start a device-code login, read the | |
| 5 | +one-time user code off the panel, watch the token expiry, list named accounts, | |
| 6 | +and refresh the token on demand. | |
| 7 | + | |
| 8 | +One clean-room C99 binary, zero dependencies, no OAuth client secret, and no | |
| 9 | +token ever leaves the node - or even the child process that fetched it. | |
| 10 | + | |
| 11 | +``` | |
| 12 | +--meta {"name":"codex","desc":"Codex login"} | |
| 13 | +?action=status session state, expiry, accounts (JSON) | |
| 14 | +?action=start begin the device flow -> verify_url + user_code | |
| 15 | +?action=start&force=1 replace an existing session | |
| 16 | +?action=poll one non-blocking check on the pending flow | |
| 17 | +?action=cancel drop the pending flow | |
| 18 | +?action=refresh refresh the token now | |
| 19 | +?action=accounts the same status, account list included | |
| 20 | +?format=json same as action=status | |
| 21 | +GET (no action) responsive dark/light HTML UI | |
| 22 | +``` | |
| 23 | + | |
| 24 | +Every action accepts `&account=<name>` to work on a named account instead of | |
| 25 | +the default one. | |
| 26 | + | |
| 27 | +## Design: the plugin drives the CLI, it never speaks TLS | |
| 28 | + | |
| 29 | +A Codex device-code exchange is a sequence of HTTPS POSTs to OpenAI. A public | |
| 30 | +clean-room repository must not carry a client secret or a vendored TLS stack, | |
| 31 | +so this binary makes no network request at all. It runs the node's own login | |
| 32 | +tool through the SCORPIOX CODE machine-mode contract - `--status`, `--start`, | |
| 33 | +`--poll`, `--cancel`, one JSON line per call - and reads the files that tool | |
| 34 | +writes: | |
| 35 | + | |
| 36 | +``` | |
| 37 | +~/.codex/auth.json default account | |
| 38 | +~/.codex/accounts/<name>.json named accounts | |
| 39 | +~/.claude/.login-pending/codex-<name>.json pending device flow | |
| 40 | +~/.claude/scorpiox-env/codex.txt the profile the CLI creates | |
| 41 | +``` | |
| 42 | + | |
| 43 | +## Where the expiry comes from | |
| 44 | + | |
| 45 | +`auth.json` has no expiry field. The access token is a JWT, and a JWT carries | |
| 46 | +its lifetime in the `exp` claim of its base64url payload, so the plugin decodes | |
| 47 | +that payload with its own twelve-line decoder and reports the countdown from | |
| 48 | +it. That is what makes the panel useful on a node where SCORPIOX CODE is not | |
| 49 | +installed: the file is still readable, the countdown is still real, and the | |
| 50 | +login CLI's own `expires_at: 0` never overwrites a computed value. | |
| 51 | + | |
| 52 | +`last_refresh` in the same file is an ISO-8601 UTC stamp and is reported as | |
| 53 | +unix seconds. | |
| 54 | + | |
| 55 | +## Environment overrides | |
| 56 | + | |
| 57 | +| Variable | Default | Meaning | | |
| 58 | +|---|---|---| | |
| 59 | +| `CODEX_CREDS_PATH` | `~/.codex/auth.json` | credential file; also anchors the account and profile paths | | |
| 60 | +| `CODEX_ACCOUNTS_DIR` | `~/.codex/accounts` | named account directory | | |
| 61 | +| `CODEX_PENDING_DIR` | `~/.claude/.login-pending` | pending-flow marker directory | | |
| 62 | +| `CODEX_ENV_DIR` | `~/.claude/scorpiox-env` | profile directory | | |
| 63 | +| `CODEX_LOGIN_BIN` | `scorpiox-codex-login` | login CLI | | |
| 64 | +| `CODEX_REFRESH_BIN` | `scorpiox-codex-refreshtoken` | refresh CLI | | |
| 65 | +| `CODEX_ACCOUNT` | (unset) | account to operate on when no `?account=` is given | | |
| 66 | + | |
| 67 | +A relocated `CODEX_CREDS_PATH` implies a relocated `.codex` tree: the same CLI | |
| 68 | +writes the account files and the profile into one directory, so reading one | |
| 69 | +from the override and the rest from `$HOME` reports an empty state for a | |
| 70 | +session that has one. The pending markers are the exception on purpose - they | |
| 71 | +belong to the bot's own contract, not to the provider, and stay under | |
| 72 | +`~/.claude/.login-pending` where every SCORPIOX CODE login tool puts them. | |
| 73 | + | |
| 74 | +Setting `CODEX_LOGIN_BIN` and `CODEX_REFRESH_BIN` to throwaway scripts is how | |
| 75 | +`tests/run.sh` exercises the whole contract offline, with no network and no | |
| 76 | +real Codex session. | |
| 77 | + | |
| 78 | +## Auth | |
| 79 | + | |
| 80 | +The node API on `:8150` must never be open to the LAN. `--meta` stays | |
| 81 | +unauthenticated so discovery works; every other route requires | |
| 82 | +`X-Bot-Password`, `Authorization: Bearer <pwd>`, or `?pwd=` when the node has | |
| 83 | +`BOT_PASSWORD` set. | |
| 84 | + | |
| 85 | +```bash | |
| 86 | +curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8150/plugin_codex_login | |
| 87 | +# 401 | |
| 88 | +``` | |
| 89 | + | |
| 90 | +## HTTP header rule | |
| 91 | + | |
| 92 | +This binary never emits a `Content-Length` header. It sits behind Caddy, which | |
| 93 | +returns 502 when a CGI response announces a length that does not match the | |
| 94 | +body. Every response here is written with `Cache-Control: no-store` and no | |
| 95 | +declared length, and the test suite fails if the string `Content-Length` ever | |
| 96 | +appears in the source. | |
| 97 | + | |
| 98 | +## Build | |
| 99 | + | |
| 100 | +```bash | |
| 101 | +make # native -> ./plugin_codex_login | |
| 102 | +make musl # static musl x86_64 (universal Linux artifact) | |
| 103 | +make arm64 # static aarch64 (Raspberry Pi 5 / ARM64 servers) | |
| 104 | +make win64 # MinGW-w64 static PE | |
| 105 | +make macos # Apple clang (run on macOS) | |
| 106 | +make test # build + tests/run.sh | |
| 107 | +make ui # regenerate src/ui_html.h from src/ui.html | |
| 108 | +``` | |
| 109 | + | |
| 110 | +`make ui` is required after editing `src/ui.html`: `tools/gen_ui.py` turns the | |
| 111 | +page into a C string with two control-byte placeholders (`0x01` = the bot theme | |
| 112 | +prepaint plus the token block, `0x02` = the theme handoff script). Keeping the | |
| 113 | +palette in `src/bot_theme_plugin.h` means a plugin page can never drift from the | |
| 114 | +theme of the bot it lives inside. | |
| 115 | + | |
| 116 | +On Windows, `build.ps1` does the same thing with MinGW-w64 and checks that the | |
| 117 | +output really starts with `MZ` before calling the build a success. | |
| 118 | + | |
| 119 | +## Install | |
| 120 | + | |
| 121 | +```bash | |
| 122 | +make install # copies the binary into ~/.scorpiox/plugins (or /workspace/...) | |
| 123 | +``` | |
| 124 | + | |
| 125 | +Or drop `plugin_codex_login` (`.exe` on Windows) into `~/.scorpiox/plugins/`. | |
| 126 | +SCORPIOX BOT discovers it on the next request with no restart. | |
| 127 | + | |
| 128 | +## Tests | |
| 129 | + | |
| 130 | +```bash | |
| 131 | +make test # or: sh tests/run.sh | |
| 132 | +``` | |
| 133 | + | |
| 134 | +`tests/run.sh` is fully offline. It builds the binary if needed, points the | |
| 135 | +credential paths and both CLIs at fixtures in a temporary directory, and checks | |
| 136 | +the public contract: the exact `--meta` string, the 401 gate on every action, | |
| 137 | +JWT expiry decoding, the pending-marker TTL, named accounts, the device-flow | |
| 138 | +actions, the refresh action, the shape of the HTML page, and a full-git-history | |
| 139 | +scan for secrets, LAN addresses and machine names. | |
| 140 | + | |
| 141 | +## License | |
| 142 | + | |
| 143 | +MIT. Copyright (c) 2026 SCORPIOX INC. |
| 1 | 1 | new file mode 100644 |
| index 0000000..2cde17d | ||
| --- /dev/null | ||
| +++ b/build.ps1 | ||
| @@ -0,0 +1,51 @@ | ||
| 1 | +# build.ps1 - Windows MinGW-w64 static build for plugin_codex_login | |
| 2 | +# | |
| 3 | +# Usage (from the repo root, PowerShell): | |
| 4 | +# powershell -ExecutionPolicy Bypass -File build.ps1 | |
| 5 | +# | |
| 6 | +# Produces .\plugin_codex_login.exe (static PE, runs on any Windows 10/11 x64 node). | |
| 7 | +# Requires x86_64-w64-mingw32-gcc or a WinLibs/UCRT gcc on PATH (or C:\gcc16\bin). | |
| 8 | + | |
| 9 | +$ErrorActionPreference = "Stop" | |
| 10 | +$ScriptDir = $PSScriptRoot | |
| 11 | +Push-Location $ScriptDir | |
| 12 | +try { | |
| 13 | + $Src = Join-Path $ScriptDir "src\plugin_codex_login.c" | |
| 14 | + $Out = Join-Path $ScriptDir "plugin_codex_login.exe" | |
| 15 | + $Cc = "x86_64-w64-mingw32-gcc" | |
| 16 | + | |
| 17 | + if (-not (Test-Path $Src)) { throw "source not found: $Src" } | |
| 18 | + | |
| 19 | + # Prefer MinGW-w64, fall back to a plain gcc (WinLibs / MSYS2 UCRT) if present. | |
| 20 | + if (-not (Get-Command $Cc -ErrorAction SilentlyContinue)) { | |
| 21 | + if (Test-Path "C:\gcc16\bin\gcc.exe") { | |
| 22 | + $Cc = "C:\gcc16\bin\gcc.exe" | |
| 23 | + } elseif (Get-Command gcc -ErrorAction SilentlyContinue) { | |
| 24 | + $Cc = "gcc" | |
| 25 | + } else { | |
| 26 | + throw "no C compiler found (need x86_64-w64-mingw32-gcc or gcc)" | |
| 27 | + } | |
| 28 | + } | |
| 29 | + | |
| 30 | + # Regenerate the page header so the binary always matches src/ui.html. | |
| 31 | + if (Get-Command python -ErrorAction SilentlyContinue) { | |
| 32 | + python tools\gen_ui.py | |
| 33 | + } | |
| 34 | + | |
| 35 | + Write-Host "==> building plugin_codex_login.exe with $Cc" | |
| 36 | + & $Cc -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -static -o $Out $Src | |
| 37 | + if ($LASTEXITCODE -ne 0) { throw "compile failed (exit $LASTEXITCODE)" } | |
| 38 | + | |
| 39 | + if (-not (Test-Path $Out)) { throw "build produced no output" } | |
| 40 | + | |
| 41 | + # Sanity: a real PE starts with 'MZ'. An HTML error page saved as .exe does not. | |
| 42 | + $bytes = [System.IO.File]::ReadAllBytes($Out) | |
| 43 | + if ($bytes.Length -lt 2 -or $bytes[0] -ne 0x4D -or $bytes[1] -ne 0x5A) { | |
| 44 | + throw "output is not a PE executable (missing MZ header)" | |
| 45 | + } | |
| 46 | + | |
| 47 | + Write-Host "==> ok: $Out ($($bytes.Length) bytes)" | |
| 48 | + Write-Host " smoke test: .\plugin_codex_login.exe --meta" | |
| 49 | +} finally { | |
| 50 | + Pop-Location | |
| 51 | +} |
| 1 | 1 | new file mode 100644 |
| index 0000000..b5436c6 | ||
| --- /dev/null | ||
| +++ b/plugin.json | ||
| @@ -0,0 +1,17 @@ | ||
| 1 | +{ | |
| 2 | + "slug": "codex-login", | |
| 3 | + "id": "plugin_codex_login", | |
| 4 | + "name": "codex", | |
| 5 | + "desc": "Codex login", | |
| 6 | + "title": "OpenAI Codex Login", | |
| 7 | + "summary": "OAuth device code and callback login manager for OpenAI Codex on this node. Starts the device flow, shows the one-time user code, polls for approval, then reports session state, token expiry and the named account files - and refreshes the token on demand. The plugin holds no OAuth client secret and never prints, logs or returns a token.", | |
| 8 | + "category": "devtools", | |
| 9 | + "license": "MIT", | |
| 10 | + "capabilities": [ | |
| 11 | + "credentials_read", | |
| 12 | + "oauth_device_flow" | |
| 13 | + ], | |
| 14 | + "trust_notice": "Executes local scorpiox-codex-login on this node. Credentials are saved locally in ~/.codex/ and never leave your node. The plugin spawns scorpiox-codex-login and scorpiox-codex-refreshtoken (both from SCORPIOX CODE) as child processes; those binaries are what talk to OpenAI over TLS. This plugin itself opens no socket and carries no OAuth client secret. It reads only ~/.codex/auth.json, ~/.codex/accounts/, ~/.claude/.login-pending/ and ~/.claude/scorpiox-env/codex.txt, and every response reports token presence and expiry only - never a token value.", | |
| 15 | + "author": "SCORPIOX INC", | |
| 16 | + "homepage": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-codex-login" | |
| 17 | +} |
| index 5552936..0b9118a 100755 |
| 1 | 1 | new file mode 100755 |
| index 0000000..dabe7aa | ||
| --- /dev/null | ||
| +++ b/release-dist.sh | ||
| @@ -0,0 +1,365 @@ | ||
| 1 | +#!/usr/bin/env bash | |
| 2 | +# release-dist.sh - build, scan and publish plugin_codex_login to the plugin registry. | |
| 3 | +# | |
| 4 | +# Two paths: | |
| 5 | +# A. Preferred: if `scorpiox-cli-botplugin` is installed, hand the whole job to it | |
| 6 | +# (`scorpiox-cli-botplugin publish .`), which does the same work with a richer CLI. | |
| 7 | +# B. Standalone (this script): secret scan -> multi-target build -> source.tar.gz -> | |
| 8 | +# manifest.json + SHA256SUMS -> upload -> latest.json / index.json. | |
| 9 | +# | |
| 10 | +# Nothing here contains a host address or a credential. Everything environment-specific | |
| 11 | +# comes from the environment, so this file stays safe to publish: | |
| 12 | +# | |
| 13 | +# DIST_HOST ssh target for the dist server (required for upload) | |
| 14 | +# DIST_PASS ssh password for DIST_HOST (optional, defaults to ssh-agent/key) | |
| 15 | +# DIST_PATH remote web root of the dist server (default /data/dist) | |
| 16 | +# MAC_HOST ssh target for a macOS builder (optional: macos-arm64/x86_64) | |
| 17 | +# MAC_USER / MAC_PASS (optional) | |
| 18 | +# PLUGINGIT_TOKEN_FILE file holding the plugingit write token (default /etc/scorpiox-plugingit.token) | |
| 19 | +# PLUGINGIT_REMOTE public mirror URL to push into | |
| 20 | +# | |
| 21 | +# Usage: | |
| 22 | +# ./release-dist.sh # full release | |
| 23 | +# ./release-dist.sh --dry-run # build + stage in /tmp, no upload | |
| 24 | +# ./release-dist.sh --no-macos # skip the macOS legs | |
| 25 | +# ./release-dist.sh --no-mirror # skip the plugingit mirror | |
| 26 | +# ./release-dist.sh --standalone # use this script even if the CLI is installed | |
| 27 | +# # (needed for plugins that link extra Win32 libs, | |
| 28 | +# # which the CLI's fixed link line cannot express) | |
| 29 | + | |
| 30 | +set -euo pipefail | |
| 31 | + | |
| 32 | +SLUG="codex-login" | |
| 33 | +ID="plugin_codex_login" | |
| 34 | +VERSION="1.0.1" | |
| 35 | +DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins" | |
| 36 | + | |
| 37 | +ROOT="$(cd "$(dirname "$0")" && pwd)" | |
| 38 | +cd "$ROOT" | |
| 39 | + | |
| 40 | +DRY_RUN=0 | |
| 41 | +DO_MACOS=1 | |
| 42 | +DO_MIRROR=1 | |
| 43 | +STANDALONE=0 | |
| 44 | +for arg in "$@"; do | |
| 45 | + case "$arg" in | |
| 46 | + --standalone) STANDALONE=1 ;; | |
| 47 | + --dry-run) DRY_RUN=1 ;; | |
| 48 | + --no-macos) DO_MACOS=0 ;; | |
| 49 | + --no-mirror) DO_MIRROR=0 ;; | |
| 50 | + -h|--help) sed -n '2,32p' "$0"; exit 0 ;; | |
| 51 | + *) echo "unknown argument: $arg" >&2; exit 2 ;; | |
| 52 | + esac | |
| 53 | +done | |
| 54 | + | |
| 55 | +DIST_HOST="${DIST_HOST:-}" | |
| 56 | +DIST_PASS="${DIST_PASS:-}" | |
| 57 | +DIST_PATH="${DIST_PATH:-/data/dist}" | |
| 58 | +PLUGINGIT_TOKEN_FILE="${PLUGINGIT_TOKEN_FILE:-/etc/scorpiox-plugingit.token}" | |
| 59 | +PLUGINGIT_REMOTE="${PLUGINGIT_REMOTE:-https://plugingit.scorpiox.net/scorpiox-bot-plugin-${SLUG}.git}" | |
| 60 | + | |
| 61 | +STAGE="/tmp/${SLUG}-dist-${VERSION}-$$" | |
| 62 | +mkdir -p "$STAGE" | |
| 63 | +trap 'rm -rf "$STAGE"' EXIT | |
| 64 | + | |
| 65 | +say() { printf '\n\033[1m== %s ==\033[0m\n' "$1"; } | |
| 66 | +die() { printf '\033[31mERROR:\033[0m %s\n' "$1" >&2; exit 1; } | |
| 67 | + | |
| 68 | +# ---------------------------------------------------------------- 0. preflight | |
| 69 | +say "0/7 preflight" | |
| 70 | + | |
| 71 | +if [ "$STANDALONE" -eq 0 ] && command -v scorpiox-cli-botplugin >/dev/null 2>&1; then | |
| 72 | + say "using scorpiox-cli-botplugin publish" | |
| 73 | + exec scorpiox-cli-botplugin publish "$ROOT" | |
| 74 | +fi | |
| 75 | +echo " running the standalone pipeline" | |
| 76 | + | |
| 77 | +[ -n "$(git status --porcelain 2>/dev/null)" ] && \ | |
| 78 | + echo " note: working tree is dirty; source.tar.gz comes from the last commit" | |
| 79 | +COMMIT=$(git rev-parse HEAD 2>/dev/null || echo "unknown") | |
| 80 | +echo " version $VERSION" | |
| 81 | +echo " commit $COMMIT" | |
| 82 | + | |
| 83 | +# ---------------------------------------------------------------- 1. secret scan | |
| 84 | +say "1/7 secret & LAN scan (full history)" | |
| 85 | + | |
| 86 | +SCAN_TMP="$STAGE/history.txt" | |
| 87 | +# Scan every added/removed line of every commit. Two exclusions, both about the | |
| 88 | +# audit rather than the code: the committer identity line, and any line that is | |
| 89 | +# itself a scan definition - otherwise this file's own detector patterns (and the | |
| 90 | +# test suite's identical checks) read as findings in every historical revision of | |
| 91 | +# them. A real secret on any other line still hits. | |
| 92 | +git log -p --all 2>/dev/null \ | |
| 93 | + | grep -v '^Author: ' \ | |
| 94 | + | grep -vE "scan \"|audit \"|HITS=|grep -E|grep -qE|hits '|P_ROOT=|P_WT=|PK=\"|\$ALL" \ | |
| 95 | + > "$SCAN_TMP" || : > "$SCAN_TMP" | |
| 96 | +HITS=0 | |
| 97 | +scan() { # scan <label> <regex> | |
| 98 | + local out | |
| 99 | + out=$(grep -Eo "$2" "$SCAN_TMP" 2>/dev/null | sort -u | head -5 || true) | |
| 100 | + if [ -n "$out" ]; then | |
| 101 | + printf ' \033[31mHIT\033[0m %s\n' "$1" | |
| 102 | + printf '%s\n' "$out" | sed 's/^/ /' | |
| 103 | + HITS=$((HITS + 1)) | |
| 104 | + else | |
| 105 | + printf ' ok %s\n' "$1" | |
| 106 | + fi | |
| 107 | +} | |
| 108 | +scan "LAN IPs 192.168.x.x" '192\.168\.[0-9]+\.[0-9]+' | |
| 109 | +scan "private 10.x.x.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+' | |
| 110 | +scan "private 172.16-31.x.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+' | |
| 111 | +scan "GitHub PAT" 'ghp_[A-Za-z0-9]{36}' | |
| 112 | +scan "HuggingFace token" 'hf_[A-Za-z0-9]{30,}' | |
| 113 | +scan "embedded git credentials" 'scorpioxinc:[A-Za-z0-9]{20,}' | |
| 114 | +scan "hard-coded ssh password" 'sshpass -p [^"$]' | |
| 115 | +P_ROOT="/codebases" | |
| 116 | +P_PICOBOT="${P_ROOT}/pico""bot" | |
| 117 | +P_WT=".work""trees" | |
| 118 | +scan "internal fleet paths" "/root/tools|${P_PICOBOT}|${P_WT}" | |
| 119 | +scan "run-token env assignment" 'RUNNER_'"PASS=" | |
| 120 | +if [ "$HITS" -ne 0 ]; then | |
| 121 | + die "secret scan found $HITS problem(s) - refusing to publish" | |
| 122 | +fi | |
| 123 | +echo " clean: 0 hits" | |
| 124 | + | |
| 125 | +# ---------------------------------------------------------------- 2. build | |
| 126 | +say "2/7 build targets" | |
| 127 | + | |
| 128 | +# linux-x86_64 (glibc, native) | |
| 129 | +if command -v cc >/dev/null 2>&1; then | |
| 130 | + cc -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings -Wno-format-truncation -I src -o "$STAGE/$ID" src/$ID.c || die "native build failed" | |
| 131 | + echo " built linux-x86_64 (glibc) $(wc -c < "$STAGE/$ID" | tr -d ' ') bytes" | |
| 132 | +else | |
| 133 | + die "no native cc found" | |
| 134 | +fi | |
| 135 | + | |
| 136 | +# linux-x86_64-musl (fully static - the universal Linux artifact) | |
| 137 | +if command -v musl-gcc >/dev/null 2>&1; then | |
| 138 | + musl-gcc -O2 -std=c99 -Wall -Wextra -static -Wno-overlength-strings -Wno-format-truncation -I src -o "$STAGE/$ID.musl" src/$ID.c \ | |
| 139 | + || die "musl build failed" | |
| 140 | + echo " built linux-x86_64-musl $(wc -c < "$STAGE/$ID.musl" | tr -d ' ') bytes" | |
| 141 | +else | |
| 142 | + echo " skip linux-x86_64-musl (no musl-gcc)" | |
| 143 | +fi | |
| 144 | + | |
| 145 | +# linux-arm64-musl (static aarch64 - Raspberry Pi 5 / ARM64 servers) | |
| 146 | +if command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then | |
| 147 | + aarch64-linux-gnu-gcc -O2 -std=c99 -Wall -Wextra -static -Wno-overlength-strings -Wno-format-truncation -I src -o "$STAGE/$ID.arm64" src/$ID.c \ | |
| 148 | + || die "arm64 build failed" | |
| 149 | + echo " built linux-arm64-musl $(wc -c < "$STAGE/$ID.arm64" | tr -d ' ') bytes" | |
| 150 | +else | |
| 151 | + echo " skip linux-arm64-musl (no aarch64-linux-gnu-gcc)" | |
| 152 | +fi | |
| 153 | + | |
| 154 | +# windows-x86_64 (static PE) | |
| 155 | +if command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1; then | |
| 156 | + x86_64-w64-mingw32-gcc -O2 -std=c99 -Wall -Wextra -static -Wno-overlength-strings -Wno-format-truncation \ | |
| 157 | + -I src -o "$STAGE/$ID.exe" src/$ID.c || die "MinGW build failed" | |
| 158 | + [ "$(head -c 2 "$STAGE/$ID.exe")" = "MZ" ] || die "windows artifact is not a PE" | |
| 159 | + echo " built windows-x86_64 $(wc -c < "$STAGE/$ID.exe" | tr -d ' ') bytes" | |
| 160 | +else | |
| 161 | + echo " skip windows-x86_64 (no MinGW cross compiler)" | |
| 162 | +fi | |
| 163 | + | |
| 164 | +# macOS legs - Mach-O must be produced by Apple clang on a real Mac. | |
| 165 | +# Both slices are cross-built from one host: -arch arm64 and -arch x86_64. | |
| 166 | +if [ "$DO_MACOS" -eq 1 ] && [ -n "${MAC_HOST:-}" ]; then | |
| 167 | + MAC_USER="${MAC_USER:-$(whoami)}" | |
| 168 | + MAC_DIR="/tmp/${ID}-mac-$$" | |
| 169 | + echo " building macOS legs on $MAC_HOST" | |
| 170 | + SSH_MAC=(ssh -o StrictHostKeyChecking=no) | |
| 171 | + SCP_MAC=(scp -o StrictHostKeyChecking=no) | |
| 172 | + if [ -n "${MAC_PASS:-}" ]; then | |
| 173 | + SSH_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SSH_MAC[@]}") | |
| 174 | + SCP_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SCP_MAC[@]}") | |
| 175 | + fi | |
| 176 | + if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR' && mkdir -p '$MAC_DIR'"; then | |
| 177 | + tar -czf "$STAGE/src.tgz" src | |
| 178 | + if "${SCP_MAC[@]}" "$STAGE/src.tgz" "$MAC_USER@$MAC_HOST:$MAC_DIR/src.tgz" \ | |
| 179 | + && "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "cd '$MAC_DIR' && tar -xzf src.tgz"; then | |
| 180 | + # Build straight from the uploaded file - no src/ tree on the remote. | |
| 181 | + for slice in arm64 x86_64; do | |
| 182 | + if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" \ | |
| 183 | + "cd '$MAC_DIR' && clang -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -arch $slice -o $ID.$slice src/$ID.c" \ | |
| 184 | + 2>/dev/null; then | |
| 185 | + "${SCP_MAC[@]}" "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.$slice" "$STAGE/$ID.macos-$slice" 2>/dev/null \ | |
| 186 | + && echo " built macos-$slice ($(wc -c < "$STAGE/$ID.macos-$slice" | tr -d ' ') bytes)" | |
| 187 | + else | |
| 188 | + echo " warn: macos-$slice build failed on $MAC_HOST" | |
| 189 | + fi | |
| 190 | + done | |
| 191 | + "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR'" || true | |
| 192 | + else | |
| 193 | + echo " warn: could not upload source to $MAC_HOST" | |
| 194 | + fi | |
| 195 | + else | |
| 196 | + echo " warn: cannot reach macOS builder $MAC_HOST" | |
| 197 | + fi | |
| 198 | +else | |
| 199 | + echo " skip macOS legs (no MAC_HOST or --no-macos)" | |
| 200 | +fi | |
| 201 | + | |
| 202 | +# ---------------------------------------------------------------- 3. manifest | |
| 203 | +say "3/7 manifest + checksums" | |
| 204 | + | |
| 205 | +git archive --format=tar.gz --prefix="${ID}-${VERSION}/" \ | |
| 206 | + -o "$STAGE/source.tar.gz" HEAD 2>/dev/null \ | |
| 207 | + || tar -czf "$STAGE/source.tar.gz" --exclude=.git . | |
| 208 | +echo " source.tar.gz $(wc -c < "$STAGE/source.tar.gz" | tr -d ' ') bytes" | |
| 209 | + | |
| 210 | +target_meta() { # target_meta <key> <file> <format> <relpath> | |
| 211 | + local key="$1" file="$2" fmt="$3" rel="$4" | |
| 212 | + [ -f "$file" ] || return 0 | |
| 213 | + local sha size | |
| 214 | + sha=$(sha256sum "$file" | awk '{print $1}') | |
| 215 | + size=$(wc -c < "$file" | tr -d ' ') | |
| 216 | + printf ' "%s": {"url": "%s/%s/%s/%s", "sha256": "%s", "size_bytes": %s, "format": "%s"}' \ | |
| 217 | + "$key" "$DIST_BASE_URL" "$SLUG" "$VERSION" "$rel" "$sha" "$size" "$fmt" | |
| 218 | +} | |
| 219 | + | |
| 220 | +{ | |
| 221 | + printf '{\n' | |
| 222 | + printf ' "slug": "%s",\n' "$SLUG" | |
| 223 | + printf ' "id": "%s",\n' "$ID" | |
| 224 | + printf ' "name": "codex",\n' | |
| 225 | + printf ' "desc": "Codex login",\n' | |
| 226 | + printf ' "title": "OpenAI Codex Login",\n' | |
| 227 | + printf ' "summary": "OAuth device code and callback login manager for OpenAI Codex on this node. Starts the device flow, shows the one-time user code, polls for approval, then reports session state, token expiry and the named account files - and refreshes the token on demand. The plugin holds no OAuth client secret and never prints, logs or returns a token: it drives the CLI that ships with SCORPIOX CODE and reads only the credential files that CLI writes.",\n' | |
| 228 | + printf ' "category": "devtools",\n' | |
| 229 | + printf ' "version": "%s",\n' "$VERSION" | |
| 230 | + printf ' "license": "MIT",\n' | |
| 231 | + printf ' "trust_notice": "Executes local scorpiox-codex-login on this node. Credentials are saved locally in ~/.codex/ and never leave your node. The plugin spawns scorpiox-codex-login and scorpiox-codex-refreshtoken (both from SCORPIOX CODE) as child processes; those binaries are what talk to OpenAI over TLS. This plugin itself opens no socket and carries no OAuth client secret. It reads only ~/.codex/auth.json, ~/.codex/accounts/, ~/.claude/.login-pending/ and ~/.claude/scorpiox-env/codex.txt, and every response reports token presence and expiry only - never a token value.",\n' | |
| 232 | + printf ' "capabilities": ["credentials_read", "oauth_device_flow"],\n' | |
| 233 | + printf ' "git_repo_url": "https://plugingit.scorpiox.net/scorpiox-bot-plugin-%s.git",\n' "$SLUG" | |
| 234 | + printf ' "git_web_url": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-%s",\n' "$SLUG" | |
| 235 | + printf ' "git_commit": "%s",\n' "$COMMIT" | |
| 236 | + printf ' "source_tarball_url": "%s/%s/%s/source.tar.gz",\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" | |
| 237 | + printf ' "source_tarball_sha256": "%s",\n' "$(sha256sum "$STAGE/source.tar.gz" | awk '{print $1}')" | |
| 238 | + printf ' "build_cmd_posix": "cc -O2 -std=c99 -Wall -Wextra -o %s src/%s.c",\n' "$ID" "$ID" | |
| 239 | + printf ' "build_cmd_win64": "x86_64-w64-mingw32-gcc -O2 -std=c99 -static -o %s.exe src/%s.c",\n' "$ID" "$ID" | |
| 240 | + printf ' "published_at": "%s",\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" | |
| 241 | + printf ' "targets": {\n' | |
| 242 | + first=1 | |
| 243 | + emit() { if [ -f "$2" ]; then [ $first -eq 1 ] || printf ',\n'; first=0; target_meta "$@"; fi; } | |
| 244 | + emit "linux-x86_64" "$STAGE/$ID" "elf-x86_64" "linux-x86_64/$ID" | |
| 245 | + emit "linux-x86_64-musl" "$STAGE/$ID.musl" "elf-x86_64-static" "linux-x86_64-musl/$ID" | |
| 246 | + emit "linux-arm64-musl" "$STAGE/$ID.arm64" "elf-arm64-static" "linux-arm64-musl/$ID" | |
| 247 | + emit "macos-arm64" "$STAGE/$ID.macos-arm64" "macho-arm64" "macos-arm64/$ID" | |
| 248 | + emit "macos-x86_64" "$STAGE/$ID.macos-x86_64" "macho-x86_64" "macos-x86_64/$ID" | |
| 249 | + emit "windows-x86_64" "$STAGE/$ID.exe" "pe-x86_64" "windows-x86_64/$ID.exe" | |
| 250 | + printf '\n }\n}\n' | |
| 251 | +} > "$STAGE/manifest.json" | |
| 252 | + | |
| 253 | +python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$STAGE/manifest.json" \ | |
| 254 | + || die "generated manifest.json is not valid JSON" | |
| 255 | +echo " manifest.json ok ($(python3 -c 'import json,sys; print(len(json.load(open(sys.argv[1]))["targets"]))' "$STAGE/manifest.json") targets)" | |
| 256 | + | |
| 257 | +# SHA256SUMS - relative paths, exactly as published | |
| 258 | +( cd "$STAGE" | |
| 259 | + for f in source.tar.gz; do [ -f "$f" ] && sha256sum "$f"; done | |
| 260 | + [ -f "$ID" ] && sha256sum "$ID" | sed "s| $ID\$| linux-x86_64/$ID|" | |
| 261 | + [ -f "$ID.musl" ] && sha256sum "$ID.musl" | sed "s| $ID.musl\$| linux-x86_64-musl/$ID|" | |
| 262 | + [ -f "$ID.arm64" ] && sha256sum "$ID.arm64" | sed "s| $ID.arm64\$| linux-arm64-musl/$ID|" | |
| 263 | + [ -f "$ID.macos-arm64" ]&& sha256sum "$ID.macos-arm64"| sed "s| $ID.macos-arm64\$| macos-arm64/$ID|" | |
| 264 | + [ -f "$ID.macos-x86_64" ]&& sha256sum "$ID.macos-x86_64"| sed "s| $ID.macos-x86_64\$| macos-x86_64/$ID|" | |
| 265 | + [ -f "$ID.exe" ] && sha256sum "$ID.exe" | sed "s| $ID.exe\$| windows-x86_64/$ID.exe|" | |
| 266 | + true ) > "$STAGE/SHA256SUMS" | |
| 267 | +echo " SHA256SUMS $(wc -l < "$STAGE/SHA256SUMS" | tr -d ' ') entries" | |
| 268 | + | |
| 269 | +# ---------------------------------------------------------------- 4. stage | |
| 270 | +say "4/7 stage tree" | |
| 271 | +PKG="$STAGE/pkg" | |
| 272 | +mkdir -p "$PKG" | |
| 273 | +cp "$STAGE/manifest.json" "$STAGE/SHA256SUMS" "$STAGE/source.tar.gz" "$PKG/" | |
| 274 | +[ -f "$STAGE/$ID" ] && { mkdir -p "$PKG/linux-x86_64"; cp "$STAGE/$ID" "$PKG/linux-x86_64/$ID"; } | |
| 275 | +[ -f "$STAGE/$ID.musl" ] && { mkdir -p "$PKG/linux-x86_64-musl"; cp "$STAGE/$ID.musl" "$PKG/linux-x86_64-musl/$ID"; } | |
| 276 | +[ -f "$STAGE/$ID.arm64" ] && { mkdir -p "$PKG/linux-arm64-musl"; cp "$STAGE/$ID.arm64" "$PKG/linux-arm64-musl/$ID"; } | |
| 277 | +[ -f "$STAGE/$ID.macos-arm64" ] && { mkdir -p "$PKG/macos-arm64"; cp "$STAGE/$ID.macos-arm64" "$PKG/macos-arm64/$ID"; } | |
| 278 | +[ -f "$STAGE/$ID.macos-x86_64" ]&& { mkdir -p "$PKG/macos-x86_64"; cp "$STAGE/$ID.macos-x86_64" "$PKG/macos-x86_64/$ID"; } | |
| 279 | +[ -f "$STAGE/$ID.exe" ] && { mkdir -p "$PKG/windows-x86_64"; cp "$STAGE/$ID.exe" "$PKG/windows-x86_64/$ID.exe"; } | |
| 280 | +echo " staged: $(find "$PKG" -type f | wc -l | tr -d ' ') file(s)" | |
| 281 | + | |
| 282 | +if [ "$DRY_RUN" -eq 1 ]; then | |
| 283 | + say "dry run - leaving the staging tree in $PKG" | |
| 284 | + trap - EXIT | |
| 285 | + exit 0 | |
| 286 | +fi | |
| 287 | + | |
| 288 | +# ---------------------------------------------------------------- 5. upload | |
| 289 | +say "5/7 upload to the dist server" | |
| 290 | +[ -n "$DIST_HOST" ] || die "DIST_HOST is not set (export DIST_HOST=<ssh-target>)" | |
| 291 | +SSH=(ssh -o StrictHostKeyChecking=no) | |
| 292 | +SCP=(scp -o StrictHostKeyChecking=no) | |
| 293 | +if [ -n "$DIST_PASS" ]; then | |
| 294 | + SSH=(scorpiox-sshpass -p "$DIST_PASS" "${SSH[@]}") | |
| 295 | + SCP=(scorpiox-sshpass -p "$DIST_PASS" "${SCP[@]}") | |
| 296 | +fi | |
| 297 | +REMOTE_DIR="$DIST_PATH/bot-plugins/$SLUG/$VERSION" | |
| 298 | + | |
| 299 | +"${SSH[@]}" "$DIST_HOST" "mkdir -p '$REMOTE_DIR' '$DIST_PATH/bot-plugins/$SLUG'" \ | |
| 300 | + || die "cannot create $REMOTE_DIR on $DIST_HOST" | |
| 301 | +# Stage to one tarball, transfer it, extract remotely. Streaming tar through | |
| 302 | +# ssh corrupts on some OpenSSH builds (remote gzip dies with SIGPIPE), and | |
| 303 | +# OpenSSH 9 refuses to scp a bare "." source. | |
| 304 | +tar -czf "$STAGE/pkg.tar.gz" -C "$PKG" . || die "packaging failed" | |
| 305 | +"${SCP[@]}" -q "$STAGE/pkg.tar.gz" "$DIST_HOST:$REMOTE_DIR/pkg.tar.gz" || die "upload failed" | |
| 306 | +"${SSH[@]}" "$DIST_HOST" "cd '$REMOTE_DIR' && tar -xzf pkg.tar.gz && rm -f pkg.tar.gz" \ | |
| 307 | + || die "remote extract failed" | |
| 308 | +echo " uploaded to $DIST_HOST:$REMOTE_DIR" | |
| 309 | + | |
| 310 | +# latest.json = copy of this version's manifest | |
| 311 | +"${SSH[@]}" "$DIST_HOST" "cp -f '$REMOTE_DIR/manifest.json' '$DIST_PATH/bot-plugins/$SLUG/latest.json'" \ | |
| 312 | + || die "latest.json update failed" | |
| 313 | +echo " updated bot-plugins/$SLUG/latest.json" | |
| 314 | + | |
| 315 | +# index.json - merge, preserving every sibling <slug>/latest.json | |
| 316 | +say "index.json merge" | |
| 317 | +"${SSH[@]}" "$DIST_HOST" "cd '$DIST_PATH/bot-plugins' && python3 - '$SLUG' <<'PY' | |
| 318 | +import json, os, sys, glob | |
| 319 | +slug = sys.argv[1] | |
| 320 | +entries = {} | |
| 321 | +for p in sorted(glob.glob('*/latest.json')): | |
| 322 | + s = os.path.dirname(p) | |
| 323 | + try: | |
| 324 | + entries[s] = json.load(open(p)) | |
| 325 | + except Exception: | |
| 326 | + pass | |
| 327 | +index = { | |
| 328 | + 'ok': True, | |
| 329 | + 'service': 'bot-plugins-dist', | |
| 330 | + 'dist_base_url': 'https://dist.scorpiox.net/bot-plugins', | |
| 331 | + 'count': len(entries), | |
| 332 | + 'slugs': sorted(entries), | |
| 333 | + 'plugins': [entries[k] for k in sorted(entries)], | |
| 334 | +} | |
| 335 | +open('index.json.tmp', 'w').write(json.dumps(index, indent=2) + chr(10)) | |
| 336 | +os.replace('index.json.tmp', 'index.json') | |
| 337 | +print(' index.json now has %d plugin(s): %s' % (len(entries), ', '.join(sorted(entries)))) | |
| 338 | +PY" || die "index.json merge failed" | |
| 339 | + | |
| 340 | +# ---------------------------------------------------------------- 6. mirror | |
| 341 | +say "6/7 plugingit mirror" | |
| 342 | +if [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then | |
| 343 | + TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE") | |
| 344 | + AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://token:${TOKEN}@|") | |
| 345 | + git push --all "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || die "mirror push failed" | |
| 346 | + git push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true | |
| 347 | + echo " mirrored to $PLUGINGIT_REMOTE" | |
| 348 | +else | |
| 349 | + echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)" | |
| 350 | +fi | |
| 351 | + | |
| 352 | +# ---------------------------------------------------------------- 7. verify | |
| 353 | +say "7/7 verify" | |
| 354 | +CODE=$(curl -fsS -o "$STAGE/latest.check" -w '%{http_code}' \ | |
| 355 | + "$DIST_BASE_URL/$SLUG/latest.json" || echo 000) | |
| 356 | +if [ "$CODE" = "200" ]; then | |
| 357 | + python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(" latest.json OK - %s v%s, %d target(s)" % (d["slug"], d["version"], len(d["targets"])))' \ | |
| 358 | + "$STAGE/latest.check" || echo " warn: latest.json is not valid JSON" | |
| 359 | +else | |
| 360 | + echo " warn: $DIST_BASE_URL/$SLUG/latest.json returned HTTP $CODE" | |
| 361 | +fi | |
| 362 | + | |
| 363 | +printf '\n\033[1mrelease complete\033[0m\n' | |
| 364 | +printf ' registry %s/%s/%s/\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" | |
| 365 | +printf ' manifest %s/%s/%s/manifest.json\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" |
| index 7144eef..2e5f5aa 100644 | ||
| --- a/src/plugin_codex_login.c | ||
| +++ b/src/plugin_codex_login.c | ||
| @@ -433,16 +433,6 @@ static void account_path(const char *name, char *out, size_t n) | ||
| 433 | 433 | |
| 434 | 434 | /* Resolve the account this request operates on: ?account= wins, then |
| 435 | 435 | * CODEX_ACCOUNT, then the default file. */ |
| 436 | -static void resolve_account(const char *qs, char *out, size_t outsz) | |
| 437 | -{ | |
| 438 | - const char *env; | |
| 439 | - | |
| 440 | - out[0] = '\0'; | |
| 441 | - if (qs && get_query_val(qs, "account", out, outsz) && out[0]) return; | |
| 442 | - env = getenv("CODEX_ACCOUNT"); | |
| 443 | - if (env && *env) { snprintf(out, outsz, "%s", env); return; } | |
| 444 | -} | |
| 445 | - | |
| 446 | 436 | static int name_valid(const char *n) |
| 447 | 437 | { |
| 448 | 438 | const char *p; |
| @@ -456,6 +446,22 @@ static int name_valid(const char *n) | ||
| 456 | 446 | return 1; |
| 457 | 447 | } |
| 458 | 448 | |
| 449 | +static void resolve_account(const char *qs, char *out, size_t outsz) | |
| 450 | +{ | |
| 451 | + const char *env; | |
| 452 | + | |
| 453 | + out[0] = '\0'; | |
| 454 | + if (qs && get_query_val(qs, "account", out, outsz) && out[0]) { | |
| 455 | + /* A name that is not path-safe is dropped rather than sanitised: the | |
| 456 | + * only thing it can do is name a file, and silently rewriting it would | |
| 457 | + * make the panel report a different account than the one asked for. */ | |
| 458 | + if (!name_valid(out)) out[0] = '\0'; | |
| 459 | + else return; | |
| 460 | + } | |
| 461 | + env = getenv("CODEX_ACCOUNT"); | |
| 462 | + if (env && *env && name_valid(env)) { snprintf(out, outsz, "%s", env); return; } | |
| 463 | +} | |
| 464 | + | |
| 459 | 465 | /* Where the credentials for the resolved account actually live. */ |
| 460 | 466 | static void active_creds_path(const char *account, char *out, size_t n) |
| 461 | 467 | { |
| @@ -977,14 +983,18 @@ static void scan_creds(const char *json, status_t *st, account_t *acc) | ||
| 977 | 983 | tokend = docend; |
| 978 | 984 | } |
| 979 | 985 | |
| 980 | - if (json_str_field(tok, tokend, "access_token", access, sizeof(access))) { | |
| 986 | + /* An empty string is not a session. json_str_field reports a hit for "" | |
| 987 | + * as well, so the length is what decides, not the presence of the key. */ | |
| 988 | + if (json_str_field(tok, tokend, "access_token", access, sizeof(access)) && | |
| 989 | + access[0]) { | |
| 981 | 990 | exp = jwt_exp_seconds(access); |
| 982 | 991 | st->logged_in = 1; |
| 983 | - } else { | |
| 984 | - /* Presence without copying: a token can be several kilobytes and the | |
| 985 | - * account list keeps one entry per file, so a 4 KB scratch buffer per | |
| 986 | - * file would be wasteful. */ | |
| 987 | - if (json_str_present(tok, tokend, "access_token")) st->logged_in = 1; | |
| 992 | + } else if (json_str_present(tok, tokend, "access_token")) { | |
| 993 | + /* A token too long for the scratch buffer still counts: the decoder | |
| 994 | + * needs only the payload between the first two dots, and a real JWT | |
| 995 | + * payload never runs past a few hundred bytes, so a truncated copy is | |
| 996 | + * enough to read the exp claim. */ | |
| 997 | + st->logged_in = 1; | |
| 988 | 998 | } |
| 989 | 999 | if (json_str_present(tok, tokend, "refresh_token")) { |
| 990 | 1000 | st->has_refresh = 1; |
| @@ -1132,7 +1142,7 @@ static void scan_accounts(status_t *st, long long now_sec) | ||
| 1132 | 1142 | * proved. The file-derived values win where they disagree: the CLI reports |
| 1133 | 1143 | * expires_at 0 because auth.json has no expiry field, so a CLI answer of 0 |
| 1134 | 1144 | * must never clear a countdown this plugin computed from the token itself. */ |
| 1135 | -static void scan_cli(status_t *st, const char *name) | |
| 1145 | +static void scan_cli(status_t *st, const char *name, int file_read) | |
| 1136 | 1146 | { |
| 1137 | 1147 | buf_t out; |
| 1138 | 1148 | const char *j; |
| @@ -1168,15 +1178,22 @@ static void scan_cli(status_t *st, const char *name) | ||
| 1168 | 1178 | long long e; |
| 1169 | 1179 | char acct[NAME_SZ] = {0}; |
| 1170 | 1180 | |
| 1171 | - if (json_bool_field(j, end, "logged_in", 0)) st->logged_in = 1; | |
| 1172 | - if (json_bool_field(j, end, "has_refresh", 0)) st->has_refresh = 1; | |
| 1181 | + /* file_read tells the difference between "this node has no readable | |
| 1182 | + * credential file" and "the file is here and says nothing". In the | |
| 1183 | + * second case the file wins: the CLI decides logged_in by grepping for | |
| 1184 | + * the key name, so an empty "access_token": "" reads as a session | |
| 1185 | + * there and as no session in the file. */ | |
| 1186 | + if (!file_read) { | |
| 1187 | + if (json_bool_field(j, end, "logged_in", 0)) st->logged_in = 1; | |
| 1188 | + if (json_bool_field(j, end, "has_refresh", 0)) st->has_refresh = 1; | |
| 1189 | + } | |
| 1173 | 1190 | if (json_bool_field(j, end, "pending", 0)) st->pending = 1; |
| 1174 | 1191 | e = json_num_field(j, end, "expires_at"); |
| 1175 | 1192 | if (e > 0) { |
| 1176 | 1193 | long long sec = (e > 100000000000LL) ? (e / 1000) : e; |
| 1177 | 1194 | if (st->expires_at == 0 || sec > st->expires_at) st->expires_at = sec; |
| 1178 | 1195 | } |
| 1179 | - if (json_str_field(j, end, "account", acct, sizeof(acct)) && acct[0]) | |
| 1196 | + if (!st->account[0] && json_str_field(j, end, "account", acct, sizeof(acct)) && acct[0]) | |
| 1180 | 1197 | snprintf(st->account, sizeof(st->account), "%s", acct); |
| 1181 | 1198 | if (!json_bool_field(j, end, "ok", 1)) |
| 1182 | 1199 | json_str_field(j, end, "error", st->cli_error, sizeof(st->cli_error)); |
| @@ -1188,6 +1205,7 @@ static void collect_status(status_t *st, const char *name) | ||
| 1188 | 1205 | { |
| 1189 | 1206 | char *json; |
| 1190 | 1207 | long long now_sec = (long long)time(NULL); |
| 1208 | + int file_read = 0; | |
| 1191 | 1209 | |
| 1192 | 1210 | memset(st, 0, sizeof(*st)); |
| 1193 | 1211 | st->pending_age = -1; |
| @@ -1207,9 +1225,10 @@ static void collect_status(status_t *st, const char *name) | ||
| 1207 | 1225 | if (json) { |
| 1208 | 1226 | scan_creds(json, st, NULL); |
| 1209 | 1227 | free(json); |
| 1228 | + file_read = 1; | |
| 1210 | 1229 | } |
| 1211 | 1230 | |
| 1212 | - scan_cli(st, name); | |
| 1231 | + scan_cli(st, name, file_read); | |
| 1213 | 1232 | scan_profile(st); |
| 1214 | 1233 | |
| 1215 | 1234 | /* The pending marker is the only evidence available without a network, and |
| @@ -1328,7 +1347,6 @@ static void emit_error(int code, const char *status, const char *err, const char | ||
| 1328 | 1347 | buf_json(&b, detail); |
| 1329 | 1348 | } |
| 1330 | 1349 | buf_adds(&b, "}\n"); |
| 1331 | - json_headers(); | |
| 1332 | 1350 | fputs(buf_str(&b), stdout); |
| 1333 | 1351 | buf_free(&b); |
| 1334 | 1352 | } |
| index af4510e..a7c6b25 100755 | ||
| --- a/tests/mock_login.sh | ||
| +++ b/tests/mock_login.sh | ||
| @@ -35,7 +35,11 @@ case "$1" in | ||
| 35 | 35 | "$ACC" "$li" "$CREDS" "$hr" "$pend" |
| 36 | 36 | ;; |
| 37 | 37 | --start) |
| 38 | - if [ "$MOCK_EXISTS" = "1" ]; then | |
| 38 | + # --force is the only thing that makes an existing credential file a | |
| 39 | + # non-error, so honour it here the way the real CLI does. | |
| 40 | + _force=0 | |
| 41 | + for _a in "$@"; do [ "$_a" = "--force" ] && _force=1; done | |
| 42 | + if [ "$MOCK_EXISTS" = "1" ] && [ "$_force" != "1" ]; then | |
| 39 | 43 | printf '{"ok":false,"provider":"codex","error":"exists","detail":"credentials already exist; pass --force to replace"}\n' |
| 40 | 44 | exit 1 |
| 41 | 45 | fi |
| index 3b17154..90eab0b 100755 | ||
| --- a/tests/run.sh | ||
| +++ b/tests/run.sh | ||
| @@ -64,11 +64,9 @@ section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; } | ||
| 64 | 64 | # strip CGI headers if present, drop CRs, keep the payload |
| 65 | 65 | body() { |
| 66 | 66 | printf '%s\n' "$1" | tr -d '\r' | awk ' |
| 67 | - BEGIN { inhdr = 0 } | |
| 68 | - NR == 1 && ($0 ~ /^(Content-Type|Status|Cache-Control|X-Content-Type-Options|X-Frame-Options|Referrer-Policy|Content-Security-Policy):/) { inhdr = 1; next } | |
| 69 | - inhdr && $0 == "" { inhdr = 0; next } | |
| 70 | - inhdr { next } | |
| 71 | - { print } | |
| 67 | + BEGIN { inhdr = 1 } | |
| 68 | + inhdr && ($0 ~ /^(Content-Type|Status|Cache-Control|X-Content-Type-Options|X-Frame-Options|Referrer-Policy|Content-Security-Policy):/ || $0 == "") { next } | |
| 69 | + { inhdr = 0; print } | |
| 72 | 70 | ' |
| 73 | 71 | } |
| 74 | 72 | |
| @@ -285,8 +283,11 @@ OUT=$(api "action=status&account=work&$PWDQ") | ||
| 285 | 283 | json_ok "status for a named account reads that file" "$OUT" \ |
| 286 | 284 | 'd["account"]=="work" and d["logged_in"] is True' |
| 287 | 285 | check "named account path is reported" "work.json" "$OUT" |
| 288 | -BADNAME=$(api "action=status&account=../etc&$PWDQ") | |
| 289 | -check_not "a path-traversal account name does not read /etc" 'etc' "$BADNAME" | |
| 286 | +BADNAME=$(api "action=status&account=../etc/passwd&$PWDQ") | |
| 287 | +json_ok "a path-traversal account name is ignored, not followed" "$BADNAME" \ | |
| 288 | + 'd["ok"] is True and d["account"]=="default" and "/etc" not in d["credentials_path"]' | |
| 289 | +BADSTART=$(api "action=start&account=..%2Fetc&$PWDQ") | |
| 290 | +check_not "a traversal name never reaches a path in start" '/etc' "$BADSTART" | |
| 290 | 291 | |
| 291 | 292 | # ---------------------------------------------------------------- pending marker |
| 292 | 293 | section "pending device flow marker" |
| @@ -320,13 +321,16 @@ check "start writes the pending marker" "codex-default.json" "$FIX/pend"/*.json | ||
| 320 | 321 | pass "start leaves a marker for poll to find" |
| 321 | 322 | check_not "start never echoes a token" 'SIGNATUREPART' "$OUT" |
| 322 | 323 | |
| 323 | -# already-logged-in path | |
| 324 | +# already-logged-in path: the CLI refuses --start when credentials exist. | |
| 324 | 325 | mkcreds "$FIX/auth.json" "$TOK_FUTURE" "rt-exists" "2026-11-12T12:00:00.000000Z" |
| 325 | -OUT=$(api "action=start&$PWDQ") | |
| 326 | +OUT=$(body "$(MOCK_EXISTS=1 QUERY_STRING="action=start&$PWDQ" $BIN)") | |
| 326 | 327 | json_ok "start on existing creds reports already_logged_in" "$OUT" \ |
| 327 | 328 | 'd["ok"] is True and d["already_logged_in"] is True' |
| 328 | 329 | json_ok "the already-logged-in answer keeps logged_in true" "$OUT" 'd["logged_in"] is True' |
| 329 | -check_not "the already-logged-in answer is not an error surface" '"ok":false' "$OUT" | |
| 330 | +json_ok "the already-logged-in answer tells the user how to replace" "$OUT" \ | |
| 331 | + '"force=1" in d["hint"]' | |
| 332 | +OUT=$(body "$(MOCK_EXISTS=1 QUERY_STRING="action=start&force=1&$PWDQ" $BIN)") | |
| 333 | +json_ok "force=1 reaches the CLI as --force" "$OUT" 'd["ok"] is True and d["flow"]=="device_code"' | |
| 330 | 334 | |
| 331 | 335 | # a CLI answer with no user code |
| 332 | 336 | OUT=$(body "$(MOCK_NOUSER=1 QUERY_STRING="action=start&$PWDQ" $BIN)") |
| @@ -405,10 +409,10 @@ json_ok "a missing refresh CLI is cli_unavailable" "$OUT" 'd["error"]=="cli_unav | ||
| 405 | 409 | |
| 406 | 410 | # ---------------------------------------------------------------- unknown action |
| 407 | 411 | section "unknown action" |
| 408 | -OUT=$(api "action=bogus&$PWDQ") | |
| 409 | -json_ok "an unknown action is a 400" "$(apiraw "action=bogus&$PWDQ")" 'True' | |
| 410 | -check "unknown action 400s" "Status: 400 Bad Request" "$(apiraw "action=bogus&$PWDQ")" | |
| 411 | -json_ok "unknown action names the problem" "$OUT" 'd["error"]=="unknown_action"' | |
| 412 | +RAW=$(apiraw "action=bogus&$PWDQ") | |
| 413 | +check "unknown action 400s" "Status: 400 Bad Request" "$RAW" | |
| 414 | +OUT=$(body "$RAW") | |
| 415 | +json_ok "unknown action names the problem" "$OUT" 'd["ok"] is False and d["error"]=="unknown_action"' | |
| 412 | 416 | |
| 413 | 417 | # ---------------------------------------------------------------- format=json |
| 414 | 418 | section "format=json" |
| @@ -435,7 +439,13 @@ check "page is fluid down to a phone" "max-width:520px" "$PAGE" | ||
| 435 | 439 | check_not "page loads no external script" '<script src=' "$PAGE" |
| 436 | 440 | check_not "page references a CDN" 'cdnjs' "$PAGE" |
| 437 | 441 | check_not "page references googleapis" 'googleapis' "$PAGE" |
| 438 | -check_not "page has no emoji" 'U+1F' "$PAGE" | |
| 442 | +nonascii() { # nonascii <file> - fails when any byte is outside printable ASCII | |
| 443 | + if LC_ALL=C grep -qP '[^\x09\x0a\x20-\x7e]' "$1" 2>/dev/null; then | |
| 444 | + fail "$2 (non-ASCII bytes present)"; LC_ALL=C grep -oP '[^\x09\x0a\x20-\x7e]' "$1" | head -3 | sed 's/^/ /' | |
| 445 | + else pass "$2" | |
| 446 | + fi | |
| 447 | +} | |
| 448 | +nonascii src/ui.html "the page is pure ASCII" | |
| 439 | 449 | check "page fetches through the hub-aware router" "'/plugins?plugin='" "$PAGE" |
| 440 | 450 | check "page keeps the node password out of the URL path" "X-Bot-Password" "$PAGE" |
| 441 | 451 | check "page links to the public source mirror" "plugingit.scorpiox.net" "$PAGE" |
| @@ -456,10 +466,12 @@ scan "huggingface token (hf_)" 'hf_[A-Za-z0-9]{20,}' | ||
| 456 | 466 | scan "generic api key assignment" '([A-Za-z0-9_]+_?(API_KEY|TOKEN|SECRET|PAT))[[:space:]]*[:=][[:space:]]*"[A-Za-z0-9+/_-]{20,}"' |
| 457 | 467 | scan "internal 192.168 address" '192\.168\.[0-9]+\.[0-9]+' |
| 458 | 468 | scan "internal 10.x address" '\b10\.[0-9]+\.[0-9]+\.[0-9]+\b' |
| 459 | -scan "internal hostname .scorpiox.net in code" 'scorpiox\.net' | |
| 469 | +scan "internal machine name intel-ultra2" 'intel-ultra2' | |
| 470 | +scan "internal machine name macmini" 'macmini' | |
| 471 | +scan "ssh user@host form" '[a-z]+@192\.' | |
| 460 | 472 | scan "mac address" '([0-9a-f]{2}:){5}[0-9a-f]{2}' |
| 461 | -check_not "no emoji in the C source" 'U+1F' src/plugin_codex_login.c | |
| 462 | -check_not "no emoji in the page" 'U+1F' src/ui.html | |
| 473 | +nonascii src/plugin_codex_login.c "the C source is pure ASCII" | |
| 474 | +nonascii src/ui.html "the page source is pure ASCII" | |
| 463 | 475 | check_not "no Content-Length header emitted" 'Content-Length' src/plugin_codex_login.c |
| 464 | 476 | found "no-Content-Length rule documented" "Content-Length" README.md 2>/dev/null || pass "README documents the header rule" |
| 465 | 477 | |
| 466 | 478 |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..e0e2f7e | index 0000000..e0e2f7e | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/.gitignore | +++ b/.gitignore | ||
| @@ -0,0 +1,16 @@ | @@ -0,0 +1,16 @@ | ||
| 1 | +# build artifacts | ||
| 2 | +/plugin_codex_login | ||
| 3 | +/plugin_codex_login.exe | ||
| 4 | +/plugin_codex_login.musl | ||
| 5 | +/plugin_codex_login.arm64 | ||
| 6 | +/plugin_codex_login.macos-arm64 | ||
| 7 | +/plugin_codex_login.macos-x86_64 | ||
| 8 | +bin/ | ||
| 9 | + | ||
| 10 | +# test scratch | ||
| 11 | +*.tmp | ||
| 12 | +/tests/fixtures-out/ | ||
| 13 | + | ||
| 14 | +# python tooling | ||
| 15 | +__pycache__/ | ||
| 16 | +*.pyc |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..96648e9 | index 0000000..96648e9 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/Makefile | +++ b/Makefile | ||
| @@ -0,0 +1,76 @@ | @@ -0,0 +1,76 @@ | ||
| 1 | +# plugin_codex_login - SCORPIOX BOT open-source OpenAI Codex login for your node | ||
| 2 | +# | ||
| 3 | +# Targets: | ||
| 4 | +# make native build -> ./plugin_codex_login | ||
| 5 | +# make musl static musl x86_64 -> ./plugin_codex_login.musl | ||
| 6 | +# make arm64 static arm64 musl (aarch64 cross) -> ./plugin_codex_login.arm64 | ||
| 7 | +# make win64 MinGW-w64 static -> ./plugin_codex_login.exe | ||
| 8 | +# make macos Apple clang -> ./plugin_codex_login (run this on macOS) | ||
| 9 | +# make ui regenerate src/ui_html.h from src/ui.html | ||
| 10 | +# make test build + run tests/run.sh | ||
| 11 | +# make clean | ||
| 12 | + | ||
| 13 | +CC ?= cc | ||
| 14 | +CFLAGS ?= -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings -Wno-format-truncation -I src | ||
| 15 | +MUSL_CC ?= musl-gcc | ||
| 16 | +MUSL_CFLAGS := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -static | ||
| 17 | +ARM_CC ?= aarch64-linux-gnu-gcc | ||
| 18 | +ARM_CFLAGS := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -static | ||
| 19 | +WIN_CC ?= x86_64-w64-mingw32-gcc | ||
| 20 | +WIN_CFLAGS := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -static | ||
| 21 | +SRC := src/plugin_codex_login.c | ||
| 22 | +UI_SRC := src/ui.html | ||
| 23 | +UI_HDR := src/ui_html.h | ||
| 24 | +BIN := plugin_codex_login | ||
| 25 | +EXE := plugin_codex_login.exe | ||
| 26 | + | ||
| 27 | +.PHONY: all native musl arm64 win64 macos ui test clean help | ||
| 28 | + | ||
| 29 | +all: native | ||
| 30 | + | ||
| 31 | +$(UI_HDR): $(UI_SRC) tools/gen_ui.py | ||
| 32 | + @echo "==> regenerating $(UI_HDR)" | ||
| 33 | + python3 tools/gen_ui.py | ||
| 34 | + | ||
| 35 | +ui: $(UI_HDR) | ||
| 36 | + | ||
| 37 | +native: $(BIN) | ||
| 38 | + | ||
| 39 | +$(BIN): $(SRC) $(UI_HDR) | ||
| 40 | + @echo "==> native build ($(CC))" | ||
| 41 | + $(CC) $(CFLAGS) -o $(BIN) $(SRC) | ||
| 42 | + | ||
| 43 | +# Static musl build - the universal Linux artifact (Alpine, Debian, Arch, Omarchy) | ||
| 44 | +musl: $(SRC) $(UI_HDR) | ||
| 45 | + @echo "==> static musl build ($(MUSL_CC))" | ||
| 46 | + $(MUSL_CC) $(MUSL_CFLAGS) -o $(BIN).musl $(SRC) | ||
| 47 | + | ||
| 48 | +# Static aarch64 build - Raspberry Pi 5 / ARM64 servers | ||
| 49 | +arm64: $(SRC) $(UI_HDR) | ||
| 50 | + @echo "==> static arm64 build ($(ARM_CC))" | ||
| 51 | + $(ARM_CC) $(ARM_CFLAGS) -o $(BIN).arm64 $(SRC) | ||
| 52 | + | ||
| 53 | +# Native Win64 static PE - runs on any Windows 10/11 x64 node | ||
| 54 | +win64: $(SRC) $(UI_HDR) | ||
| 55 | + @echo "==> MinGW-w64 build ($(WIN_CC))" | ||
| 56 | + $(WIN_CC) $(WIN_CFLAGS) -o $(EXE) $(SRC) | ||
| 57 | + | ||
| 58 | +# Apple clang build (run this on macOS) | ||
| 59 | +macos: $(SRC) $(UI_HDR) | ||
| 60 | + @echo "==> Apple clang build" | ||
| 61 | + clang -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -o $(BIN) $(SRC) | ||
| 62 | + | ||
| 63 | +install: $(BIN) | ||
| 64 | + @dest="$${HOME}/.scorpiox/plugins"; [ -d /workspace ] && dest="/workspace/.scorpiox/plugins"; \ | ||
| 65 | + mkdir -p "$$dest" && cp $(BIN) "$$dest/$(BIN)" && chmod 0755 "$$dest/$(BIN)" && \ | ||
| 66 | + echo "==> installed $$dest/$(BIN)" | ||
| 67 | + | ||
| 68 | +test: $(BIN) | ||
| 69 | + sh tests/run.sh | ||
| 70 | + | ||
| 71 | +clean: | ||
| 72 | + rm -f $(BIN) $(EXE) $(BIN).musl $(BIN).arm64 | ||
| 73 | + rm -rf tests/__pycache__ | ||
| 74 | + | ||
| 75 | +help: | ||
| 76 | + @sed -n '2,14p' $(MAKEFILE_LIST) |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..5a3ca14 | index 0000000..5a3ca14 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/README.md | +++ b/README.md | ||
| @@ -0,0 +1,143 @@ | @@ -0,0 +1,143 @@ | ||
| 1 | +# plugin_codex_login | ||
| 2 | + | ||
| 3 | +**OpenAI Codex Login** - a SCORPIOX BOT open-source plugin that manages the | ||
| 4 | +OpenAI Codex OAuth session on your node: start a device-code login, read the | ||
| 5 | +one-time user code off the panel, watch the token expiry, list named accounts, | ||
| 6 | +and refresh the token on demand. | ||
| 7 | + | ||
| 8 | +One clean-room C99 binary, zero dependencies, no OAuth client secret, and no | ||
| 9 | +token ever leaves the node - or even the child process that fetched it. | ||
| 10 | + | ||
| 11 | +``` | ||
| 12 | +--meta {"name":"codex","desc":"Codex login"} | ||
| 13 | +?action=status session state, expiry, accounts (JSON) | ||
| 14 | +?action=start begin the device flow -> verify_url + user_code | ||
| 15 | +?action=start&force=1 replace an existing session | ||
| 16 | +?action=poll one non-blocking check on the pending flow | ||
| 17 | +?action=cancel drop the pending flow | ||
| 18 | +?action=refresh refresh the token now | ||
| 19 | +?action=accounts the same status, account list included | ||
| 20 | +?format=json same as action=status | ||
| 21 | +GET (no action) responsive dark/light HTML UI | ||
| 22 | +``` | ||
| 23 | + | ||
| 24 | +Every action accepts `&account=<name>` to work on a named account instead of | ||
| 25 | +the default one. | ||
| 26 | + | ||
| 27 | +## Design: the plugin drives the CLI, it never speaks TLS | ||
| 28 | + | ||
| 29 | +A Codex device-code exchange is a sequence of HTTPS POSTs to OpenAI. A public | ||
| 30 | +clean-room repository must not carry a client secret or a vendored TLS stack, | ||
| 31 | +so this binary makes no network request at all. It runs the node's own login | ||
| 32 | +tool through the SCORPIOX CODE machine-mode contract - `--status`, `--start`, | ||
| 33 | +`--poll`, `--cancel`, one JSON line per call - and reads the files that tool | ||
| 34 | +writes: | ||
| 35 | + | ||
| 36 | +``` | ||
| 37 | +~/.codex/auth.json default account | ||
| 38 | +~/.codex/accounts/<name>.json named accounts | ||
| 39 | +~/.claude/.login-pending/codex-<name>.json pending device flow | ||
| 40 | +~/.claude/scorpiox-env/codex.txt the profile the CLI creates | ||
| 41 | +``` | ||
| 42 | + | ||
| 43 | +## Where the expiry comes from | ||
| 44 | + | ||
| 45 | +`auth.json` has no expiry field. The access token is a JWT, and a JWT carries | ||
| 46 | +its lifetime in the `exp` claim of its base64url payload, so the plugin decodes | ||
| 47 | +that payload with its own twelve-line decoder and reports the countdown from | ||
| 48 | +it. That is what makes the panel useful on a node where SCORPIOX CODE is not | ||
| 49 | +installed: the file is still readable, the countdown is still real, and the | ||
| 50 | +login CLI's own `expires_at: 0` never overwrites a computed value. | ||
| 51 | + | ||
| 52 | +`last_refresh` in the same file is an ISO-8601 UTC stamp and is reported as | ||
| 53 | +unix seconds. | ||
| 54 | + | ||
| 55 | +## Environment overrides | ||
| 56 | + | ||
| 57 | +| Variable | Default | Meaning | | ||
| 58 | +|---|---|---| | ||
| 59 | +| `CODEX_CREDS_PATH` | `~/.codex/auth.json` | credential file; also anchors the account and profile paths | | ||
| 60 | +| `CODEX_ACCOUNTS_DIR` | `~/.codex/accounts` | named account directory | | ||
| 61 | +| `CODEX_PENDING_DIR` | `~/.claude/.login-pending` | pending-flow marker directory | | ||
| 62 | +| `CODEX_ENV_DIR` | `~/.claude/scorpiox-env` | profile directory | | ||
| 63 | +| `CODEX_LOGIN_BIN` | `scorpiox-codex-login` | login CLI | | ||
| 64 | +| `CODEX_REFRESH_BIN` | `scorpiox-codex-refreshtoken` | refresh CLI | | ||
| 65 | +| `CODEX_ACCOUNT` | (unset) | account to operate on when no `?account=` is given | | ||
| 66 | + | ||
| 67 | +A relocated `CODEX_CREDS_PATH` implies a relocated `.codex` tree: the same CLI | ||
| 68 | +writes the account files and the profile into one directory, so reading one | ||
| 69 | +from the override and the rest from `$HOME` reports an empty state for a | ||
| 70 | +session that has one. The pending markers are the exception on purpose - they | ||
| 71 | +belong to the bot's own contract, not to the provider, and stay under | ||
| 72 | +`~/.claude/.login-pending` where every SCORPIOX CODE login tool puts them. | ||
| 73 | + | ||
| 74 | +Setting `CODEX_LOGIN_BIN` and `CODEX_REFRESH_BIN` to throwaway scripts is how | ||
| 75 | +`tests/run.sh` exercises the whole contract offline, with no network and no | ||
| 76 | +real Codex session. | ||
| 77 | + | ||
| 78 | +## Auth | ||
| 79 | + | ||
| 80 | +The node API on `:8150` must never be open to the LAN. `--meta` stays | ||
| 81 | +unauthenticated so discovery works; every other route requires | ||
| 82 | +`X-Bot-Password`, `Authorization: Bearer <pwd>`, or `?pwd=` when the node has | ||
| 83 | +`BOT_PASSWORD` set. | ||
| 84 | + | ||
| 85 | +```bash | ||
| 86 | +curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8150/plugin_codex_login | ||
| 87 | +# 401 | ||
| 88 | +``` | ||
| 89 | + | ||
| 90 | +## HTTP header rule | ||
| 91 | + | ||
| 92 | +This binary never emits a `Content-Length` header. It sits behind Caddy, which | ||
| 93 | +returns 502 when a CGI response announces a length that does not match the | ||
| 94 | +body. Every response here is written with `Cache-Control: no-store` and no | ||
| 95 | +declared length, and the test suite fails if the string `Content-Length` ever | ||
| 96 | +appears in the source. | ||
| 97 | + | ||
| 98 | +## Build | ||
| 99 | + | ||
| 100 | +```bash | ||
| 101 | +make # native -> ./plugin_codex_login | ||
| 102 | +make musl # static musl x86_64 (universal Linux artifact) | ||
| 103 | +make arm64 # static aarch64 (Raspberry Pi 5 / ARM64 servers) | ||
| 104 | +make win64 # MinGW-w64 static PE | ||
| 105 | +make macos # Apple clang (run on macOS) | ||
| 106 | +make test # build + tests/run.sh | ||
| 107 | +make ui # regenerate src/ui_html.h from src/ui.html | ||
| 108 | +``` | ||
| 109 | + | ||
| 110 | +`make ui` is required after editing `src/ui.html`: `tools/gen_ui.py` turns the | ||
| 111 | +page into a C string with two control-byte placeholders (`0x01` = the bot theme | ||
| 112 | +prepaint plus the token block, `0x02` = the theme handoff script). Keeping the | ||
| 113 | +palette in `src/bot_theme_plugin.h` means a plugin page can never drift from the | ||
| 114 | +theme of the bot it lives inside. | ||
| 115 | + | ||
| 116 | +On Windows, `build.ps1` does the same thing with MinGW-w64 and checks that the | ||
| 117 | +output really starts with `MZ` before calling the build a success. | ||
| 118 | + | ||
| 119 | +## Install | ||
| 120 | + | ||
| 121 | +```bash | ||
| 122 | +make install # copies the binary into ~/.scorpiox/plugins (or /workspace/...) | ||
| 123 | +``` | ||
| 124 | + | ||
| 125 | +Or drop `plugin_codex_login` (`.exe` on Windows) into `~/.scorpiox/plugins/`. | ||
| 126 | +SCORPIOX BOT discovers it on the next request with no restart. | ||
| 127 | + | ||
| 128 | +## Tests | ||
| 129 | + | ||
| 130 | +```bash | ||
| 131 | +make test # or: sh tests/run.sh | ||
| 132 | +``` | ||
| 133 | + | ||
| 134 | +`tests/run.sh` is fully offline. It builds the binary if needed, points the | ||
| 135 | +credential paths and both CLIs at fixtures in a temporary directory, and checks | ||
| 136 | +the public contract: the exact `--meta` string, the 401 gate on every action, | ||
| 137 | +JWT expiry decoding, the pending-marker TTL, named accounts, the device-flow | ||
| 138 | +actions, the refresh action, the shape of the HTML page, and a full-git-history | ||
| 139 | +scan for secrets, LAN addresses and machine names. | ||
| 140 | + | ||
| 141 | +## License | ||
| 142 | + | ||
| 143 | +MIT. Copyright (c) 2026 SCORPIOX INC. |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..2cde17d | index 0000000..2cde17d | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/build.ps1 | +++ b/build.ps1 | ||
| @@ -0,0 +1,51 @@ | @@ -0,0 +1,51 @@ | ||
| 1 | +# build.ps1 - Windows MinGW-w64 static build for plugin_codex_login | ||
| 2 | +# | ||
| 3 | +# Usage (from the repo root, PowerShell): | ||
| 4 | +# powershell -ExecutionPolicy Bypass -File build.ps1 | ||
| 5 | +# | ||
| 6 | +# Produces .\plugin_codex_login.exe (static PE, runs on any Windows 10/11 x64 node). | ||
| 7 | +# Requires x86_64-w64-mingw32-gcc or a WinLibs/UCRT gcc on PATH (or C:\gcc16\bin). | ||
| 8 | + | ||
| 9 | +$ErrorActionPreference = "Stop" | ||
| 10 | +$ScriptDir = $PSScriptRoot | ||
| 11 | +Push-Location $ScriptDir | ||
| 12 | +try { | ||
| 13 | + $Src = Join-Path $ScriptDir "src\plugin_codex_login.c" | ||
| 14 | + $Out = Join-Path $ScriptDir "plugin_codex_login.exe" | ||
| 15 | + $Cc = "x86_64-w64-mingw32-gcc" | ||
| 16 | + | ||
| 17 | + if (-not (Test-Path $Src)) { throw "source not found: $Src" } | ||
| 18 | + | ||
| 19 | + # Prefer MinGW-w64, fall back to a plain gcc (WinLibs / MSYS2 UCRT) if present. | ||
| 20 | + if (-not (Get-Command $Cc -ErrorAction SilentlyContinue)) { | ||
| 21 | + if (Test-Path "C:\gcc16\bin\gcc.exe") { | ||
| 22 | + $Cc = "C:\gcc16\bin\gcc.exe" | ||
| 23 | + } elseif (Get-Command gcc -ErrorAction SilentlyContinue) { | ||
| 24 | + $Cc = "gcc" | ||
| 25 | + } else { | ||
| 26 | + throw "no C compiler found (need x86_64-w64-mingw32-gcc or gcc)" | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + # Regenerate the page header so the binary always matches src/ui.html. | ||
| 31 | + if (Get-Command python -ErrorAction SilentlyContinue) { | ||
| 32 | + python tools\gen_ui.py | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + Write-Host "==> building plugin_codex_login.exe with $Cc" | ||
| 36 | + & $Cc -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -static -o $Out $Src | ||
| 37 | + if ($LASTEXITCODE -ne 0) { throw "compile failed (exit $LASTEXITCODE)" } | ||
| 38 | + | ||
| 39 | + if (-not (Test-Path $Out)) { throw "build produced no output" } | ||
| 40 | + | ||
| 41 | + # Sanity: a real PE starts with 'MZ'. An HTML error page saved as .exe does not. | ||
| 42 | + $bytes = [System.IO.File]::ReadAllBytes($Out) | ||
| 43 | + if ($bytes.Length -lt 2 -or $bytes[0] -ne 0x4D -or $bytes[1] -ne 0x5A) { | ||
| 44 | + throw "output is not a PE executable (missing MZ header)" | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + Write-Host "==> ok: $Out ($($bytes.Length) bytes)" | ||
| 48 | + Write-Host " smoke test: .\plugin_codex_login.exe --meta" | ||
| 49 | +} finally { | ||
| 50 | + Pop-Location | ||
| 51 | +} |
| 1 | new file mode 100644 | 1 | new file mode 100644 |
| index 0000000..b5436c6 | index 0000000..b5436c6 | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/plugin.json | +++ b/plugin.json | ||
| @@ -0,0 +1,17 @@ | @@ -0,0 +1,17 @@ | ||
| 1 | +{ | ||
| 2 | + "slug": "codex-login", | ||
| 3 | + "id": "plugin_codex_login", | ||
| 4 | + "name": "codex", | ||
| 5 | + "desc": "Codex login", | ||
| 6 | + "title": "OpenAI Codex Login", | ||
| 7 | + "summary": "OAuth device code and callback login manager for OpenAI Codex on this node. Starts the device flow, shows the one-time user code, polls for approval, then reports session state, token expiry and the named account files - and refreshes the token on demand. The plugin holds no OAuth client secret and never prints, logs or returns a token.", | ||
| 8 | + "category": "devtools", | ||
| 9 | + "license": "MIT", | ||
| 10 | + "capabilities": [ | ||
| 11 | + "credentials_read", | ||
| 12 | + "oauth_device_flow" | ||
| 13 | + ], | ||
| 14 | + "trust_notice": "Executes local scorpiox-codex-login on this node. Credentials are saved locally in ~/.codex/ and never leave your node. The plugin spawns scorpiox-codex-login and scorpiox-codex-refreshtoken (both from SCORPIOX CODE) as child processes; those binaries are what talk to OpenAI over TLS. This plugin itself opens no socket and carries no OAuth client secret. It reads only ~/.codex/auth.json, ~/.codex/accounts/, ~/.claude/.login-pending/ and ~/.claude/scorpiox-env/codex.txt, and every response reports token presence and expiry only - never a token value.", | ||
| 15 | + "author": "SCORPIOX INC", | ||
| 16 | + "homepage": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-codex-login" | ||
| 17 | +} |
| index 5552936..0b9118a 100755 | index 5552936..0b9118a 100755 |
| 1 | new file mode 100755 | 1 | new file mode 100755 |
| index 0000000..dabe7aa | index 0000000..dabe7aa | ||
| --- /dev/null | --- /dev/null | ||
| +++ b/release-dist.sh | +++ b/release-dist.sh | ||
| @@ -0,0 +1,365 @@ | @@ -0,0 +1,365 @@ | ||
| 1 | +#!/usr/bin/env bash | ||
| 2 | +# release-dist.sh - build, scan and publish plugin_codex_login to the plugin registry. | ||
| 3 | +# | ||
| 4 | +# Two paths: | ||
| 5 | +# A. Preferred: if `scorpiox-cli-botplugin` is installed, hand the whole job to it | ||
| 6 | +# (`scorpiox-cli-botplugin publish .`), which does the same work with a richer CLI. | ||
| 7 | +# B. Standalone (this script): secret scan -> multi-target build -> source.tar.gz -> | ||
| 8 | +# manifest.json + SHA256SUMS -> upload -> latest.json / index.json. | ||
| 9 | +# | ||
| 10 | +# Nothing here contains a host address or a credential. Everything environment-specific | ||
| 11 | +# comes from the environment, so this file stays safe to publish: | ||
| 12 | +# | ||
| 13 | +# DIST_HOST ssh target for the dist server (required for upload) | ||
| 14 | +# DIST_PASS ssh password for DIST_HOST (optional, defaults to ssh-agent/key) | ||
| 15 | +# DIST_PATH remote web root of the dist server (default /data/dist) | ||
| 16 | +# MAC_HOST ssh target for a macOS builder (optional: macos-arm64/x86_64) | ||
| 17 | +# MAC_USER / MAC_PASS (optional) | ||
| 18 | +# PLUGINGIT_TOKEN_FILE file holding the plugingit write token (default /etc/scorpiox-plugingit.token) | ||
| 19 | +# PLUGINGIT_REMOTE public mirror URL to push into | ||
| 20 | +# | ||
| 21 | +# Usage: | ||
| 22 | +# ./release-dist.sh # full release | ||
| 23 | +# ./release-dist.sh --dry-run # build + stage in /tmp, no upload | ||
| 24 | +# ./release-dist.sh --no-macos # skip the macOS legs | ||
| 25 | +# ./release-dist.sh --no-mirror # skip the plugingit mirror | ||
| 26 | +# ./release-dist.sh --standalone # use this script even if the CLI is installed | ||
| 27 | +# # (needed for plugins that link extra Win32 libs, | ||
| 28 | +# # which the CLI's fixed link line cannot express) | ||
| 29 | + | ||
| 30 | +set -euo pipefail | ||
| 31 | + | ||
| 32 | +SLUG="codex-login" | ||
| 33 | +ID="plugin_codex_login" | ||
| 34 | +VERSION="1.0.1" | ||
| 35 | +DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins" | ||
| 36 | + | ||
| 37 | +ROOT="$(cd "$(dirname "$0")" && pwd)" | ||
| 38 | +cd "$ROOT" | ||
| 39 | + | ||
| 40 | +DRY_RUN=0 | ||
| 41 | +DO_MACOS=1 | ||
| 42 | +DO_MIRROR=1 | ||
| 43 | +STANDALONE=0 | ||
| 44 | +for arg in "$@"; do | ||
| 45 | + case "$arg" in | ||
| 46 | + --standalone) STANDALONE=1 ;; | ||
| 47 | + --dry-run) DRY_RUN=1 ;; | ||
| 48 | + --no-macos) DO_MACOS=0 ;; | ||
| 49 | + --no-mirror) DO_MIRROR=0 ;; | ||
| 50 | + -h|--help) sed -n '2,32p' "$0"; exit 0 ;; | ||
| 51 | + *) echo "unknown argument: $arg" >&2; exit 2 ;; | ||
| 52 | + esac | ||
| 53 | +done | ||
| 54 | + | ||
| 55 | +DIST_HOST="${DIST_HOST:-}" | ||
| 56 | +DIST_PASS="${DIST_PASS:-}" | ||
| 57 | +DIST_PATH="${DIST_PATH:-/data/dist}" | ||
| 58 | +PLUGINGIT_TOKEN_FILE="${PLUGINGIT_TOKEN_FILE:-/etc/scorpiox-plugingit.token}" | ||
| 59 | +PLUGINGIT_REMOTE="${PLUGINGIT_REMOTE:-https://plugingit.scorpiox.net/scorpiox-bot-plugin-${SLUG}.git}" | ||
| 60 | + | ||
| 61 | +STAGE="/tmp/${SLUG}-dist-${VERSION}-$$" | ||
| 62 | +mkdir -p "$STAGE" | ||
| 63 | +trap 'rm -rf "$STAGE"' EXIT | ||
| 64 | + | ||
| 65 | +say() { printf '\n\033[1m== %s ==\033[0m\n' "$1"; } | ||
| 66 | +die() { printf '\033[31mERROR:\033[0m %s\n' "$1" >&2; exit 1; } | ||
| 67 | + | ||
| 68 | +# ---------------------------------------------------------------- 0. preflight | ||
| 69 | +say "0/7 preflight" | ||
| 70 | + | ||
| 71 | +if [ "$STANDALONE" -eq 0 ] && command -v scorpiox-cli-botplugin >/dev/null 2>&1; then | ||
| 72 | + say "using scorpiox-cli-botplugin publish" | ||
| 73 | + exec scorpiox-cli-botplugin publish "$ROOT" | ||
| 74 | +fi | ||
| 75 | +echo " running the standalone pipeline" | ||
| 76 | + | ||
| 77 | +[ -n "$(git status --porcelain 2>/dev/null)" ] && \ | ||
| 78 | + echo " note: working tree is dirty; source.tar.gz comes from the last commit" | ||
| 79 | +COMMIT=$(git rev-parse HEAD 2>/dev/null || echo "unknown") | ||
| 80 | +echo " version $VERSION" | ||
| 81 | +echo " commit $COMMIT" | ||
| 82 | + | ||
| 83 | +# ---------------------------------------------------------------- 1. secret scan | ||
| 84 | +say "1/7 secret & LAN scan (full history)" | ||
| 85 | + | ||
| 86 | +SCAN_TMP="$STAGE/history.txt" | ||
| 87 | +# Scan every added/removed line of every commit. Two exclusions, both about the | ||
| 88 | +# audit rather than the code: the committer identity line, and any line that is | ||
| 89 | +# itself a scan definition - otherwise this file's own detector patterns (and the | ||
| 90 | +# test suite's identical checks) read as findings in every historical revision of | ||
| 91 | +# them. A real secret on any other line still hits. | ||
| 92 | +git log -p --all 2>/dev/null \ | ||
| 93 | + | grep -v '^Author: ' \ | ||
| 94 | + | grep -vE "scan \"|audit \"|HITS=|grep -E|grep -qE|hits '|P_ROOT=|P_WT=|PK=\"|\$ALL" \ | ||
| 95 | + > "$SCAN_TMP" || : > "$SCAN_TMP" | ||
| 96 | +HITS=0 | ||
| 97 | +scan() { # scan <label> <regex> | ||
| 98 | + local out | ||
| 99 | + out=$(grep -Eo "$2" "$SCAN_TMP" 2>/dev/null | sort -u | head -5 || true) | ||
| 100 | + if [ -n "$out" ]; then | ||
| 101 | + printf ' \033[31mHIT\033[0m %s\n' "$1" | ||
| 102 | + printf '%s\n' "$out" | sed 's/^/ /' | ||
| 103 | + HITS=$((HITS + 1)) | ||
| 104 | + else | ||
| 105 | + printf ' ok %s\n' "$1" | ||
| 106 | + fi | ||
| 107 | +} | ||
| 108 | +scan "LAN IPs 192.168.x.x" '192\.168\.[0-9]+\.[0-9]+' | ||
| 109 | +scan "private 10.x.x.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+' | ||
| 110 | +scan "private 172.16-31.x.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+' | ||
| 111 | +scan "GitHub PAT" 'ghp_[A-Za-z0-9]{36}' | ||
| 112 | +scan "HuggingFace token" 'hf_[A-Za-z0-9]{30,}' | ||
| 113 | +scan "embedded git credentials" 'scorpioxinc:[A-Za-z0-9]{20,}' | ||
| 114 | +scan "hard-coded ssh password" 'sshpass -p [^"$]' | ||
| 115 | +P_ROOT="/codebases" | ||
| 116 | +P_PICOBOT="${P_ROOT}/pico""bot" | ||
| 117 | +P_WT=".work""trees" | ||
| 118 | +scan "internal fleet paths" "/root/tools|${P_PICOBOT}|${P_WT}" | ||
| 119 | +scan "run-token env assignment" 'RUNNER_'"PASS=" | ||
| 120 | +if [ "$HITS" -ne 0 ]; then | ||
| 121 | + die "secret scan found $HITS problem(s) - refusing to publish" | ||
| 122 | +fi | ||
| 123 | +echo " clean: 0 hits" | ||
| 124 | + | ||
| 125 | +# ---------------------------------------------------------------- 2. build | ||
| 126 | +say "2/7 build targets" | ||
| 127 | + | ||
| 128 | +# linux-x86_64 (glibc, native) | ||
| 129 | +if command -v cc >/dev/null 2>&1; then | ||
| 130 | + cc -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings -Wno-format-truncation -I src -o "$STAGE/$ID" src/$ID.c || die "native build failed" | ||
| 131 | + echo " built linux-x86_64 (glibc) $(wc -c < "$STAGE/$ID" | tr -d ' ') bytes" | ||
| 132 | +else | ||
| 133 | + die "no native cc found" | ||
| 134 | +fi | ||
| 135 | + | ||
| 136 | +# linux-x86_64-musl (fully static - the universal Linux artifact) | ||
| 137 | +if command -v musl-gcc >/dev/null 2>&1; then | ||
| 138 | + musl-gcc -O2 -std=c99 -Wall -Wextra -static -Wno-overlength-strings -Wno-format-truncation -I src -o "$STAGE/$ID.musl" src/$ID.c \ | ||
| 139 | + || die "musl build failed" | ||
| 140 | + echo " built linux-x86_64-musl $(wc -c < "$STAGE/$ID.musl" | tr -d ' ') bytes" | ||
| 141 | +else | ||
| 142 | + echo " skip linux-x86_64-musl (no musl-gcc)" | ||
| 143 | +fi | ||
| 144 | + | ||
| 145 | +# linux-arm64-musl (static aarch64 - Raspberry Pi 5 / ARM64 servers) | ||
| 146 | +if command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then | ||
| 147 | + aarch64-linux-gnu-gcc -O2 -std=c99 -Wall -Wextra -static -Wno-overlength-strings -Wno-format-truncation -I src -o "$STAGE/$ID.arm64" src/$ID.c \ | ||
| 148 | + || die "arm64 build failed" | ||
| 149 | + echo " built linux-arm64-musl $(wc -c < "$STAGE/$ID.arm64" | tr -d ' ') bytes" | ||
| 150 | +else | ||
| 151 | + echo " skip linux-arm64-musl (no aarch64-linux-gnu-gcc)" | ||
| 152 | +fi | ||
| 153 | + | ||
| 154 | +# windows-x86_64 (static PE) | ||
| 155 | +if command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1; then | ||
| 156 | + x86_64-w64-mingw32-gcc -O2 -std=c99 -Wall -Wextra -static -Wno-overlength-strings -Wno-format-truncation \ | ||
| 157 | + -I src -o "$STAGE/$ID.exe" src/$ID.c || die "MinGW build failed" | ||
| 158 | + [ "$(head -c 2 "$STAGE/$ID.exe")" = "MZ" ] || die "windows artifact is not a PE" | ||
| 159 | + echo " built windows-x86_64 $(wc -c < "$STAGE/$ID.exe" | tr -d ' ') bytes" | ||
| 160 | +else | ||
| 161 | + echo " skip windows-x86_64 (no MinGW cross compiler)" | ||
| 162 | +fi | ||
| 163 | + | ||
| 164 | +# macOS legs - Mach-O must be produced by Apple clang on a real Mac. | ||
| 165 | +# Both slices are cross-built from one host: -arch arm64 and -arch x86_64. | ||
| 166 | +if [ "$DO_MACOS" -eq 1 ] && [ -n "${MAC_HOST:-}" ]; then | ||
| 167 | + MAC_USER="${MAC_USER:-$(whoami)}" | ||
| 168 | + MAC_DIR="/tmp/${ID}-mac-$$" | ||
| 169 | + echo " building macOS legs on $MAC_HOST" | ||
| 170 | + SSH_MAC=(ssh -o StrictHostKeyChecking=no) | ||
| 171 | + SCP_MAC=(scp -o StrictHostKeyChecking=no) | ||
| 172 | + if [ -n "${MAC_PASS:-}" ]; then | ||
| 173 | + SSH_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SSH_MAC[@]}") | ||
| 174 | + SCP_MAC=(scorpiox-sshpass -p "$MAC_PASS" "${SCP_MAC[@]}") | ||
| 175 | + fi | ||
| 176 | + if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR' && mkdir -p '$MAC_DIR'"; then | ||
| 177 | + tar -czf "$STAGE/src.tgz" src | ||
| 178 | + if "${SCP_MAC[@]}" "$STAGE/src.tgz" "$MAC_USER@$MAC_HOST:$MAC_DIR/src.tgz" \ | ||
| 179 | + && "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "cd '$MAC_DIR' && tar -xzf src.tgz"; then | ||
| 180 | + # Build straight from the uploaded file - no src/ tree on the remote. | ||
| 181 | + for slice in arm64 x86_64; do | ||
| 182 | + if "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" \ | ||
| 183 | + "cd '$MAC_DIR' && clang -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -Wno-format-truncation -I src -arch $slice -o $ID.$slice src/$ID.c" \ | ||
| 184 | + 2>/dev/null; then | ||
| 185 | + "${SCP_MAC[@]}" "$MAC_USER@$MAC_HOST:$MAC_DIR/$ID.$slice" "$STAGE/$ID.macos-$slice" 2>/dev/null \ | ||
| 186 | + && echo " built macos-$slice ($(wc -c < "$STAGE/$ID.macos-$slice" | tr -d ' ') bytes)" | ||
| 187 | + else | ||
| 188 | + echo " warn: macos-$slice build failed on $MAC_HOST" | ||
| 189 | + fi | ||
| 190 | + done | ||
| 191 | + "${SSH_MAC[@]}" "$MAC_USER@$MAC_HOST" "rm -rf '$MAC_DIR'" || true | ||
| 192 | + else | ||
| 193 | + echo " warn: could not upload source to $MAC_HOST" | ||
| 194 | + fi | ||
| 195 | + else | ||
| 196 | + echo " warn: cannot reach macOS builder $MAC_HOST" | ||
| 197 | + fi | ||
| 198 | +else | ||
| 199 | + echo " skip macOS legs (no MAC_HOST or --no-macos)" | ||
| 200 | +fi | ||
| 201 | + | ||
| 202 | +# ---------------------------------------------------------------- 3. manifest | ||
| 203 | +say "3/7 manifest + checksums" | ||
| 204 | + | ||
| 205 | +git archive --format=tar.gz --prefix="${ID}-${VERSION}/" \ | ||
| 206 | + -o "$STAGE/source.tar.gz" HEAD 2>/dev/null \ | ||
| 207 | + || tar -czf "$STAGE/source.tar.gz" --exclude=.git . | ||
| 208 | +echo " source.tar.gz $(wc -c < "$STAGE/source.tar.gz" | tr -d ' ') bytes" | ||
| 209 | + | ||
| 210 | +target_meta() { # target_meta <key> <file> <format> <relpath> | ||
| 211 | + local key="$1" file="$2" fmt="$3" rel="$4" | ||
| 212 | + [ -f "$file" ] || return 0 | ||
| 213 | + local sha size | ||
| 214 | + sha=$(sha256sum "$file" | awk '{print $1}') | ||
| 215 | + size=$(wc -c < "$file" | tr -d ' ') | ||
| 216 | + printf ' "%s": {"url": "%s/%s/%s/%s", "sha256": "%s", "size_bytes": %s, "format": "%s"}' \ | ||
| 217 | + "$key" "$DIST_BASE_URL" "$SLUG" "$VERSION" "$rel" "$sha" "$size" "$fmt" | ||
| 218 | +} | ||
| 219 | + | ||
| 220 | +{ | ||
| 221 | + printf '{\n' | ||
| 222 | + printf ' "slug": "%s",\n' "$SLUG" | ||
| 223 | + printf ' "id": "%s",\n' "$ID" | ||
| 224 | + printf ' "name": "codex",\n' | ||
| 225 | + printf ' "desc": "Codex login",\n' | ||
| 226 | + printf ' "title": "OpenAI Codex Login",\n' | ||
| 227 | + printf ' "summary": "OAuth device code and callback login manager for OpenAI Codex on this node. Starts the device flow, shows the one-time user code, polls for approval, then reports session state, token expiry and the named account files - and refreshes the token on demand. The plugin holds no OAuth client secret and never prints, logs or returns a token: it drives the CLI that ships with SCORPIOX CODE and reads only the credential files that CLI writes.",\n' | ||
| 228 | + printf ' "category": "devtools",\n' | ||
| 229 | + printf ' "version": "%s",\n' "$VERSION" | ||
| 230 | + printf ' "license": "MIT",\n' | ||
| 231 | + printf ' "trust_notice": "Executes local scorpiox-codex-login on this node. Credentials are saved locally in ~/.codex/ and never leave your node. The plugin spawns scorpiox-codex-login and scorpiox-codex-refreshtoken (both from SCORPIOX CODE) as child processes; those binaries are what talk to OpenAI over TLS. This plugin itself opens no socket and carries no OAuth client secret. It reads only ~/.codex/auth.json, ~/.codex/accounts/, ~/.claude/.login-pending/ and ~/.claude/scorpiox-env/codex.txt, and every response reports token presence and expiry only - never a token value.",\n' | ||
| 232 | + printf ' "capabilities": ["credentials_read", "oauth_device_flow"],\n' | ||
| 233 | + printf ' "git_repo_url": "https://plugingit.scorpiox.net/scorpiox-bot-plugin-%s.git",\n' "$SLUG" | ||
| 234 | + printf ' "git_web_url": "https://plugingit.scorpiox.net/repo?name=scorpiox-bot-plugin-%s",\n' "$SLUG" | ||
| 235 | + printf ' "git_commit": "%s",\n' "$COMMIT" | ||
| 236 | + printf ' "source_tarball_url": "%s/%s/%s/source.tar.gz",\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" | ||
| 237 | + printf ' "source_tarball_sha256": "%s",\n' "$(sha256sum "$STAGE/source.tar.gz" | awk '{print $1}')" | ||
| 238 | + printf ' "build_cmd_posix": "cc -O2 -std=c99 -Wall -Wextra -o %s src/%s.c",\n' "$ID" "$ID" | ||
| 239 | + printf ' "build_cmd_win64": "x86_64-w64-mingw32-gcc -O2 -std=c99 -static -o %s.exe src/%s.c",\n' "$ID" "$ID" | ||
| 240 | + printf ' "published_at": "%s",\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" | ||
| 241 | + printf ' "targets": {\n' | ||
| 242 | + first=1 | ||
| 243 | + emit() { if [ -f "$2" ]; then [ $first -eq 1 ] || printf ',\n'; first=0; target_meta "$@"; fi; } | ||
| 244 | + emit "linux-x86_64" "$STAGE/$ID" "elf-x86_64" "linux-x86_64/$ID" | ||
| 245 | + emit "linux-x86_64-musl" "$STAGE/$ID.musl" "elf-x86_64-static" "linux-x86_64-musl/$ID" | ||
| 246 | + emit "linux-arm64-musl" "$STAGE/$ID.arm64" "elf-arm64-static" "linux-arm64-musl/$ID" | ||
| 247 | + emit "macos-arm64" "$STAGE/$ID.macos-arm64" "macho-arm64" "macos-arm64/$ID" | ||
| 248 | + emit "macos-x86_64" "$STAGE/$ID.macos-x86_64" "macho-x86_64" "macos-x86_64/$ID" | ||
| 249 | + emit "windows-x86_64" "$STAGE/$ID.exe" "pe-x86_64" "windows-x86_64/$ID.exe" | ||
| 250 | + printf '\n }\n}\n' | ||
| 251 | +} > "$STAGE/manifest.json" | ||
| 252 | + | ||
| 253 | +python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$STAGE/manifest.json" \ | ||
| 254 | + || die "generated manifest.json is not valid JSON" | ||
| 255 | +echo " manifest.json ok ($(python3 -c 'import json,sys; print(len(json.load(open(sys.argv[1]))["targets"]))' "$STAGE/manifest.json") targets)" | ||
| 256 | + | ||
| 257 | +# SHA256SUMS - relative paths, exactly as published | ||
| 258 | +( cd "$STAGE" | ||
| 259 | + for f in source.tar.gz; do [ -f "$f" ] && sha256sum "$f"; done | ||
| 260 | + [ -f "$ID" ] && sha256sum "$ID" | sed "s| $ID\$| linux-x86_64/$ID|" | ||
| 261 | + [ -f "$ID.musl" ] && sha256sum "$ID.musl" | sed "s| $ID.musl\$| linux-x86_64-musl/$ID|" | ||
| 262 | + [ -f "$ID.arm64" ] && sha256sum "$ID.arm64" | sed "s| $ID.arm64\$| linux-arm64-musl/$ID|" | ||
| 263 | + [ -f "$ID.macos-arm64" ]&& sha256sum "$ID.macos-arm64"| sed "s| $ID.macos-arm64\$| macos-arm64/$ID|" | ||
| 264 | + [ -f "$ID.macos-x86_64" ]&& sha256sum "$ID.macos-x86_64"| sed "s| $ID.macos-x86_64\$| macos-x86_64/$ID|" | ||
| 265 | + [ -f "$ID.exe" ] && sha256sum "$ID.exe" | sed "s| $ID.exe\$| windows-x86_64/$ID.exe|" | ||
| 266 | + true ) > "$STAGE/SHA256SUMS" | ||
| 267 | +echo " SHA256SUMS $(wc -l < "$STAGE/SHA256SUMS" | tr -d ' ') entries" | ||
| 268 | + | ||
| 269 | +# ---------------------------------------------------------------- 4. stage | ||
| 270 | +say "4/7 stage tree" | ||
| 271 | +PKG="$STAGE/pkg" | ||
| 272 | +mkdir -p "$PKG" | ||
| 273 | +cp "$STAGE/manifest.json" "$STAGE/SHA256SUMS" "$STAGE/source.tar.gz" "$PKG/" | ||
| 274 | +[ -f "$STAGE/$ID" ] && { mkdir -p "$PKG/linux-x86_64"; cp "$STAGE/$ID" "$PKG/linux-x86_64/$ID"; } | ||
| 275 | +[ -f "$STAGE/$ID.musl" ] && { mkdir -p "$PKG/linux-x86_64-musl"; cp "$STAGE/$ID.musl" "$PKG/linux-x86_64-musl/$ID"; } | ||
| 276 | +[ -f "$STAGE/$ID.arm64" ] && { mkdir -p "$PKG/linux-arm64-musl"; cp "$STAGE/$ID.arm64" "$PKG/linux-arm64-musl/$ID"; } | ||
| 277 | +[ -f "$STAGE/$ID.macos-arm64" ] && { mkdir -p "$PKG/macos-arm64"; cp "$STAGE/$ID.macos-arm64" "$PKG/macos-arm64/$ID"; } | ||
| 278 | +[ -f "$STAGE/$ID.macos-x86_64" ]&& { mkdir -p "$PKG/macos-x86_64"; cp "$STAGE/$ID.macos-x86_64" "$PKG/macos-x86_64/$ID"; } | ||
| 279 | +[ -f "$STAGE/$ID.exe" ] && { mkdir -p "$PKG/windows-x86_64"; cp "$STAGE/$ID.exe" "$PKG/windows-x86_64/$ID.exe"; } | ||
| 280 | +echo " staged: $(find "$PKG" -type f | wc -l | tr -d ' ') file(s)" | ||
| 281 | + | ||
| 282 | +if [ "$DRY_RUN" -eq 1 ]; then | ||
| 283 | + say "dry run - leaving the staging tree in $PKG" | ||
| 284 | + trap - EXIT | ||
| 285 | + exit 0 | ||
| 286 | +fi | ||
| 287 | + | ||
| 288 | +# ---------------------------------------------------------------- 5. upload | ||
| 289 | +say "5/7 upload to the dist server" | ||
| 290 | +[ -n "$DIST_HOST" ] || die "DIST_HOST is not set (export DIST_HOST=<ssh-target>)" | ||
| 291 | +SSH=(ssh -o StrictHostKeyChecking=no) | ||
| 292 | +SCP=(scp -o StrictHostKeyChecking=no) | ||
| 293 | +if [ -n "$DIST_PASS" ]; then | ||
| 294 | + SSH=(scorpiox-sshpass -p "$DIST_PASS" "${SSH[@]}") | ||
| 295 | + SCP=(scorpiox-sshpass -p "$DIST_PASS" "${SCP[@]}") | ||
| 296 | +fi | ||
| 297 | +REMOTE_DIR="$DIST_PATH/bot-plugins/$SLUG/$VERSION" | ||
| 298 | + | ||
| 299 | +"${SSH[@]}" "$DIST_HOST" "mkdir -p '$REMOTE_DIR' '$DIST_PATH/bot-plugins/$SLUG'" \ | ||
| 300 | + || die "cannot create $REMOTE_DIR on $DIST_HOST" | ||
| 301 | +# Stage to one tarball, transfer it, extract remotely. Streaming tar through | ||
| 302 | +# ssh corrupts on some OpenSSH builds (remote gzip dies with SIGPIPE), and | ||
| 303 | +# OpenSSH 9 refuses to scp a bare "." source. | ||
| 304 | +tar -czf "$STAGE/pkg.tar.gz" -C "$PKG" . || die "packaging failed" | ||
| 305 | +"${SCP[@]}" -q "$STAGE/pkg.tar.gz" "$DIST_HOST:$REMOTE_DIR/pkg.tar.gz" || die "upload failed" | ||
| 306 | +"${SSH[@]}" "$DIST_HOST" "cd '$REMOTE_DIR' && tar -xzf pkg.tar.gz && rm -f pkg.tar.gz" \ | ||
| 307 | + || die "remote extract failed" | ||
| 308 | +echo " uploaded to $DIST_HOST:$REMOTE_DIR" | ||
| 309 | + | ||
| 310 | +# latest.json = copy of this version's manifest | ||
| 311 | +"${SSH[@]}" "$DIST_HOST" "cp -f '$REMOTE_DIR/manifest.json' '$DIST_PATH/bot-plugins/$SLUG/latest.json'" \ | ||
| 312 | + || die "latest.json update failed" | ||
| 313 | +echo " updated bot-plugins/$SLUG/latest.json" | ||
| 314 | + | ||
| 315 | +# index.json - merge, preserving every sibling <slug>/latest.json | ||
| 316 | +say "index.json merge" | ||
| 317 | +"${SSH[@]}" "$DIST_HOST" "cd '$DIST_PATH/bot-plugins' && python3 - '$SLUG' <<'PY' | ||
| 318 | +import json, os, sys, glob | ||
| 319 | +slug = sys.argv[1] | ||
| 320 | +entries = {} | ||
| 321 | +for p in sorted(glob.glob('*/latest.json')): | ||
| 322 | + s = os.path.dirname(p) | ||
| 323 | + try: | ||
| 324 | + entries[s] = json.load(open(p)) | ||
| 325 | + except Exception: | ||
| 326 | + pass | ||
| 327 | +index = { | ||
| 328 | + 'ok': True, | ||
| 329 | + 'service': 'bot-plugins-dist', | ||
| 330 | + 'dist_base_url': 'https://dist.scorpiox.net/bot-plugins', | ||
| 331 | + 'count': len(entries), | ||
| 332 | + 'slugs': sorted(entries), | ||
| 333 | + 'plugins': [entries[k] for k in sorted(entries)], | ||
| 334 | +} | ||
| 335 | +open('index.json.tmp', 'w').write(json.dumps(index, indent=2) + chr(10)) | ||
| 336 | +os.replace('index.json.tmp', 'index.json') | ||
| 337 | +print(' index.json now has %d plugin(s): %s' % (len(entries), ', '.join(sorted(entries)))) | ||
| 338 | +PY" || die "index.json merge failed" | ||
| 339 | + | ||
| 340 | +# ---------------------------------------------------------------- 6. mirror | ||
| 341 | +say "6/7 plugingit mirror" | ||
| 342 | +if [ "$DO_MIRROR" -eq 1 ] && [ -f "$PLUGINGIT_TOKEN_FILE" ]; then | ||
| 343 | + TOKEN=$(tr -d ' \r\n' < "$PLUGINGIT_TOKEN_FILE") | ||
| 344 | + AUTH_REMOTE=$(printf '%s' "$PLUGINGIT_REMOTE" | sed "s|https://|https://token:${TOKEN}@|") | ||
| 345 | + git push --all "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || die "mirror push failed" | ||
| 346 | + git push --tags "$AUTH_REMOTE" 2>&1 | sed 's/^/ /' || true | ||
| 347 | + echo " mirrored to $PLUGINGIT_REMOTE" | ||
| 348 | +else | ||
| 349 | + echo " skip (no $PLUGINGIT_TOKEN_FILE or --no-mirror)" | ||
| 350 | +fi | ||
| 351 | + | ||
| 352 | +# ---------------------------------------------------------------- 7. verify | ||
| 353 | +say "7/7 verify" | ||
| 354 | +CODE=$(curl -fsS -o "$STAGE/latest.check" -w '%{http_code}' \ | ||
| 355 | + "$DIST_BASE_URL/$SLUG/latest.json" || echo 000) | ||
| 356 | +if [ "$CODE" = "200" ]; then | ||
| 357 | + python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(" latest.json OK - %s v%s, %d target(s)" % (d["slug"], d["version"], len(d["targets"])))' \ | ||
| 358 | + "$STAGE/latest.check" || echo " warn: latest.json is not valid JSON" | ||
| 359 | +else | ||
| 360 | + echo " warn: $DIST_BASE_URL/$SLUG/latest.json returned HTTP $CODE" | ||
| 361 | +fi | ||
| 362 | + | ||
| 363 | +printf '\n\033[1mrelease complete\033[0m\n' | ||
| 364 | +printf ' registry %s/%s/%s/\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" | ||
| 365 | +printf ' manifest %s/%s/%s/manifest.json\n' "$DIST_BASE_URL" "$SLUG" "$VERSION" |
| index 7144eef..2e5f5aa 100644 | index 7144eef..2e5f5aa 100644 | ||
| --- a/src/plugin_codex_login.c | --- a/src/plugin_codex_login.c | ||
| +++ b/src/plugin_codex_login.c | +++ b/src/plugin_codex_login.c | ||
| @@ -433,16 +433,6 @@ static void account_path(const char *name, char *out, size_t n) | @@ -433,16 +433,6 @@ static void account_path(const char *name, char *out, size_t n) | ||
| 433 | 433 | ||
| 434 | /* Resolve the account this request operates on: ?account= wins, then | 434 | /* Resolve the account this request operates on: ?account= wins, then |
| 435 | * CODEX_ACCOUNT, then the default file. */ | 435 | * CODEX_ACCOUNT, then the default file. */ |
| 436 | -static void resolve_account(const char *qs, char *out, size_t outsz) | ||
| 437 | -{ | ||
| 438 | - const char *env; | ||
| 439 | - | ||
| 440 | - out[0] = '\0'; | ||
| 441 | - if (qs && get_query_val(qs, "account", out, outsz) && out[0]) return; | ||
| 442 | - env = getenv("CODEX_ACCOUNT"); | ||
| 443 | - if (env && *env) { snprintf(out, outsz, "%s", env); return; } | ||
| 444 | -} | ||
| 445 | - | ||
| 446 | static int name_valid(const char *n) | 436 | static int name_valid(const char *n) |
| 447 | { | 437 | { |
| 448 | const char *p; | 438 | const char *p; |
| @@ -456,6 +446,22 @@ static int name_valid(const char *n) | @@ -456,6 +446,22 @@ static int name_valid(const char *n) | ||
| 456 | return 1; | 446 | return 1; |
| 457 | } | 447 | } |
| 458 | 448 | ||
| 449 | +static void resolve_account(const char *qs, char *out, size_t outsz) | ||
| 450 | +{ | ||
| 451 | + const char *env; | ||
| 452 | + | ||
| 453 | + out[0] = '\0'; | ||
| 454 | + if (qs && get_query_val(qs, "account", out, outsz) && out[0]) { | ||
| 455 | + /* A name that is not path-safe is dropped rather than sanitised: the | ||
| 456 | + * only thing it can do is name a file, and silently rewriting it would | ||
| 457 | + * make the panel report a different account than the one asked for. */ | ||
| 458 | + if (!name_valid(out)) out[0] = '\0'; | ||
| 459 | + else return; | ||
| 460 | + } | ||
| 461 | + env = getenv("CODEX_ACCOUNT"); | ||
| 462 | + if (env && *env && name_valid(env)) { snprintf(out, outsz, "%s", env); return; } | ||
| 463 | +} | ||
| 464 | + | ||
| 459 | /* Where the credentials for the resolved account actually live. */ | 465 | /* Where the credentials for the resolved account actually live. */ |
| 460 | static void active_creds_path(const char *account, char *out, size_t n) | 466 | static void active_creds_path(const char *account, char *out, size_t n) |
| 461 | { | 467 | { |
| @@ -977,14 +983,18 @@ static void scan_creds(const char *json, status_t *st, account_t *acc) | @@ -977,14 +983,18 @@ static void scan_creds(const char *json, status_t *st, account_t *acc) | ||
| 977 | tokend = docend; | 983 | tokend = docend; |
| 978 | } | 984 | } |
| 979 | 985 | ||
| 980 | - if (json_str_field(tok, tokend, "access_token", access, sizeof(access))) { | 986 | + /* An empty string is not a session. json_str_field reports a hit for "" |
| 987 | + * as well, so the length is what decides, not the presence of the key. */ | ||
| 988 | + if (json_str_field(tok, tokend, "access_token", access, sizeof(access)) && | ||
| 989 | + access[0]) { | ||
| 981 | exp = jwt_exp_seconds(access); | 990 | exp = jwt_exp_seconds(access); |
| 982 | st->logged_in = 1; | 991 | st->logged_in = 1; |
| 983 | - } else { | 992 | + } else if (json_str_present(tok, tokend, "access_token")) { |
| 984 | - /* Presence without copying: a token can be several kilobytes and the | 993 | + /* A token too long for the scratch buffer still counts: the decoder |
| 985 | - * account list keeps one entry per file, so a 4 KB scratch buffer per | 994 | + * needs only the payload between the first two dots, and a real JWT |
| 986 | - * file would be wasteful. */ | 995 | + * payload never runs past a few hundred bytes, so a truncated copy is |
| 987 | - if (json_str_present(tok, tokend, "access_token")) st->logged_in = 1; | 996 | + * enough to read the exp claim. */ |
| 997 | + st->logged_in = 1; | ||
| 988 | } | 998 | } |
| 989 | if (json_str_present(tok, tokend, "refresh_token")) { | 999 | if (json_str_present(tok, tokend, "refresh_token")) { |
| 990 | st->has_refresh = 1; | 1000 | st->has_refresh = 1; |
| @@ -1132,7 +1142,7 @@ static void scan_accounts(status_t *st, long long now_sec) | @@ -1132,7 +1142,7 @@ static void scan_accounts(status_t *st, long long now_sec) | ||
| 1132 | * proved. The file-derived values win where they disagree: the CLI reports | 1142 | * proved. The file-derived values win where they disagree: the CLI reports |
| 1133 | * expires_at 0 because auth.json has no expiry field, so a CLI answer of 0 | 1143 | * expires_at 0 because auth.json has no expiry field, so a CLI answer of 0 |
| 1134 | * must never clear a countdown this plugin computed from the token itself. */ | 1144 | * must never clear a countdown this plugin computed from the token itself. */ |
| 1135 | -static void scan_cli(status_t *st, const char *name) | 1145 | +static void scan_cli(status_t *st, const char *name, int file_read) |
| 1136 | { | 1146 | { |
| 1137 | buf_t out; | 1147 | buf_t out; |
| 1138 | const char *j; | 1148 | const char *j; |
| @@ -1168,15 +1178,22 @@ static void scan_cli(status_t *st, const char *name) | @@ -1168,15 +1178,22 @@ static void scan_cli(status_t *st, const char *name) | ||
| 1168 | long long e; | 1178 | long long e; |
| 1169 | char acct[NAME_SZ] = {0}; | 1179 | char acct[NAME_SZ] = {0}; |
| 1170 | 1180 | ||
| 1171 | - if (json_bool_field(j, end, "logged_in", 0)) st->logged_in = 1; | 1181 | + /* file_read tells the difference between "this node has no readable |
| 1172 | - if (json_bool_field(j, end, "has_refresh", 0)) st->has_refresh = 1; | 1182 | + * credential file" and "the file is here and says nothing". In the |
| 1183 | + * second case the file wins: the CLI decides logged_in by grepping for | ||
| 1184 | + * the key name, so an empty "access_token": "" reads as a session | ||
| 1185 | + * there and as no session in the file. */ | ||
| 1186 | + if (!file_read) { | ||
| 1187 | + if (json_bool_field(j, end, "logged_in", 0)) st->logged_in = 1; | ||
| 1188 | + if (json_bool_field(j, end, "has_refresh", 0)) st->has_refresh = 1; | ||
| 1189 | + } | ||
| 1173 | if (json_bool_field(j, end, "pending", 0)) st->pending = 1; | 1190 | if (json_bool_field(j, end, "pending", 0)) st->pending = 1; |
| 1174 | e = json_num_field(j, end, "expires_at"); | 1191 | e = json_num_field(j, end, "expires_at"); |
| 1175 | if (e > 0) { | 1192 | if (e > 0) { |
| 1176 | long long sec = (e > 100000000000LL) ? (e / 1000) : e; | 1193 | long long sec = (e > 100000000000LL) ? (e / 1000) : e; |
| 1177 | if (st->expires_at == 0 || sec > st->expires_at) st->expires_at = sec; | 1194 | if (st->expires_at == 0 || sec > st->expires_at) st->expires_at = sec; |
| 1178 | } | 1195 | } |
| 1179 | - if (json_str_field(j, end, "account", acct, sizeof(acct)) && acct[0]) | 1196 | + if (!st->account[0] && json_str_field(j, end, "account", acct, sizeof(acct)) && acct[0]) |
| 1180 | snprintf(st->account, sizeof(st->account), "%s", acct); | 1197 | snprintf(st->account, sizeof(st->account), "%s", acct); |
| 1181 | if (!json_bool_field(j, end, "ok", 1)) | 1198 | if (!json_bool_field(j, end, "ok", 1)) |
| 1182 | json_str_field(j, end, "error", st->cli_error, sizeof(st->cli_error)); | 1199 | json_str_field(j, end, "error", st->cli_error, sizeof(st->cli_error)); |
| @@ -1188,6 +1205,7 @@ static void collect_status(status_t *st, const char *name) | @@ -1188,6 +1205,7 @@ static void collect_status(status_t *st, const char *name) | ||
| 1188 | { | 1205 | { |
| 1189 | char *json; | 1206 | char *json; |
| 1190 | long long now_sec = (long long)time(NULL); | 1207 | long long now_sec = (long long)time(NULL); |
| 1208 | + int file_read = 0; | ||
| 1191 | 1209 | ||
| 1192 | memset(st, 0, sizeof(*st)); | 1210 | memset(st, 0, sizeof(*st)); |
| 1193 | st->pending_age = -1; | 1211 | st->pending_age = -1; |
| @@ -1207,9 +1225,10 @@ static void collect_status(status_t *st, const char *name) | @@ -1207,9 +1225,10 @@ static void collect_status(status_t *st, const char *name) | ||
| 1207 | if (json) { | 1225 | if (json) { |
| 1208 | scan_creds(json, st, NULL); | 1226 | scan_creds(json, st, NULL); |
| 1209 | free(json); | 1227 | free(json); |
| 1228 | + file_read = 1; | ||
| 1210 | } | 1229 | } |
| 1211 | 1230 | ||
| 1212 | - scan_cli(st, name); | 1231 | + scan_cli(st, name, file_read); |
| 1213 | scan_profile(st); | 1232 | scan_profile(st); |
| 1214 | 1233 | ||
| 1215 | /* The pending marker is the only evidence available without a network, and | 1234 | /* The pending marker is the only evidence available without a network, and |
| @@ -1328,7 +1347,6 @@ static void emit_error(int code, const char *status, const char *err, const char | @@ -1328,7 +1347,6 @@ static void emit_error(int code, const char *status, const char *err, const char | ||
| 1328 | buf_json(&b, detail); | 1347 | buf_json(&b, detail); |
| 1329 | } | 1348 | } |
| 1330 | buf_adds(&b, "}\n"); | 1349 | buf_adds(&b, "}\n"); |
| 1331 | - json_headers(); | ||
| 1332 | fputs(buf_str(&b), stdout); | 1350 | fputs(buf_str(&b), stdout); |
| 1333 | buf_free(&b); | 1351 | buf_free(&b); |
| 1334 | } | 1352 | } |
| index af4510e..a7c6b25 100755 | index af4510e..a7c6b25 100755 | ||
| --- a/tests/mock_login.sh | --- a/tests/mock_login.sh | ||
| +++ b/tests/mock_login.sh | +++ b/tests/mock_login.sh | ||
| @@ -35,7 +35,11 @@ case "$1" in | @@ -35,7 +35,11 @@ case "$1" in | ||
| 35 | "$ACC" "$li" "$CREDS" "$hr" "$pend" | 35 | "$ACC" "$li" "$CREDS" "$hr" "$pend" |
| 36 | ;; | 36 | ;; |
| 37 | --start) | 37 | --start) |
| 38 | - if [ "$MOCK_EXISTS" = "1" ]; then | 38 | + # --force is the only thing that makes an existing credential file a |
| 39 | + # non-error, so honour it here the way the real CLI does. | ||
| 40 | + _force=0 | ||
| 41 | + for _a in "$@"; do [ "$_a" = "--force" ] && _force=1; done | ||
| 42 | + if [ "$MOCK_EXISTS" = "1" ] && [ "$_force" != "1" ]; then | ||
| 39 | printf '{"ok":false,"provider":"codex","error":"exists","detail":"credentials already exist; pass --force to replace"}\n' | 43 | printf '{"ok":false,"provider":"codex","error":"exists","detail":"credentials already exist; pass --force to replace"}\n' |
| 40 | exit 1 | 44 | exit 1 |
| 41 | fi | 45 | fi |
| index 3b17154..90eab0b 100755 | index 3b17154..90eab0b 100755 | ||
| --- a/tests/run.sh | --- a/tests/run.sh | ||
| +++ b/tests/run.sh | +++ b/tests/run.sh | ||
| @@ -64,11 +64,9 @@ section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; } | @@ -64,11 +64,9 @@ section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; } | ||
| 64 | # strip CGI headers if present, drop CRs, keep the payload | 64 | # strip CGI headers if present, drop CRs, keep the payload |
| 65 | body() { | 65 | body() { |
| 66 | printf '%s\n' "$1" | tr -d '\r' | awk ' | 66 | printf '%s\n' "$1" | tr -d '\r' | awk ' |
| 67 | - BEGIN { inhdr = 0 } | 67 | + BEGIN { inhdr = 1 } |
| 68 | - NR == 1 && ($0 ~ /^(Content-Type|Status|Cache-Control|X-Content-Type-Options|X-Frame-Options|Referrer-Policy|Content-Security-Policy):/) { inhdr = 1; next } | 68 | + inhdr && ($0 ~ /^(Content-Type|Status|Cache-Control|X-Content-Type-Options|X-Frame-Options|Referrer-Policy|Content-Security-Policy):/ || $0 == "") { next } |
| 69 | - inhdr && $0 == "" { inhdr = 0; next } | 69 | + { inhdr = 0; print } |
| 70 | - inhdr { next } | ||
| 71 | - { print } | ||
| 72 | ' | 70 | ' |
| 73 | } | 71 | } |
| 74 | 72 | ||
| @@ -285,8 +283,11 @@ OUT=$(api "action=status&account=work&$PWDQ") | @@ -285,8 +283,11 @@ OUT=$(api "action=status&account=work&$PWDQ") | ||
| 285 | json_ok "status for a named account reads that file" "$OUT" \ | 283 | json_ok "status for a named account reads that file" "$OUT" \ |
| 286 | 'd["account"]=="work" and d["logged_in"] is True' | 284 | 'd["account"]=="work" and d["logged_in"] is True' |
| 287 | check "named account path is reported" "work.json" "$OUT" | 285 | check "named account path is reported" "work.json" "$OUT" |
| 288 | -BADNAME=$(api "action=status&account=../etc&$PWDQ") | 286 | +BADNAME=$(api "action=status&account=../etc/passwd&$PWDQ") |
| 289 | -check_not "a path-traversal account name does not read /etc" 'etc' "$BADNAME" | 287 | +json_ok "a path-traversal account name is ignored, not followed" "$BADNAME" \ |
| 288 | + 'd["ok"] is True and d["account"]=="default" and "/etc" not in d["credentials_path"]' | ||
| 289 | +BADSTART=$(api "action=start&account=..%2Fetc&$PWDQ") | ||
| 290 | +check_not "a traversal name never reaches a path in start" '/etc' "$BADSTART" | ||
| 290 | 291 | ||
| 291 | # ---------------------------------------------------------------- pending marker | 292 | # ---------------------------------------------------------------- pending marker |
| 292 | section "pending device flow marker" | 293 | section "pending device flow marker" |
| @@ -320,13 +321,16 @@ check "start writes the pending marker" "codex-default.json" "$FIX/pend"/*.json | @@ -320,13 +321,16 @@ check "start writes the pending marker" "codex-default.json" "$FIX/pend"/*.json | ||
| 320 | pass "start leaves a marker for poll to find" | 321 | pass "start leaves a marker for poll to find" |
| 321 | check_not "start never echoes a token" 'SIGNATUREPART' "$OUT" | 322 | check_not "start never echoes a token" 'SIGNATUREPART' "$OUT" |
| 322 | 323 | ||
| 323 | -# already-logged-in path | 324 | +# already-logged-in path: the CLI refuses --start when credentials exist. |
| 324 | mkcreds "$FIX/auth.json" "$TOK_FUTURE" "rt-exists" "2026-11-12T12:00:00.000000Z" | 325 | mkcreds "$FIX/auth.json" "$TOK_FUTURE" "rt-exists" "2026-11-12T12:00:00.000000Z" |
| 325 | -OUT=$(api "action=start&$PWDQ") | 326 | +OUT=$(body "$(MOCK_EXISTS=1 QUERY_STRING="action=start&$PWDQ" $BIN)") |
| 326 | json_ok "start on existing creds reports already_logged_in" "$OUT" \ | 327 | json_ok "start on existing creds reports already_logged_in" "$OUT" \ |
| 327 | 'd["ok"] is True and d["already_logged_in"] is True' | 328 | 'd["ok"] is True and d["already_logged_in"] is True' |
| 328 | json_ok "the already-logged-in answer keeps logged_in true" "$OUT" 'd["logged_in"] is True' | 329 | json_ok "the already-logged-in answer keeps logged_in true" "$OUT" 'd["logged_in"] is True' |
| 329 | -check_not "the already-logged-in answer is not an error surface" '"ok":false' "$OUT" | 330 | +json_ok "the already-logged-in answer tells the user how to replace" "$OUT" \ |
| 331 | + '"force=1" in d["hint"]' | ||
| 332 | +OUT=$(body "$(MOCK_EXISTS=1 QUERY_STRING="action=start&force=1&$PWDQ" $BIN)") | ||
| 333 | +json_ok "force=1 reaches the CLI as --force" "$OUT" 'd["ok"] is True and d["flow"]=="device_code"' | ||
| 330 | 334 | ||
| 331 | # a CLI answer with no user code | 335 | # a CLI answer with no user code |
| 332 | OUT=$(body "$(MOCK_NOUSER=1 QUERY_STRING="action=start&$PWDQ" $BIN)") | 336 | OUT=$(body "$(MOCK_NOUSER=1 QUERY_STRING="action=start&$PWDQ" $BIN)") |
| @@ -405,10 +409,10 @@ json_ok "a missing refresh CLI is cli_unavailable" "$OUT" 'd["error"]=="cli_unav | @@ -405,10 +409,10 @@ json_ok "a missing refresh CLI is cli_unavailable" "$OUT" 'd["error"]=="cli_unav | ||
| 405 | 409 | ||
| 406 | # ---------------------------------------------------------------- unknown action | 410 | # ---------------------------------------------------------------- unknown action |
| 407 | section "unknown action" | 411 | section "unknown action" |
| 408 | -OUT=$(api "action=bogus&$PWDQ") | 412 | +RAW=$(apiraw "action=bogus&$PWDQ") |
| 409 | -json_ok "an unknown action is a 400" "$(apiraw "action=bogus&$PWDQ")" 'True' | 413 | +check "unknown action 400s" "Status: 400 Bad Request" "$RAW" |
| 410 | -check "unknown action 400s" "Status: 400 Bad Request" "$(apiraw "action=bogus&$PWDQ")" | 414 | +OUT=$(body "$RAW") |
| 411 | -json_ok "unknown action names the problem" "$OUT" 'd["error"]=="unknown_action"' | 415 | +json_ok "unknown action names the problem" "$OUT" 'd["ok"] is False and d["error"]=="unknown_action"' |
| 412 | 416 | ||
| 413 | # ---------------------------------------------------------------- format=json | 417 | # ---------------------------------------------------------------- format=json |
| 414 | section "format=json" | 418 | section "format=json" |
| @@ -435,7 +439,13 @@ check "page is fluid down to a phone" "max-width:520px" "$PAGE" | @@ -435,7 +439,13 @@ check "page is fluid down to a phone" "max-width:520px" "$PAGE" | ||
| 435 | check_not "page loads no external script" '<script src=' "$PAGE" | 439 | check_not "page loads no external script" '<script src=' "$PAGE" |
| 436 | check_not "page references a CDN" 'cdnjs' "$PAGE" | 440 | check_not "page references a CDN" 'cdnjs' "$PAGE" |
| 437 | check_not "page references googleapis" 'googleapis' "$PAGE" | 441 | check_not "page references googleapis" 'googleapis' "$PAGE" |
| 438 | -check_not "page has no emoji" 'U+1F' "$PAGE" | 442 | +nonascii() { # nonascii <file> - fails when any byte is outside printable ASCII |
| 443 | + if LC_ALL=C grep -qP '[^\x09\x0a\x20-\x7e]' "$1" 2>/dev/null; then | ||
| 444 | + fail "$2 (non-ASCII bytes present)"; LC_ALL=C grep -oP '[^\x09\x0a\x20-\x7e]' "$1" | head -3 | sed 's/^/ /' | ||
| 445 | + else pass "$2" | ||
| 446 | + fi | ||
| 447 | +} | ||
| 448 | +nonascii src/ui.html "the page is pure ASCII" | ||
| 439 | check "page fetches through the hub-aware router" "'/plugins?plugin='" "$PAGE" | 449 | check "page fetches through the hub-aware router" "'/plugins?plugin='" "$PAGE" |
| 440 | check "page keeps the node password out of the URL path" "X-Bot-Password" "$PAGE" | 450 | check "page keeps the node password out of the URL path" "X-Bot-Password" "$PAGE" |
| 441 | check "page links to the public source mirror" "plugingit.scorpiox.net" "$PAGE" | 451 | check "page links to the public source mirror" "plugingit.scorpiox.net" "$PAGE" |
| @@ -456,10 +466,12 @@ scan "huggingface token (hf_)" 'hf_[A-Za-z0-9]{20,}' | @@ -456,10 +466,12 @@ scan "huggingface token (hf_)" 'hf_[A-Za-z0-9]{20,}' | ||
| 456 | scan "generic api key assignment" '([A-Za-z0-9_]+_?(API_KEY|TOKEN|SECRET|PAT))[[:space:]]*[:=][[:space:]]*"[A-Za-z0-9+/_-]{20,}"' | 466 | scan "generic api key assignment" '([A-Za-z0-9_]+_?(API_KEY|TOKEN|SECRET|PAT))[[:space:]]*[:=][[:space:]]*"[A-Za-z0-9+/_-]{20,}"' |
| 457 | scan "internal 192.168 address" '192\.168\.[0-9]+\.[0-9]+' | 467 | scan "internal 192.168 address" '192\.168\.[0-9]+\.[0-9]+' |
| 458 | scan "internal 10.x address" '\b10\.[0-9]+\.[0-9]+\.[0-9]+\b' | 468 | scan "internal 10.x address" '\b10\.[0-9]+\.[0-9]+\.[0-9]+\b' |
| 459 | -scan "internal hostname .scorpiox.net in code" 'scorpiox\.net' | 469 | +scan "internal machine name intel-ultra2" 'intel-ultra2' |
| 470 | +scan "internal machine name macmini" 'macmini' | ||
| 471 | +scan "ssh user@host form" '[a-z]+@192\.' | ||
| 460 | scan "mac address" '([0-9a-f]{2}:){5}[0-9a-f]{2}' | 472 | scan "mac address" '([0-9a-f]{2}:){5}[0-9a-f]{2}' |
| 461 | -check_not "no emoji in the C source" 'U+1F' src/plugin_codex_login.c | 473 | +nonascii src/plugin_codex_login.c "the C source is pure ASCII" |
| 462 | -check_not "no emoji in the page" 'U+1F' src/ui.html | 474 | +nonascii src/ui.html "the page source is pure ASCII" |
| 463 | check_not "no Content-Length header emitted" 'Content-Length' src/plugin_codex_login.c | 475 | check_not "no Content-Length header emitted" 'Content-Length' src/plugin_codex_login.c |
| 464 | found "no-Content-Length rule documented" "Content-Length" README.md 2>/dev/null || pass "README documents the header rule" | 476 | found "no-Content-Length rule documented" "Content-Length" README.md 2>/dev/null || pass "README documents the header rule" |
| 465 | 477 | ||
| 466 | 478 |