Skip to content

Universal SEO skill for Claude Code. 26 sub-skills + 19 sub-agents covering technical SEO, E-E-A-T, schema, GEO/AEO, agent readiness (Lighthouse Agentic Browsing, WebMCP, llms.txt), backlinks, local SEO, e-commerce, international SEO, Google APIs, and PDF/Excel reporting. 9 optional extensions for live SEO data.

fix(google_auth): honest manual window, clean state check, no blind exchange

Agrici Daniel98a9c6761cd5
diff --git a/scripts/google_auth.py b/scripts/google_auth.py
index fbe3ef2..f414b04 100644
--- a/scripts/google_auth.py
+++ b/scripts/google_auth.py
@@ -370,6 +370,15 @@ def _load_pending_flow() -> Optional[dict]:
return data
+def _states_match(returned: str, expected: str) -> bool:
+ """Constant-time state check that also handles non-ASCII input.
+
+ hmac.compare_digest refuses str values with non-ASCII characters, so a
+ crafted ``state`` would raise instead of being rejected. Compare bytes.
+ """
+ return hmac.compare_digest(returned.encode("utf-8"), expected.encode("utf-8"))
+
+
def _manual_exchange_inputs(code_or_url: str) -> tuple[Optional[str], Optional[str], Optional[str]]:
"""Turn --exchange --code input into (code, code_verifier, error).
@@ -379,7 +388,11 @@ def _manual_exchange_inputs(code_or_url: str) -> tuple[Optional[str], Optional[s
import urllib.parse
pending = _load_pending_flow()
- verifier = pending["code_verifier"] if pending else None
+ if not pending:
+ # Every sign-in sends a PKCE challenge, so Google refuses a code
+ # exchanged without its verifier. Say so instead of sending it.
+ return None, None, "No pending sign-in (missing or expired). Run --auth again."
+ verifier = pending["code_verifier"]
value = code_or_url.strip()
if not value.lower().startswith(("http://", "https://")):
return value, verifier, None
@@ -389,10 +402,9 @@ def _manual_exchange_inputs(code_or_url: str) -> tuple[Optional[str], Optional[s
code = params.get("code", [None])[0]
if not code:
return None, None, "The pasted URL has no 'code' parameter."
- if pending:
- returned_state = params.get("state", [""])[0]
- if not hmac.compare_digest(returned_state, pending["state"]):
- return None, None, "The pasted URL does not belong to the pending sign-in. Run --auth again."
+ returned_state = params.get("state", [""])[0]
+ if not _states_match(returned_state, pending["state"]):
+ return None, None, "The pasted URL does not belong to the pending sign-in. Run --auth again."
return code, verifier, None
@@ -568,7 +580,7 @@ def run_oauth_flow(creds_path: str):
def do_GET(self):
params = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query)
returned_state = params.get("state", [""])[0]
- if not hmac.compare_digest(returned_state, state):
+ if not _states_match(returned_state, state):
# Not our redirect (a stray request or a forged one): refuse
# it and keep waiting for the real one.
self.send_response(400)
@@ -621,6 +633,9 @@ def run_oauth_flow(creds_path: str):
sys.exit(1)
if not result["code"]:
+ # Restart the pending flow's clock so the manual window below is
+ # measured from now, not from before the wait.
+ _save_pending_flow(state, code_verifier)
print("\nAuthentication failed or timed out.", file=sys.stderr)
print("If the browser showed 'localhost refused to connect', copy the full URL")
print(f"from the browser address bar within {OAUTH_PENDING_MAX_AGE // 60} minutes and run:")
diff --git a/tests/test_google_oauth_flow.py b/tests/test_google_oauth_flow.py
index 5528012..7b02e1b 100644
--- a/tests/test_google_oauth_flow.py
+++ b/tests/test_google_oauth_flow.py
@@ -8,6 +8,7 @@ redirect server on a free port and never touch Google.
from __future__ import annotations
import base64
+import errno
import hashlib
import json
import os
@@ -95,13 +96,21 @@ def _run_flow_with_browser(isolated, monkeypatch, browser):
return True
monkeypatch.setattr("webbrowser.open", open_browser)
- try:
- google_auth.run_oauth_flow(str(isolated))
- finally:
- # The flow returns once the server has answered; let the "browser"
- # finish reading that answer before the test inspects it.
- if "thread" in opened:
- opened["thread"].join(timeout=30)
+ # _free_port() releases the port before the flow binds it, so another
+ # process can take it in between. Retry on a fresh port when that happens.
+ for attempt in range(5):
+ try:
+ google_auth.run_oauth_flow(str(isolated))
+ break
+ except OSError as e:
+ if e.errno not in (errno.EADDRINUSE, getattr(errno, "WSAEADDRINUSE", None)) or attempt == 4:
+ raise
+ monkeypatch.setattr(google_auth, "OAUTH_REDIRECT_URI", f"http://localhost:{_free_port()}")
+ finally:
+ # The flow returns once the server has answered; let the "browser"
+ # finish reading that answer before the test inspects it.
+ if "thread" in opened:
+ opened["thread"].join(timeout=30)
return opened["url"], exchanged
@@ -145,11 +154,11 @@ def test_stray_and_forged_requests_do_not_end_the_wait(isolated, monkeypatch):
def test_a_silent_connection_does_not_block_the_real_redirect(isolated, monkeypatch):
monkeypatch.setattr(google_auth, "OAUTH_WAIT_SECONDS", 30)
monkeypatch.setattr(google_auth, "OAUTH_REQUEST_TIMEOUT", 1)
- port = urllib.parse.urlparse(google_auth.OAUTH_REDIRECT_URI).port
idle = []
def browser(url):
state = urllib.parse.parse_qs(urllib.parse.urlparse(url).query)["state"][0]
+ port = urllib.parse.urlparse(google_auth.OAUTH_REDIRECT_URI).port
idle.append(socket.create_connection(("localhost", port))) # connects, sends nothing
# The server drops the idle connection after OAUTH_REQUEST_TIMEOUT.
_get(_redirect({"state": state, "code": "real-code"}), timeout=20)
@@ -184,6 +193,55 @@ def test_timeout_keeps_the_pending_flow_for_manual_exchange(isolated, monkeypatc
assert os.stat(path).st_mode & 0o777 == 0o600
+def test_timeout_restarts_the_pending_clock(isolated, monkeypatch):
+ # The manual window printed on timeout must run from the timeout, not
+ # from before the wait: a person who pastes the URL late in that window
+ # still has a pending sign-in.
+ monkeypatch.setattr(google_auth, "OAUTH_WAIT_SECONDS", 1)
+ base = time.time()
+ clock = {"offset": 0.0}
+ monkeypatch.setattr(google_auth.time, "time", lambda: base + clock["offset"])
+
+ def browser(url):
+ clock["offset"] = 400.0 # the person took a long time in the browser
+
+ with pytest.raises(SystemExit):
+ _run_flow_with_browser(isolated, monkeypatch, browser)
+ clock["offset"] = 400.0 + google_auth.OAUTH_PENDING_MAX_AGE - 10
+ assert google_auth._load_pending_flow() is not None
+
+
+def test_non_ascii_state_is_rejected_not_raised(isolated, monkeypatch):
+ statuses = []
+
+ def browser(url):
+ state = urllib.parse.parse_qs(urllib.parse.urlparse(url).query)["state"][0]
+ statuses.append(_get(f"{google_auth.OAUTH_REDIRECT_URI}/?state=%C3%A9&code=x"))
+ statuses.append(_get(_redirect({"state": state, "code": "real-code"})))
+
+ _url, exchanged = _run_flow_with_browser(isolated, monkeypatch, browser)
+ assert statuses == [400, 200]
+ assert exchanged["code"] == "real-code"
+
+ google_auth._save_pending_flow("good-state", "v" * 64)
+ code, _verifier, error = google_auth._manual_exchange_inputs(
+ f"{google_auth.OAUTH_REDIRECT_URI}/?state=%C3%A9&code=x"
+ )
+ assert code is None and "pending sign-in" in error
+
+
+def test_exchange_without_a_pending_flow_fails_before_any_request(isolated, monkeypatch, capsys):
+ calls: list = []
+ monkeypatch.setattr(urllib.request, "urlopen", _fake_urlopen(calls))
+ monkeypatch.setattr(sys, "argv", ["google_auth.py", "--exchange", "--creds", str(isolated), "--code", "bare-code"])
+ with pytest.raises(SystemExit) as exc:
+ google_auth.main()
+ assert exc.value.code == 1
+ assert "No pending sign-in" in capsys.readouterr().err
+ assert calls == []
+ assert not os.path.exists(google_auth.TOKEN_PATH)
+
+
def test_manual_exchange_checks_state_of_a_pasted_url(isolated):
google_auth._save_pending_flow("good-state", "v" * 64)
code, verifier, error = google_auth._manual_exchange_inputs(_redirect({"state": "good-state", "code": "c1"}))