feat: auto-detect realm from request Host header#249
Merged
brendanjryan merged 7 commits intomainfrom Mar 27, 2026
Merged
Conversation
commit: |
88f6e2d to
e498d70
Compare
tmm
approved these changes
Mar 27, 2026
src/server/Mppx.ts
Outdated
Comment on lines
+161
to
+165
| if (!realm) | ||
| warnOnce( | ||
| Warnings.noRealm, | ||
| 'No realm configured. Will auto-detect from request Host header. Set `realm` in Mppx.create() or MPP_REALM env var for explicit control.', | ||
| ) |
Member
There was a problem hiding this comment.
Should we only show warning message if auto-detection fails?
Member
There was a problem hiding this comment.
Feel like this message will be noisy for most people otherwise
Collaborator
Author
There was a problem hiding this comment.
fair - can remove
tmm
reviewed
Mar 27, 2026
.changeset/realm-auto-detection.md
Outdated
| @@ -0,0 +1,5 @@ | |||
| --- | |||
| 'mppx': minor | |||
Member
There was a problem hiding this comment.
patch unless breaking change
Suggested change
| 'mppx': minor | |
| 'mppx': patch |
tmm
reviewed
Mar 27, 2026
.changeset/realm-auto-detection.md
Outdated
| 'mppx': minor | ||
| --- | ||
|
|
||
| Auto-detect `realm` from the request `Host` header when not explicitly configured. Resolution order: explicit value → env vars (`MPP_REALM`, `FLY_APP_NAME`, `VERCEL_URL`, etc.) → request URL hostname → `"MPP Payment"` fallback with a one-time warning. Removed the hard-coded `"MPP Payment"` default and deprioritized `HOST`/`HOSTNAME` env vars in favor of platform-specific alternatives. |
Member
There was a problem hiding this comment.
Suggested change
| Auto-detect `realm` from the request `Host` header when not explicitly configured. Resolution order: explicit value → env vars (`MPP_REALM`, `FLY_APP_NAME`, `VERCEL_URL`, etc.) → request URL hostname → `"MPP Payment"` fallback with a one-time warning. Removed the hard-coded `"MPP Payment"` default and deprioritized `HOST`/`HOSTNAME` env vars in favor of platform-specific alternatives. | |
| Added `realm` auto-detectection from the request `Host` header when not explicitly configured. Resolution order: explicit value → env vars (`MPP_REALM`, `FLY_APP_NAME`, `VERCEL_URL`, etc.) → request URL hostname → `"MPP Payment"` fallback with a one-time warning. Removed the hard-coded `"MPP Payment"` default and deprioritized `HOST`/`HOSTNAME` env vars in favor of platform-specific alternatives. |
3-tier realm resolution: explicit > env var > request hostname. Removes hardcoded 'MPP Payment' fallback. When no realm is configured, derives it from the incoming request's URL hostname at charge-time. Throws if realm cannot be determined (no URL or non-HTTP URL). This enables Cloudflare Workers (and any runtime) to automatically use the correct domain as the realm without explicit configuration. Also fixes env var precedence: MPP_REALM is now checked first. Removes HOST/HOSTNAME from realm detection (unsafe in containers). Warns once at Mppx.create() time when realm is not explicitly set.
3397b2d to
b2ee629
Compare
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
Updates realm resolution to be more robust, flowing from
This fixes a pretty common footgun where devs forget to set their realm and we get poor analytics as a result.