Repository navigation
feat: add public URLs and typed custom config - #465
Merged
Merged
Conversation
Add the optional setting `server.public_url`, the URL of the app as clients see it behind a reverse proxy or ingress. It is checked and normalized with `foca.utils.urls` and must not have a query or fragment. Add `foca.utils.urls.get_public_url()`, which builds URLs of paths of the app from it, the root path of the app and the path, percent- encoded. It raises a `RuntimeError` if `public_url` is not set, rather than trusting the `Host` and `X-Forwarded-*` headers of requests, and a `ValueError` for paths with dot segments. Add `foca.get_custom_config()`, which returns the custom config of the app typed as the given model, or raises a `RuntimeError`. Moved from cloud-registry (`cloud_registry/utils.py`), generalized. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Describe `server.public_url` (with a proxy example), `get_public_url()` and `get_custom_config()`, add the option to `templates/config.yaml`, and mention both in the notes for 0.18. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Raise `InvalidURL` (400 Bad Request) instead of a plain `ValueError` for dot segments, as paths may come from clients. Import Flask and the config only in `get_public_url()`, so that URL checks stay independent of them. Document how percent-encodings and slashes in paths are kept, and tidy tests and docs after review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Raise `InvalidURL` (400) for paths with unpaired surrogates, which `quote()` cannot encode, rather than a `UnicodeEncodeError` (500). Import Flask at module level again; only `get_config()` is imported lazily, because of the import cycle with the config models. Test that `server.public_url` resolves dot segments and repeated slashes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideIntroduces two opt-in FOCA helpers: normalized reverse-proxy-aware public URL generation with defensive path validation, and typed access to custom application configuration, with exports, documentation, templates, and extensive tests. Sequence diagram for building a validated public URLsequenceDiagram
participant App as FOCA app
participant Config as ServerConfig
participant Request as Flask request
participant URLs as get_public_url
participant Client as Client
App->>Config: _normalize_public_url(public_url)
Config-->>App: normalized server.public_url
Client->>URLs: get_public_url(path)
URLs->>Config: get_config()
URLs->>Request: read script_root
URLs->>URLs: quote(root) and quote(path)
URLs-->>Client: public URL
alt public_url is not configured
URLs-->>Client: RuntimeError
else path has dot segments or unpaired surrogates
URLs-->>Client: InvalidURL
end
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #465 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 29 29
Lines 3342 3378 +36
=========================================
+ Hits 3342 3378 +36
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
uniqueg
marked this pull request as ready for review
October 24, 2026 13:02
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Moves the public URL and typed custom config helpers of cloud-registry (
cloud_registry/utils.py) into FOCA, generically:server.public_url(new, optional, defaultNone): absolutehttp/httpsURL of the app as clients see it, including any path prefix of a reverse proxy. Checked and normalized withfoca.utils.urls.normalize_url()(no user info; e.g., lowercase host, no default port or trailing slash, canonical bracketed IPv6 hosts with encoded zone IDs, so_format_host()is not needed). Queries and fragments (also empty ones) are rejected.foca.utils.urls.get_public_url(path=""): in a request context,public_urlfollowed by the app's root path (request.script_root) andpath, percent-encoded (the prefix ofpublic_urlas configured), without trailing slash unless it is the host's root, as in cloud-registry. RaisesRuntimeErrorifpublic_urlis not set: no fallback to theHostandX-Forwarded-*headers, which clients control unless a proxy sets them. Paths with dot segments (.,..) raiseInvalidURL(400), as clients would resolve them, e.g., to URLs outside of the app, and paths may come from clients (identifiers). Paths with unpaired surrogates, which cannot be encoded, also raiseInvalidURL. Percent-encodings inpathare kept literally (%2e%2ebecomes%252e%252e).foca.get_custom_config(model)(infoca.config.access, exported fromfoca): the custom config of the current app, typed, if it is an instance ofmodel; otherwiseRuntimeError, naming the expected and found types and hinting thatcustom_config_modelmust be the model's full module path.configuration/server.md(public URL section with a proxy example),configuration/custom.md(get_custom_config()),utilities.md(building public URLs), API reference,templates/config.yaml, and a short entry under "Other changes" inmigration-0.18.md(opt-in, no changes needed for existing apps).Backwards compatible: new optional setting and functions only.
Verification
Locally (Python 3.12, MongoDB 7.0 on port 12345), after merging
origin/dev:coverage run -m pytest: all passed (2255 with MongoDB after mergingorigin/dev; 2127 without MongoDB after the last commit), 1 skipped;coverage report: 100%mypy: no issues (88 files)ruff check,ruff format --check: passpre-commit run --all-files: all hooks pass (incl. semgrep, ast-grep and their rule tests)mkdocs build --strict: builds without warningsReview: adversarial-reviewer and quality-reviewer, 2 rounds, no high findings. Round 1 fixes (
InvalidURLinstead ofValueErrorfor dot segments, docstring and docs corrections, extra tests) are in the third commit; round 2 fixes (InvalidURLfor unpaired surrogates, onlyget_configimported lazily because of the import cycle, normalization tests) in the last one.CI: all checks pass (unit tests on Python 3.11 to 3.15 with MongoDB 7.0 and 8.0 and lowest dependencies, integration tests, chart tests on kind, image scans, CodeQL, Codecov patch and project, Read the Docs).
Declined review findings
a/b) cannot be expressed, aspathis a whole, not percent-encoded path, as in cloud-registry. Accepting a sequence of segments would change the API proposed in feat: add public URLs and typed custom config #460; documented instead (apps should not allow slashes in identifiers that are part of public URLs)./a//b) are kept, not collapsed or rejected, as they are part of the path as given; documented and tested.Open questions
None. The issue's open question (fallback to the request URL when
public_urlis unset) keeps the choice the issue names: no fallback,RuntimeError; a fallback can be added later.Follow-ups
custom.endpoints.servicewithserver.public_urland deleteget_custom_config(),get_public_url()and_format_host()fromcloud_registry/utils.py(separate issue there, as described in feat: add public URLs and typed custom config #460).server.public_urlfromhostName(its tests setcustom.public_urltoday).Closes #460
🤖 Generated with Claude Code
Summary by Sourcery
Add opt-in support for client-facing public URLs and typed custom configuration access.
New Features:
Enhancements:
Documentation:
Tests: