Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ walk the structure to find what the negative is about.
| `browser` | none | — | Playwright, **public-records hosts only** (allow-listed). |
| `extract` | none | — | Local dynamic filtering: fields, not pages. |

Set `COURTLISTENER_TOKEN` for the authenticated tier.
Set `COURTLISTENER_TOKEN` for the authenticated tier, or put the token in
`~/.config/courtlistener/token` (mode 600; `COURTLISTENER_TOKEN_FILE` overrides the
path). The variable wins when both are set. The file is the one to use for agents
and scheduled jobs, whose shells do not read your login profile.

## Browser escalation and humanomation

Expand Down
29 changes: 28 additions & 1 deletion evidence_search/sources/courtlistener.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,35 @@
SOURCE = "courtlistener"


def _token_file() -> str:
explicit = os.environ.get("COURTLISTENER_TOKEN_FILE")
if explicit:
return explicit
base = os.environ.get("XDG_CONFIG_HOME") or os.path.join(os.path.expanduser("~"), ".config")
return os.path.join(base, "courtlistener", "token")


def _token() -> str | None:
"""The API token: the environment first, then a file.

The file exists because the variable alone is not enough. A token exported
from a shell profile reaches login shells only; agent tool shells, cron and
cloud setup scripts start without it, and every call then ran on the
anonymous tier while the caller believed it was authenticated (2026-10-10:
401s on docket endpoints with a paid membership in place).
"""
tok = (os.environ.get("COURTLISTENER_TOKEN") or "").strip()
if tok:
return tok
try:
with open(_token_file(), encoding="utf-8") as f:
return f.read().strip() or None
except OSError:
return None


def _headers():
tok = os.environ.get("COURTLISTENER_TOKEN")
tok = _token()
return {"Authorization": f"Token {tok}"} if tok else {}


Expand Down
56 changes: 56 additions & 0 deletions tests/test_courtlistener_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Where the CourtListener token comes from.

Regression (2026-10-10): the token was exported from a login profile, so agent
tool shells never saw it and every call ran unauthenticated without saying so.
"""
import pytest

from evidence_search.sources import courtlistener as cl


@pytest.fixture
def clean_env(monkeypatch, tmp_path):
monkeypatch.delenv("COURTLISTENER_TOKEN", raising=False)
monkeypatch.delenv("COURTLISTENER_TOKEN_FILE", raising=False)
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
return tmp_path


def _write(tmp_path, text):
d = tmp_path / "courtlistener"
d.mkdir()
(d / "token").write_text(text)


def test_no_variable_and_no_file_sends_no_header(clean_env):
assert cl._headers() == {}


def test_file_is_used_when_the_variable_is_unset(clean_env):
_write(clean_env, "abc123\n")
assert cl._headers() == {"Authorization": "Token abc123"}


def test_variable_wins_over_the_file(clean_env, monkeypatch):
_write(clean_env, "from-file")
monkeypatch.setenv("COURTLISTENER_TOKEN", "from-env")
assert cl._headers() == {"Authorization": "Token from-env"}


def test_empty_variable_falls_through_to_the_file(clean_env, monkeypatch):
_write(clean_env, "from-file")
monkeypatch.setenv("COURTLISTENER_TOKEN", " ")
assert cl._headers() == {"Authorization": "Token from-file"}


def test_empty_file_sends_no_header(clean_env):
_write(clean_env, "\n")
assert cl._headers() == {}


def test_explicit_file_path_overrides_the_default(clean_env, monkeypatch, tmp_path):
other = tmp_path / "elsewhere"
other.write_text("explicit")
_write(clean_env, "default")
monkeypatch.setenv("COURTLISTENER_TOKEN_FILE", str(other))
assert cl._headers() == {"Authorization": "Token explicit"}
Loading