fix: respect proxy headers when constructing resource URLs in withMcpAuth and protectedResourceHandler #136
+217
−13
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.
This pull request adds robust support for detecting the public-facing URL and origin of requests, especially when running behind reverse proxies. It introduces new utility functions to correctly interpret proxy headers, updates authentication handlers to use these utilities or allow explicit URL overrides, and adds comprehensive tests for these scenarios.
Proxy-aware URL detection and utilities:
getPublicOriginandgetPublicUrlutility functions insrc/lib/url.tsto reconstruct the public-facing origin and URL from incoming requests, respectingX-Forwarded-Host,X-Forwarded-Proto, andForwardedheaders, or falling back toreq.urlif not present.getPublicOriginandgetPublicUrlfrom the package entry point for external use.Authentication handler improvements:
protectedResourceHandlerinsrc/auth/auth-metadata.tsto usegetPublicUrlfor auto-detecting the public URL, and added an optionalresourceUrlparameter for explicit overrides, improving support for deployments behind proxies or with unusual network setups. [1] [2]withMcpAuthinsrc/auth/auth-wrapper.tsto use the newgetPublicOriginutility and allow explicitresourceUrloverrides, ensuring correct construction of resource metadata URLs in proxied environments. [1] [2]Testing and validation:
tests/auth.test.tsto verify correct behavior of public URL detection with various proxy header scenarios and explicit resource URL overrides.