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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

All notable changes to this monorepo.

## backends v3.1.0 - 2026-11-15 (`bug-report-py` 3.1.0)

The proxy's core is a plain function. No contract change.

- **`bug_report.proxy.forward(method, rest, query, body, browser_headers, *, central_url, app, token_provider, person_provider, timeout=30)`**
returns `(status, body_bytes, headers_dict)` and uses no web framework, so an app on the standard-library `http.server`
(the daily report) can proxy to the central store. It carries every rule `create_proxy_blueprint` had: `..` and `\` are 400;
a list defaults to `app=<app>`; only `Content-Type` and `Accept` come from the browser; the bearer token and `X-Leap-*` headers
are added; a marked trust refusal and a short body are 502; `http.client` with no `Connection: close`; generic 502 bodies; only
the contract's response headers come back.
- `forward()` takes `rest` raw or decoded: it decodes once before refusing `..` and `\`, so `%2e%2e` and `%5c` are 400 whatever the caller passes, and re-quotes each outgoing segment.
- `create_proxy_blueprint` is now a thin Flask wrapper over `forward` (Flask is imported inside it). Its behaviour and tests are unchanged.

## backends v3.0.2 — 2026-11-15 (`bug-report-py` 3.0.2)

One fix to `create_proxy_blueprint`, found on the seal viewer's first live day. No contract change.
Expand Down
1 change: 1 addition & 0 deletions docs/CENTRAL-STORE.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ LeapHQ Review ──(LeapHQ's own identity, a token)──► leaphq-reports (on
- It is the only place reports are stored, emails are sent and triage rights are checked: one `BUG_ADMINS`, and one sending identity (`leaphq-bug-notify`).
- **The apps keep their URL.** Each app's `/api/bugs` becomes `create_proxy_blueprint(central_url, app="placements")` from the library.
- It forwards every route (POST, list, detail, PATCH and screenshot) with its body unchanged.
- An app that is not Flask (the daily report's `http.server`) calls the framework-free core, `bug_report.proxy.forward()`, which the blueprint wraps.
- The widget, BugQueue and every page that links to a report keep working, with no front-end change.
- The app stamps `app`; the caller cannot set it.
- **Who is asking.** Every request carries a token: the calling app's **own managed identity** (an Entra token). There is no person path on the service, so a request with no token is 401 and the service never reads `X-Ms-Client-Principal` or Tailscale headers.
Expand Down
20 changes: 20 additions & 0 deletions packages/backend-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ and 401 (sign in to vote) pass through. A list with no `app` query is asked for
passes through. A `..` path segment is refused (400). A service that cannot be reached is a 502, and a token or identity that cannot be
produced is a 502 with nothing sent; the 502 bodies are generic (the reason goes to the log, not the browser). Standard library only (`http.client`; it never sends `Connection: close`, which Azure Container Apps' internal ingress answers by cutting large responses short).

### Without Flask: `bug_report.proxy.forward` (3.1.0)

An app that is not Flask (a standard-library `http.server`, say) calls the proxy's core directly. The blueprint above is a
thin wrapper over it, so every rule is the same:

```python
from bug_report.proxy import forward

status, body, headers = forward(
method, rest, query, body, browser_headers, # rest: "" | "<id>" | "<id>/vote" | "<id>/screenshot"; query: no "?"
central_url="http://leaphq-reports/api/bugs", app="report",
token_provider=lambda: credential.get_token(SCOPE).token,
person_provider=lambda: {"email": "ann@example.com", "name": "Ann"},
)
# write `status`, `headers` and `body` back to the browser
```

It returns `(status, body_bytes, headers_dict)` and never raises for a service or network problem (those are 502s). The
package still depends on Flask and `import bug_report` imports it; `forward()` itself uses no Flask. `rest` may be raw from the request line (`a%20b`, `%2e%2e`) or already decoded; it is decoded once and `..`, `.` and `\` segments are refused (400, nothing sent) either way.

## The service side: who is calling (3.0.0)

```python
Expand Down
4 changes: 2 additions & 2 deletions packages/backend-python/bug_report/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from .blueprint import create_blueprint
from .importer import ImportMismatch, import_reports
from .proxy import create_proxy_blueprint
from .proxy import create_proxy_blueprint, forward
from .trust import Caller, TrustedCallers, trusted_caller
from . import identity
from .email import AcsNotifier, Notice, render
Expand All @@ -44,7 +44,7 @@
valid_bug_id,
)

__version__ = "3.0.1"
__version__ = "3.1.0"

from . import contract
from .contract import (
Expand Down
178 changes: 111 additions & 67 deletions packages/backend-python/bug_report/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
person_provider=person_provider(trust_easyauth=True),
), url_prefix="/api")

The core is ``forward()`` (3.1.0), a plain function with no web framework in it, for an app that
is not Flask - the daily report's standard-library ``http.server`` calls it directly. The blueprint
below is a thin Flask wrapper over it, so the rules live once.

Every route (POST, list, detail, PATCH, vote, screenshot) is forwarded with
its method, query, body and content type, and the service's status code, body
and content type come back unchanged. Each forwarded request carries
Expand All @@ -33,10 +37,8 @@
import http.client
import json
import logging
from typing import Callable
from urllib.parse import quote, urlsplit

from flask import Blueprint, Response, request
from typing import Callable, Mapping
from urllib.parse import quote, unquote, urlsplit

from .trust import TRUST_REFUSAL_HEADER

Expand All @@ -47,6 +49,99 @@
_FORWARD = ("Content-Type", "Accept")
#: response headers passed back: the contract's, plus the redirect target
_BACK = ("Content-Type", "Location", "Cache-Control", "X-Leap-App-Used", "X-Leap-App-Ignored")
#: methods that carry a body to the service
_WITH_BODY = ("POST", "PATCH", "DELETE")


def _target(central_url: str):
base = central_url.rstrip("/")
target = urlsplit(base)
if target.scheme not in ("http", "https") or not target.hostname:
raise ValueError(f"central_url must be an http(s) URL: {central_url!r}")
return target


def _ascii(v: str) -> str:
return quote(v or "", safe=" @.+-_'")


def _err(status: int, message: str) -> tuple[int, bytes, dict]:
return status, json.dumps({"error": message}).encode(), {"Content-Type": "application/json"}


def forward(
method: str,
rest: str,
query: str,
body: bytes | None,
browser_headers: Mapping[str, str],
*,
central_url: str,
app: str,
token_provider: Callable[[], str],
person_provider: Callable[[], dict | None],
timeout: float = 30,
) -> tuple[int, bytes, dict]:
"""Forward one ``/bugs`` request to the central service; return ``(status, body, headers)``.

``rest`` is the path after ``/bugs`` ("" for the collection, "<id>", "<id>/vote", ...), in either form:
raw from the request line (``a%20b``, ``%2e%2e``) or already decoded (Flask's). It is percent-decoded once,
checked for ``..``, ``.`` and ``\\`` segments (400, nothing sent), and each segment re-quoted going out. ``query``
the raw query string without the "?", ``body`` the request body (sent for POST, PATCH and DELETE
only), ``browser_headers`` the browser's request headers (only Content-Type and Accept are taken,
any case). Never raises for a service or network problem: those are 502s.
"""
target = _target(central_url)
method = method.upper()
# `rest` may be percent-encoded or already decoded (Flask hands over a decoded one): it is decoded here, once,
# BEFORE the dot-dot and backslash refusal, so `%2e%2e` and `%5c` are refused whichever way the caller got
# it, and each segment is re-quoted for the outgoing path.
segments = unquote(rest).split("/") if rest else []
if any(seg in ("..", ".") or "\\" in seg for seg in segments):
return _err(400, "invalid path")
path = target.path.rstrip("/") + "".join("/" + quote(seg, safe="") for seg in segments)
# The list is this app's own reports unless the caller says otherwise (3.0.1): the open count and
# the queue must not show every app's, which is what the service gives a trusted app with no filter.
if method == "GET" and not rest and "app" not in {p.split("=", 1)[0] for p in query.split("&") if p}:
query = (query + "&" if query else "") + "app=" + quote(app, safe="")
if query:
path += "?" + query
try:
person = person_provider() or {}
lowered = {k.lower(): v for k, v in browser_headers.items()}
headers = {h: lowered[h.lower()] for h in _FORWARD if h.lower() in lowered}
headers.update({
"Authorization": f"Bearer {token_provider()}",
"X-Leap-Actor-Email": _ascii(person.get("email", "")),
"X-Leap-Actor-Name": _ascii(person.get("name", "")),
"X-Leap-App": app,
})
except Exception as e: # noqa: BLE001 - no token / identity: say so, do not guess
log.warning("reports proxy (%s): could not get a token or identity: %s", app, e)
return _err(502, "could not authenticate to the reports service")
data = body if method in _WITH_BODY else None
connection = http.client.HTTPSConnection if target.scheme == "https" else http.client.HTTPConnection
conn = connection(target.hostname, target.port, timeout=timeout)
try:
# Any status - 3xx (a signed screenshot URL goes back as it is), 4xx, 5xx - is the service's answer.
conn.request(method, path, body=data, headers=headers)
r = conn.getresponse()
status, payload, upstream = r.status, r.read(), r.headers
except http.client.IncompleteRead as e:
log.warning("reports proxy (%s): %s came back short: %d bytes, %s more expected",
app, path, len(e.partial), e.expected)
return _err(502, "the reports service's answer came back incomplete")
except (http.client.HTTPException, OSError, TimeoutError) as e:
log.warning("reports proxy (%s): %s unreachable: %s", app, path, e)
return _err(502, "the reports service is not reachable")
finally:
conn.close()
if status in (401, 403) and upstream.get(TRUST_REFUSAL_HEADER):
# The service refused THIS APP (a setup mistake), not the person. A 401 here would read, to a
# client that treats 401 as "your sign-in failed" (a seal laptop's outbox), as the person's.
log.warning("reports proxy (%s): the service refused this app: %s %s", app, status, payload[:300])
return _err(502, "the reports service refused this app")
return status, payload, {h: upstream[h] for h in _BACK if upstream.get(h) is not None}


def create_proxy_blueprint(
Expand All @@ -57,73 +152,22 @@ def create_proxy_blueprint(
*,
url_prefix: str = "/bugs",
timeout: float = 30,
) -> Blueprint:
bp = Blueprint(f"bug_report_proxy_{app}", __name__)
base = central_url.rstrip("/")
target = urlsplit(base)
if target.scheme not in ("http", "https") or not target.hostname:
raise ValueError(f"central_url must be an http(s) URL: {central_url!r}")
connection = http.client.HTTPSConnection if target.scheme == "https" else http.client.HTTPConnection
):
from flask import Blueprint, Response, request

def _ascii(v: str) -> str:
return quote(v or "", safe=" @.+-_'")
_target(central_url) # a bad URL fails at start-up, not on the first report
bp = Blueprint(f"bug_report_proxy_{app}", __name__)

@bp.route(url_prefix, defaults={"rest": ""}, methods=["GET", "POST", "PATCH", "DELETE"])
@bp.route(f"{url_prefix}/<path:rest>", methods=["GET", "POST", "PATCH", "DELETE"])
def forward(rest: str):
if any(seg in ("..", ".") or "\\" in seg for seg in rest.split("/")):
return _err(400, "invalid path")
path = target.path + ("/" + rest if rest else "")
query = request.query_string.decode("latin-1")
# The list is this app's own reports unless the caller says otherwise (3.0.1): the open count and
# the queue must not show every app's, which is what the service gives a trusted app with no filter.
if request.method == "GET" and not rest and "app" not in request.args:
query = (query + "&" if query else "") + "app=" + quote(app, safe="")
if query:
path += "?" + query
try:
person = person_provider() or {}
headers = {h: request.headers[h] for h in _FORWARD if h in request.headers}
headers.update({
"Authorization": f"Bearer {token_provider()}",
"X-Leap-Actor-Email": _ascii(person.get("email", "")),
"X-Leap-Actor-Name": _ascii(person.get("name", "")),
"X-Leap-App": app,
})
except Exception as e: # noqa: BLE001 — no token / identity: say so, do not guess
log.warning("reports proxy (%s): could not get a token or identity: %s", app, e)
return _err(502, "could not authenticate to the reports service")
data = request.get_data() if request.method in ("POST", "PATCH", "DELETE") else None
conn = connection(target.hostname, target.port, timeout=timeout)
try:
# Any status - 3xx (a signed screenshot URL goes back as it is), 4xx, 5xx - is the service's answer.
conn.request(request.method, path, body=data, headers=headers)
r = conn.getresponse()
return _relay(r.status, r.read(), r.headers)
except http.client.IncompleteRead as e:
log.warning("reports proxy (%s): %s came back short: %d bytes, %s more expected",
app, path, len(e.partial), e.expected)
return _err(502, "the reports service's answer came back incomplete")
except (http.client.HTTPException, OSError, TimeoutError) as e:
log.warning("reports proxy (%s): %s unreachable: %s", app, path, e)
return _err(502, "the reports service is not reachable")
finally:
conn.close()

def _relay(status, body, upstream_headers):
if status in (401, 403) and upstream_headers.get(TRUST_REFUSAL_HEADER):
# The service refused THIS APP (a setup mistake), not the person. A 401 here would read, to a
# client that treats 401 as "your sign-in failed" (a seal laptop's outbox), as the person's.
log.warning("reports proxy (%s): the service refused this app: %s %s", app, status, body[:300])
return _err(502, "the reports service refused this app")
resp = Response(body, status=status)
for h in _BACK:
v = upstream_headers.get(h)
if v is not None:
resp.headers[h] = v
def _forward(rest: str):
status, payload, headers = forward(
request.method, rest, request.query_string.decode("latin-1"), request.get_data(),
request.headers, central_url=central_url, app=app, token_provider=token_provider,
person_provider=person_provider, timeout=timeout)
resp = Response(payload, status=status)
for k, v in headers.items():
resp.headers[k] = v
return resp

def _err(status, message):
return Response(json.dumps({"error": message}), status=status, mimetype="application/json")

return bp
2 changes: 1 addition & 1 deletion packages/backend-python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "bug-report-py"
version = "3.0.2"
version = "3.1.0"
description = "Flask blueprint + storage interface implementing the bug-report-widget API spec."
readme = "README.md"
license = { text = "MIT" }
Expand Down
Loading
Loading