## Summary A page reached through a port forward could not sign in. The gateway accepted a browser `Origin` only when it named the port the gateway bound, and VS Code and `ssh -L` pick another local port whenever the remote one is taken on the browser's machine, so every `POST /auth/exchange` answered `403 bad origin` and every `/rpc` upgrade was refused with it. The sign-in page reported an expired link over a refusal it cannot see, and re-running `raven web` minted one more link into the same refusal. Measured on 2026-10-13: 13 refusals in six minutes, each from a page at `127.0.0.1:18793` against a gateway on 18792, all within a minute of their nonce being minted. The same check had already forced the container's nginx to rewrite `Origin` for every request it proxies. `_origin_ok` now also accepts an `Origin` that asks the request's own origin (`Origin` equals `Host`) on a loopback name. That is what a browser sends through a forward, since a forwarder relays `Host` unchanged. It still refuses a page another local server shows, because cookies and SameSite ignore the port and that origin names a port this page does not ask; and it still refuses DNS rebinding, where a name resolving to 127.0.0.1 asks its own origin and the two agree. The bound-port allowlist stays for the container's nginx, which forwards `Host` without a port. Three smaller changes ride with it: - The session cookie is named for the port the browser reached rather than the port bound, so two gateways that both bound 18792 no longer share one jar entry when one browser reaches them through different forwards. A `Host` without a port keeps the bound port, which is what the container's auto-login sets. - `handle_auth_exchange` answers with the reason it refused, and the sign-in page shows that text rather than calling every failure an expired link. - `_open` prints the sign-in link every time, and before handing it to a browser. `webbrowser.open` returns True whenever it ran something: VS Code's `browser.sh` exits 0 whatever happened, and a machine with no display hands the URL to a console browser that has no JavaScript and never redeems the nonce. The old fallback was unreachable, so the link was never printed at all. The standalone serve test now uses a real aiohttp `Application` while replacing only `AppRunner` and `TCPSite`. It selects temporary UI locations for both an absent page and a built page, and checks that the response revalidation hook is registered only for the built page. This removes dependence on whether the checkout contains a UI build. This PR also retains the reviewed Windows test guards and separate `RAVEN_HOME` directories for real-upgrade fixtures. ## Type - [x] Fix - [ ] Feature - [ ] Docs - [ ] CI / tooling - [ ] Refactor - [ ] Other ## Verification Linux fixture follow-up (Python 3.12.13; the three-file suite and diff coverage were rechecked after rebasing onto the current main): - Before the fix, parameterizing `TestTheSignInLink` over an absent page and a built page produced 4 passed / 1 failed. Only the built-page case failed, with the same missing `on_response_prepare` as CI. - `uv run --no-sync pytest -q -n 0 tests/test_cli_serve_commands.py::TestTheSignInLink`: 5 passed after the fix. - `uv run --no-sync pytest -q tests/test_rpc_transport.py tests/test_cli_serve_commands.py tests/test_docker_runtime.py --cov=raven.rpc.transports.ws --cov=raven.cli.serve_commands --cov-report=json:<coverage-json>`: 234 passed. `<coverage-json>` denotes the local coverage output file. - `uv run --no-sync python scripts/coverage_gate.py --coverage-json <coverage-json> diff --base-ref origin/main`: 100.00% (32/32 executable changed lines), passing the 90% threshold. This measures diff coverage, not the full-suite ratchet. - A temporary test plugin removed the response hook without changing the checkout. The built-page case failed its hook assertion and the absent-page control passed (1 failed / 1 passed). - `uv run --no-sync ruff check tests/test_cli_serve_commands.py raven/cli/serve_commands.py`, `uv run --no-sync ruff format --check tests/test_cli_serve_commands.py raven/cli/serve_commands.py`, and `uv run --no-sync ty check raven/cli/serve_commands.py tests/test_cli_serve_commands.py`: passed. - `UV_NO_SYNC=1 make lint-python lint-imports lint-deps lint-types`: passed after rebase (2268 Python files formatted; 10 import contracts kept; no dependency issues; type checks passed). Both commit-message validators and the repository source-language and large-file gates also passed. - The earlier description incorrectly called this an unrelated CI failure. It was introduced by this PR's standalone-serve test, and the CI build made that incomplete fixture fail. The fixture follow-up was validated on Linux; the native Windows and full-suite measurements below are earlier verification. CI must validate the pushed revision. Native Windows follow-up (build 10.0.26300): - Parallel real-upgrade cases now get separate `RAVEN_HOME` directories, isolating both upgrade markers and downloaded wheels. A scratch barrier forced both helpers to finish writing the same partial-wheel name before rename: the original fixture produced 1 failed / 1 passed with `Distribution not found` in the shared cache; the identical probe with isolated homes produced 2 passed on Python 3.13.12. The ordinary run had passed before the forced overlap, so this result identifies a race rather than a consistently failing upgrade. - Captured upgrade diagnostics tolerate localized Windows error bytes and include stderr on a missing success message. Version and companion-package assertions remain strict. - Three reported unit-test constraints were reproduced. Symlink setup now skips only Windows privilege error 1314; other errors propagate and confinement still runs when a link can be created. Owner-only mode bits and two SIGTERM-handler tests are explicitly POSIX-only. A native subprocess probe confirmed that Windows Popen.terminate does not execute the Python SIGTERM handler. - `PYTHONUTF8=1`; `uv run --frozen --no-sync pytest tests/test_rpc_transport.py tests/test_cli_serve_commands.py tests/test_docker_runtime.py::test_container_bootstraps_the_browser_after_the_engine_is_ready -n 0 -x --tb=short`: 195 passed, 30 skipped on Python 3.13.12. Twenty-six skips were already POSIX-specific; four now identify the reported mode/signal/symlink preconditions. The skipped checks are not Windows equivalents. - `PYTHONUTF8=1`, `UV_PYTHON=3.12`, and an isolated caller `RAVEN_HOME`; `uv run --no-project --python 3.12 --with 'pytest>=8,<10' --with pytest-asyncio --with pytest-xdist pytest --noconftest tests/integration/test_cli_upgrade_real_uv.py -q --tb=short -n 4`: 3 passed, covering two real uv upgrade variants and the workflow contract. - Whole-PR pre-commit, full Python-target Ruff check/format, all ten import contracts, changed-production type checks, commitlint and repository commit/asset/source-language gates passed. Earlier author verification before this Windows follow-up: - `uv run --frozen --python 3.12 --all-extras pytest -q tests/test_rpc_transport.py tests/test_cli_serve_commands.py tests/test_docker_runtime.py` -> `233 passed`. - The same three files plus every `tests/test_rpc_*.py` and `tests/test_cli_smoke.py` -> `3121 passed` on the first revision, before the fixture follow-up. - The earlier full suite, under `--cov=raven --cov-branch` -> `8 failed, 29791 passed, 158 skipped`, then `scripts/coverage_gate.py diff --base-ref origin/main --threshold 90` -> `100.00% (32/32 executable changed lines)`, passed. All eight failures are local baselines: five proxy tests (`tests/test_config_update_providers.py`) that fail whenever the shell carries this box's proxy; `test_install_script.py::test_resolve_node_dir_answers_each_case_it_exists_for` (node/npm resolution on this box); `test_subagent_node_runtime.py::test_what_cannot_be_read_names_nothing` (fails as root); and `test_knowledge_parser_excel.py::test_a_legacy_workbook_is_converted_and_read`. The last three were replayed in an unmodified baseline worktree and fail there too (`3 failed`). - The tests were written before the code and watched fail on it, each for its own reason: the forwarded origin as `403: bad origin`, the port-forward cookie case as `two sign-ins wrote one jar entry: ['raven_session_18792']`, the launcher cases on empty output. Against `main`'s two source files the three test files now fail 18 tests. - Eleven mutants, one construct each, all killed, with both files restored and compared byte for byte: the forwarded rule off, Origin equal to Host dropped, the loopback test dropped, the `localhost` branch removed, either parse left unguarded, the cookie keyed to the bound port, the two refusals given one text, the link not printed, the link printed after the browser, and `raven serve --open` bypassing `_open`. - `ruff check` and `ruff format --check` clean; `lint-imports` reports `10 kept, 0 broken`. - `scripts/check_source_language.py origin/main...HEAD` and `scripts/check_large_files.py origin/main...HEAD` both exit 0. - Real browser, end to end, against an isolated `raven serve` on its own `RAVEN_HOME` reached through a TCP forwarder: the tab signed in and landed on `/`, the cookie it kept was `raven_session_<forwarded port>`, and an RPC call over a socket opened from that page was answered (server log: `serve: ws client connected`, `system.hello`). A direct tab on the bound port signed in too. With `fetch` stubbed, the page rendered each of the four answers it can receive (refused address, used or expired link, other status, no answer) with the matching text. ## Risk - Security impact considered. The change widens one check: an `Origin` that equals the request's `Host` on a loopback name. The cross-origin local-page and DNS-rebinding cases remain refused, and both are covered by tests. What it accepts that the old rule did not is a page whose origin is exactly the address it asked for -- which only something serving that address can show, and a file the agent produced arrives with the CSP `sandbox` directive and so has an opaque origin. - Backward compatibility: a tab signed in directly on the bound port keeps its cookie name and stays signed in. A tab reaching the gateway through a forward had no working session before, so there is nothing to migrate. The container's auto-login cookie is unchanged, and a test derives the entrypoint's name from `cookie_name` so the two cannot drift. - Rollback: revert the commit. Nothing is written outside the process except the cookie already in the browser, and no config or format changed. - A `Host` that does not parse (`127.0.0.1:99999`, `127.0.0.1:abc`, `[::1`) is answered 401 on `/rpc`, as on `main`, and an Origin equal to such a Host is answered 403. Without the guard both would be a 500, since the cookie's name is read before any credential is checked. - Not fixed here: the page's own "this tab's sign-in expired" notice (main page, i18n `gui.auth.stale`) still names the remedy correctly but not the cause, so a tab that reaches the gateway by a non-loopback address still reports a sign-in failure it cannot act on. Also unmeasured: a real VS Code forward and a real container (both request shapes were reproduced instead). ## Related Issues N/A
211 lines
7.8 KiB
Bash
Executable File
211 lines
7.8 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
# Container entrypoint: nginx in front, the Raven engine behind it.
|
|
#
|
|
# docker compose up -> run (default)
|
|
# docker compose exec raven docker-entrypoint.sh signin
|
|
# -> print a fresh sign-in URL
|
|
# docker compose run --rm raven provider set anthropic --api-key sk-...
|
|
# -> any raven CLI command
|
|
#
|
|
# `run` and `signin` are dispatch keywords; anything else is handed to the
|
|
# `raven` CLI verbatim.
|
|
|
|
set -eu
|
|
|
|
# Defaults mirror the image's ENV. Repeated here so `set -u` cannot turn a
|
|
# variable someone dropped from .env into an unreadable failure.
|
|
PAGE_PORT="${RAVEN_PAGE_PORT:-18792}"
|
|
WEB_PORT="${RAVEN_WEB_PORT:-${PAGE_PORT}}"
|
|
AUTO_LOGIN="${RAVEN_AUTO_LOGIN:-1}"
|
|
RAVEN_HOME="${RAVEN_HOME:-/data/.raven}"
|
|
RAVEN_DIST_ROOT="${RAVEN_DIST_ROOT:-/app/ui-web/dist}"
|
|
RAVEN_PAGE_PORT="${PAGE_PORT}"
|
|
export RAVEN_HOME RAVEN_DIST_ROOT RAVEN_PAGE_PORT
|
|
BASE="http://127.0.0.1:${PAGE_PORT}"
|
|
|
|
log() { printf 'raven-docker: %s\n' "$*"; }
|
|
|
|
# The mark, on the container's first line of output. Quoted heredoc so the
|
|
# shell reads the art as bytes -- nothing in it is a shell metacharacter today,
|
|
# and this keeps that true if the drawing is ever redrawn. Lines carry no
|
|
# trailing spaces: the repo strips those on commit, and they render the same.
|
|
banner() {
|
|
cat <<'RAVEN_MARK'
|
|
░█████████
|
|
░██ ░██
|
|
░██ ░██ ░██████ ░██ ░██ ░███████ ░████████
|
|
░█████████ ░██ ░██ ░██ ░██ ░██ ░██ ░██
|
|
░██ ░██ ░███████ ░██ ░██ ░█████████ ░██ ░██
|
|
░██ ░██ ░██ ░██ ░██░██ ░██ ░██ ░██
|
|
░██ ░██ ░█████░██ ░███ ░███████ ░██ ░██
|
|
RAVEN_MARK
|
|
printf '\n'
|
|
}
|
|
|
|
# The engine writes {port, token, pid} here once it is listening; the token is
|
|
# what mints a sign-in nonce. Generated per boot rather than shipped in .env,
|
|
# so no deployment shares a secret with the repository.
|
|
serve_state() {
|
|
python - "$1" <<'PY'
|
|
import json, os, sys
|
|
path = os.path.join(os.environ.get("RAVEN_HOME", ""), "serve.json")
|
|
try:
|
|
with open(path, encoding="utf-8") as fh:
|
|
print(json.load(fh).get(sys.argv[1], ""))
|
|
except (OSError, ValueError):
|
|
print("")
|
|
PY
|
|
}
|
|
|
|
# 0 listening, 1 timed out, 2 the engine is gone.
|
|
wait_for_engine() {
|
|
# 120 s: a first boot builds the agent loop, the plugin stack and the
|
|
# memory store before it listens.
|
|
i=0
|
|
while [ "$i" -lt 240 ]; do
|
|
# Asked before the probe. A child that died on its first line answers
|
|
# nothing for the whole two minutes otherwise, and the container spends
|
|
# them looking like a slow start instead of a failed one -- `top` shows
|
|
# nginx, this shell and a `sleep`, and no engine.
|
|
if ! kill -0 "${engine}" 2>/dev/null; then
|
|
return 2
|
|
fi
|
|
if curl -fsS -m 2 "${BASE}/health" >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
i=$((i + 1))
|
|
sleep 0.5
|
|
done
|
|
return 1
|
|
}
|
|
|
|
sign_in_url() {
|
|
token="$(serve_state token)"
|
|
if [ -z "$token" ]; then
|
|
log "no serve.json yet -- is the engine running?"
|
|
return 1
|
|
fi
|
|
nonce="$(curl -fsS -m 5 -X POST -H "X-Raven-Token: ${token}" "${BASE}/auth/nonce" 2>/dev/null \
|
|
| python -c 'import json,sys; print(json.load(sys.stdin).get("nonce",""))' 2>/dev/null || true)"
|
|
if [ -z "$nonce" ]; then
|
|
log "could not mint a sign-in nonce"
|
|
return 1
|
|
fi
|
|
printf 'http://localhost:%s/auth#%s\n' "${WEB_PORT}" "${nonce}"
|
|
}
|
|
|
|
announce() {
|
|
url="$(sign_in_url || true)"
|
|
printf '\n'
|
|
log "the page is on http://localhost:${WEB_PORT}"
|
|
if [ -n "${url:-}" ]; then
|
|
log "sign in once with this one-time link (replace localhost if you are remote):"
|
|
printf '\n %s\n\n' "$url"
|
|
log "for another link: docker compose exec raven docker-entrypoint.sh signin"
|
|
fi
|
|
}
|
|
|
|
# A provider named in the environment is written into config on every boot:
|
|
# the container is declarative, so .env is the source of truth whenever it says
|
|
# anything. Leave RAVEN_PROVIDER empty and the browser wizard owns the config.
|
|
seed_provider() {
|
|
if [ -z "${RAVEN_PROVIDER:-}" ] \
|
|
|| { [ -z "${RAVEN_API_KEY:-}" ] && [ -z "${RAVEN_API_BASE:-}" ]; }; then
|
|
return 0
|
|
fi
|
|
log "configuring provider '${RAVEN_PROVIDER}' from the environment"
|
|
set -- provider set "${RAVEN_PROVIDER}"
|
|
if [ -n "${RAVEN_API_KEY:-}" ]; then
|
|
set -- "$@" --api-key "${RAVEN_API_KEY}"
|
|
fi
|
|
if [ -n "${RAVEN_API_BASE:-}" ]; then
|
|
set -- "$@" --api-base "${RAVEN_API_BASE}"
|
|
fi
|
|
raven "$@"
|
|
}
|
|
|
|
start_nginx() {
|
|
RAVEN_AUTO_LOGIN_COOKIE=""
|
|
if [ "${AUTO_LOGIN}" = "1" ] || [ "${AUTO_LOGIN}" = "true" ]; then
|
|
cookie="$(serve_state cookie)"
|
|
if [ -z "${cookie}" ]; then
|
|
log "the engine did not publish its browser session cookie"
|
|
return 1
|
|
fi
|
|
# The name has to be the one the engine reads, and it derives that from the
|
|
# request's Host port -- which nginx forwards without one, so the engine keeps
|
|
# its own PAGE_PORT. Spelled here as that same port; test_docker_runtime.py
|
|
# holds the two sides together.
|
|
RAVEN_AUTO_LOGIN_COOKIE="raven_session_${PAGE_PORT}=${cookie}; Path=/; Max-Age=2592000; HttpOnly; SameSite=Strict"
|
|
fi
|
|
export RAVEN_AUTO_LOGIN_COOKIE
|
|
mkdir -p /etc/nginx/conf.d
|
|
# Only these values: nginx's own $variables must survive substitution.
|
|
envsubst '${RAVEN_DIST_ROOT} ${RAVEN_PAGE_PORT} ${RAVEN_AUTO_LOGIN_COOKIE}' \
|
|
< /etc/nginx/templates/raven.conf.template \
|
|
> /etc/nginx/conf.d/raven.conf
|
|
nginx -t
|
|
nginx
|
|
log "nginx serving ${RAVEN_DIST_ROOT} on :80, proxying ${BASE}"
|
|
}
|
|
|
|
run() {
|
|
# Printed here rather than at the top of the file: `signin` and the CLI
|
|
# passthrough are read by people and by scripts, and a banner ahead of a
|
|
# sign-in URL or a `raven status` is output nobody asked for.
|
|
banner
|
|
|
|
# The engine must land on exactly this port or nginx proxies into nothing;
|
|
# without strict mode it probes forward when the port looks taken.
|
|
RAVEN_SERVE_PORT_STRICT=1
|
|
export RAVEN_SERVE_PORT_STRICT
|
|
mkdir -p "${RAVEN_HOME}"
|
|
|
|
seed_provider
|
|
|
|
# Starts with no provider configured and warns; the page's Settings >
|
|
# Models is how one gets added, and the next turn picks it up.
|
|
raven gateway --page-port "${PAGE_PORT}" &
|
|
engine=$!
|
|
trap 'log "stopping"; kill -TERM "${engine}" 2>/dev/null || true' TERM INT
|
|
|
|
boot=0
|
|
wait_for_engine || boot=$?
|
|
if [ "$boot" -eq 0 ]; then
|
|
start_nginx
|
|
announce
|
|
elif [ "$boot" -eq 2 ]; then
|
|
log "the engine exited during start-up -- its error is above this line"
|
|
else
|
|
log "the engine did not answer ${BASE}/health within 120s; see the log above"
|
|
fi
|
|
|
|
# `wait` returns the moment a trap fires, with the child still shutting
|
|
# down -- a turn in flight and the memory store both want to finish. So the
|
|
# status is taken from the first wait and the exit is held until the process
|
|
# is actually gone.
|
|
status=0
|
|
wait "${engine}" || status=$?
|
|
while kill -0 "${engine}" 2>/dev/null; do
|
|
sleep 0.2
|
|
done
|
|
|
|
# Last, so the page keeps answering while the engine winds down, and the
|
|
# container never lingers serving a socket that is no longer there.
|
|
nginx -s quit 2>/dev/null || true
|
|
return "${status}"
|
|
}
|
|
|
|
case "${1:-run}" in
|
|
run)
|
|
shift 2>/dev/null || true
|
|
run
|
|
;;
|
|
signin)
|
|
sign_in_url
|
|
;;
|
|
*)
|
|
exec raven "$@"
|
|
;;
|
|
esac
|