Why browser FHIR apps need CORS on HAPI FHIR and when to configure it
If a web app runs on a different origin than your HAPI FHIR server, the browser blocks the app's responses until the server returns the right CORS headers. An origin is the combination of scheme, host and port, so and are different origins even when both belong to your organization. Before the browser lets JavaScript read a cross-origin response, it checks the Access-Control-Allow-Origin header and, for credentialed requests, Access-Control-Allow-Credentials. For requests that use methods or headers beyond the simple set, such as PUT, DELETE, or a custom Authorization or Content-Type value, the browser first sends a preflight OPTIONS request and only sends the real request if the server approves the method and headers. Your team needs to configure CORS whenever a browser-based client, such as a SMART app, a clinician portal or a patient-facing front end, calls the server directly.
CORS is a browser control, not an access control. It tells the browser whether a web page from another origin may read a response, but it does not stop curl, a backend service, a mobile app or a malicious script from calling the server. Authentication and authorization still have to be enforced on the server through the interceptors covered elsewhere on this page's sibling topics. HAPI FHIR takes the same position: its security documentation describes it as offering building blocks, including authentication interceptors, the AuthorizationInterceptor, consent and search narrowing interceptors, CORS support and BALP audit logging, rather than a single security layer. Treat CORS as one piece of the design, and check the HAPI FHIR security introduction to see how these pieces fit together.
Decide early which origins really need access. Each web app, environment and identity provider redirect host should be listed explicitly, and you should confirm with your application team whether any apps need to call the server at all versus going through a backend-for-frontend or API gateway. Note that a server that does not answer preflight requests will fail browser calls even when authorization is correct, so test from the real browser and not only from a REST client. Confirm the exact CORS settings and defaults for your HAPI FHIR version in the server documentation and your own test environment before you rely on them in production.
CORS configuration options and when each fits
This table compares the CORS settings a HAPI FHIR operator chooses for browser-based apps, with the use case that fits each option and the risk if it is misused. Read each row as a decision: pick the narrowest setting the app needs, then confirm the exact configuration keys in the HAPI FHIR version you run. The main takeaway is that wildcard origins are only safe for truly public, unauthenticated data.
| Setting | When it fits | Risk if misused |
|---|---|---|
| Explicit origin list | Known production and staging front ends with fixed hostnames, such as a single SMART app or patient portal | Low risk; the main cost is keeping the list current when a new app or environment is added |
| Environment-specific origin list | Separate lists for development, test and production so local origins never reach production | Developer origins such as localhost left in production config let any local page call the server |
| Wildcard origin for public read-only data | Openly published reference data, such as a public terminology or conformance endpoint with no patient data and no credentials | Exposes any response to any site; must never be combined with credentials or patient-level resources |
| Allow credentials | Browser apps that send cookies or Authorization headers and need the response readable across origins | Requires explicit origins; a wildcard origin with credentials is rejected by browsers, and a reflected origin lets hostile sites use a signed-in user's session |
| Allowed headers | Only the headers the app sends, typically Authorization and Content-Type, plus any custom header the API requires | Broad header lists widen what a cross-origin request can carry; unused custom headers add surface without benefit |
| Allowed methods | Only the HTTP methods the app uses, often GET and POST for search and writes, adding PUT or DELETE only when required | Permitting write methods for read-only apps enables cross-origin changes that the app never needed |
| Max age for preflight responses | Caching preflight results for a period you choose, to reduce repeated OPTIONS requests in busy apps | Long caching delays the effect of tightened rules; confirm the default and accepted range in the HAPI FHIR documentation |
Setting up CORS on a HAPI FHIR server step by step
This sequence takes a browser-based FHIR app from an inventory of origins to a verified CORS configuration on the HAPI FHIR server, with each step confirmed before the next begins. It keeps the allowed origins explicit and makes sure credentials are only sent to origins you have named.
Inventory every browser app and its origins
List each web application that will call the FHIR server from a browser, including its scheme, host and port for every environment (development, test and production). Include any staging or preview hosts that a developer or tester will use. The engineering lead for each app confirms the list, and the platform team records it in the change ticket. This step is done when every origin is written down as an exact value, with no wildcards, and each app owner has signed off on their entries. Origins that are not on this list will be refused once the configuration is in place, so missing entries will surface as browser errors during testing.
Decide how the token reaches the server
Choose between sending an OAuth 2.0 bearer token in the Authorization header and relying on a cookie that the browser attaches automatically. Bearer tokens need the Authorization header allowed in CORS, and cookies require credentialed requests, which change what the server may return to the browser. The security architect and the app team make this decision together and record it, because it determines whether the allowed-origins list can be combined with credentials at all. This step is done when the team has a written choice for each app, with the reason, and the choice is matched to the authorization design in the server's access control layer.
Set explicit allowed origins in the JPA starter
In the JPA server starter configuration, set the CORS allowed origins to the exact list from step one, and set allowed methods and headers to the minimum your apps use, such as the standard FHIR methods and the Authorization and Content-Type headers. Do not use a wildcard origin when credentials are allowed, because browsers refuse that combination. Confirm the exact configuration keys and their current syntax against the HAPI FHIR documentation for your version, since the starter's property names are version-specific. Check the result by restarting the server and confirming it starts without configuration errors and that the settings appear in the loaded configuration.
Add a gateway only if it enforces CORS
Decide whether a reverse proxy or API gateway sits in front of the server and whether it will answer or rewrite CORS headers. If the gateway handles CORS, the server's CORS settings must match it or be left in a single place, because duplicate or conflicting Access-Control headers cause browsers to reject responses. If the gateway only forwards traffic, leave CORS to the HAPI FHIR server and make sure the proxy does not strip the Origin, Access-Control or Authorization headers. The platform engineer checks this by listing every header rule the proxy applies. This step is done when exactly one layer is named as the CORS owner in the runbook.
Verify preflight responses with curl
Send an OPTIONS request to a FHIR endpoint with an Origin header that matches an allowed origin, the intended method in Access-Control-Request-Method, and the headers your app sends in Access-Control-Request-Headers. Confirm the response includes Access-Control-Allow-Origin with that exact origin, the expected methods and headers, and Access-Control-Allow-Credentials only when cookies are in use. Repeat with an origin that is not on the list and confirm the allow-origin header is absent. Save both outputs with the change record. This check is done when the allowed origin receives the expected headers and the unlisted origin does not.
Test the real browser calls
Open each app in a browser from every environment on the origin list and run the calls it makes on startup and during a typical workflow, such as a patient search and a resource read. Use the browser developer tools network panel to confirm that preflight requests succeed, that actual requests return the expected headers, and that no CORS errors appear in the console. Also test from an unlisted origin and confirm the browser blocks the call. Record any failures with the origin, method and header involved. This step is done when every listed app completes its core calls without console errors and the unlisted origin is blocked.
Review the configuration and record the baseline
Compare the final configuration with the inventory from step one and the token decision from step two, and remove any origin, method or header that no app uses. Confirm that no wildcard origin is combined with credentials, and that the gateway or proxy, if present, does not add a second set of CORS headers. Store the configuration, the curl outputs and the browser test notes as the baseline for future changes. Ask the team to re-run the curl check after every upgrade of the server or the gateway, and whenever a new app origin is added. This step is done when the configuration, evidence and owner for each origin are on file.
CORS settings by environment: development, staging and production
Development environments exist so engineers can run a single-page app against a local or test HAPI FHIR server. Allowing localhost origins here is normal, because the server holds synthetic or de-identified data and the developers need fast feedback. The main risk is that a development setting quietly travels to another environment, so the allowed list should be explicit and visibly marked as non-production.
- Named localhost origins: List the exact scheme, host and port each developer tool uses, such as ` rather than a broad pattern.
- No real patient data: Keep the permissive development list on servers that hold only synthetic or de-identified data.
- Separate configuration file: Store the development origins in an environment-specific file so the production file never inherits them.
- Credentials off by default: Enable credentialed requests only for the apps that actually sign in during development.
- Startup visibility: Log the active origin list at startup so anyone can confirm which environment they are hitting.
Before sharing a development build, check that its origin list contains nothing a production deployment would also need to carry.
Staging exists to catch configuration problems before production does. Its origin list should mirror production as closely as possible, including the exact origins of the release candidate apps, the same credential behavior and the same header and method settings. When staging differs from production, a CORS failure can appear only after release, which is the failure staging is meant to prevent.
- Production-equivalent origins: Use the same origin values as production, changing only the host names where the environment requires it.
- Same header and method lists: Keep allowed headers and methods identical so preflight behavior matches what users will see.
- Test with the real browser: Verify preflight requests from the candidate app build in an actual browser, not only with command-line tools.
- Controlled test data: Staging can hold larger or realistic-looking datasets, but it should not hold live patient records unless your own privacy controls cover it.
- Drift checks: Compare the rendered origin configuration of staging and production as part of each release.
Confirm with your application team that every origin in staging matches an approved production origin, and that no staging-only entry remains in the production configuration.
Production should allow only the origins of known applications that your organization has approved. Each entry is a trust decision: any origin on the list can make authenticated browser requests that return patient data, so the list should be short, documented and owned. Wildcard origins are not an acceptable shortcut here, and credentialed requests should be limited to origins that truly need them.
- Known app origins only: Allow the exact scheme, host and port of each approved front end, and nothing else.
- No localhost entries: Confirm that no development origins can reach the production server through configuration inheritance.
- Owner for each origin: Record which application team owns each origin and why it is needed.
- Minimal credentials: Enable credentialed access only for origins that sign users in through the server's authorization model.
- Monitoring of rejected origins: Watch logs for blocked origins so that new or misconfigured apps are found quickly.
Before each release, confirm that the production origin list matches the approved register and that a reviewer other than the author has signed off on any change.
Origin lists belong in configuration, not code, so that environments can differ without a rebuild and so that changes are visible in version control. The HAPI FHIR server is typically configured through its deployment settings, and the exact property names and mechanisms depend on the version and how you deploy the server, so confirm them against the documentation for your release. Changes should go through the same review path as other infrastructure changes.
- Externalized settings: Keep the origin list in environment-specific configuration supplied at deployment, such as a values file or secret-free config map.
- Version-controlled changes: Store configuration in a repository where every origin change has an author, a reason and an approval.
- Two-person review: Require a reviewer from security or the platform team for any new origin or credential change.
- Scheduled recertification: Review the full origin list on a set cadence and remove applications that no longer exist.
- Testing before promotion: Run the change in staging and confirm preflight responses before it reaches production.
Ask your deployment team to show how an origin change moves from request to production, and who can approve it.
Security mistakes to avoid with CORS on FHIR
CORS decides which browser origins can read responses from your FHIR server, but it does not decide who the user is or what they may see. The mistakes below share one root cause: relaxing browser checks and then assuming the server's access controls will fill the gap. Each card pairs a risk with the control that should carry the real security load.
Wildcard origins with credentials
Allowing any origin while also allowing credentials lets any website make authenticated requests on behalf of a logged-in clinician and read the responses. Browsers refuse the combination, so a wildcard with credentials usually fails in testing and tempts teams to fix it by reflecting origins instead. Set an explicit list of allowed origins and enable credentials only for those. Check the configured origins in the HAPI FHIR CORS settings against every production and staging app hostname.
HAPI FHIR security docsReflecting the Origin header
Some servers copy whatever value arrives in the Origin header back into Access-Control-Allow-Origin. This turns the allowlist into a formality, because any attacker-controlled site is accepted. Compare the incoming origin against a fixed list and return it only on an exact match. Include a test that sends an unlisted origin and confirms the response omits the allow-origin header.
Trusting subdomains broadly
Matching a pattern such as any subdomain of a company domain includes hosts you may not control, including forgotten test environments, marketing sites hosted by third parties, or a subdomain taken over after its DNS record was left dangling. Each hostname that receives credentials becomes part of your trust boundary. List exact hostnames, review the list when apps are retired, and treat any wildcard subdomain pattern as a finding to justify.
Treating CORS as authorization
CORS only tells a browser whether to expose a response to a page's script. Tools such as curl, server-side code and malicious clients ignore it entirely, so a request without a permitted origin can still reach the server. Authorization must happen on the server, for example with the AuthorizationInterceptor rules that limit resource types, compartments and operations, plus token validation for every request. Test by calling the API directly without any Origin header and confirming that unauthorized requests are still denied.
Missing audit trail for browser access
Browser-based apps often run under shared service accounts or long-lived tokens, which hides who read which record. When a CORS policy is loosened, the audit trail becomes the main way to detect misuse. Configure BALP AuditEvent logging for reads, searches and writes, and record the client identity and user context supplied by the token. Confirm that events from the browser app can be traced to a named user and a specific client registration.
How a browser request passes CORS checks on the way to HAPI FHIR
The request starts in the browser app, which runs on its own origin outside your infrastructure. Before any data call, the browser sends a preflight OPTIONS request through the reverse proxy or gateway to HAPI FHIR. The server replies with the allowed origin, methods and headers, and only then does the browser send the real request with a bearer token. HAPI FHIR validates the token with the identity provider and applies its authorization rules, while the CORS headers decide whether the browser may read the response. Check that the proxy passes these headers through unchanged.
Preflight and actual request paths, with the trust boundary around your infrastructure.
Limits to confirm before relying on CORS settings
CORS behavior depends on the HAPI FHIR version, the application server or proxy in front of it, and any API gateway that handles responses. The cards below list the points to confirm in vendor documentation and in your own environment before you rely on a configuration in production.
Exact configuration keys for your version
Property names, interceptor class names and default values for CORS can change between HAPI FHIR releases, and the JPA server starter may expose them differently from a custom WAR build. Check the keys against the documentation for the exact version you run, not against examples copied from older blog posts or forum threads. Record the version alongside the configuration in your repository so a future upgrade has a reference point. Confirm that the server actually loads the setting by sending a test preflight request and reading the response headers.
HAPI FHIR versionsBehavior behind the application server
CORS headers are produced by the application stack, so the behavior depends on whether the servlet container, Spring Boot or the WAR deployment on Tomcat, Jetty or WildFly applies its own filters first. A filter order conflict can cause the server to answer preflight requests before the HAPI FHIR CORS logic runs, or to add duplicate headers that browsers reject. Test each deployment target you use, because a Docker Compose setup and a Kubernetes release may behave differently. Confirm the behavior in the container runtime you actually operate.
Whether the gateway overwrites CORS headers
A reverse proxy, ingress controller or API gateway may add, strip or replace Access-Control-Allow-Origin and related headers. If the gateway sets its own values, the browser sees the gateway policy and the server policy may never take effect, or the two may disagree. Ask the platform team which layer owns CORS in each environment and write that decision down. Compare responses at the browser edge with responses from the server directly to find where headers change.
Preflight caching in browsers
Browsers cache preflight responses for a period set by the Access-Control-Max-Age header, and the browser decides how long to honor that value. A stale cached preflight can keep blocking an app after you tighten or widen the allowed headers or methods. Confirm how your target browsers and any test tools handle the cache, and plan a cache-busting test after each change. Do not assume every browser applies the same limit.
Credentials and origin behavior in your stack
Browsers refuse a wildcard origin when credentials are included, so the configuration must name exact origins that carry cookies or Authorization headers. Confirm that your identity provider, SMART launch flow and any token endpoint respond with the same origin rules. Ask whether any component appends a wildcard by default. Test with an allowed origin, a disallowed origin and a credentialed request to see each outcome.
Frequently asked questions about CORS for HAPI FHIR
How much effort does CORS setup for a HAPI FHIR server take?
The effort depends on how many browser apps need access, how many origins they use, and whether requests pass through a gateway or reverse proxy. A single known origin with token-based requests is a small configuration change plus testing. Multiple origins across development, staging and production, plus preflight handling and credential rules, adds design and verification work. No figure is given here because the amount of work depends on your environment. Ask any partner for a written breakdown covering origin inventory, configuration, environment testing and handover documentation, so the effort is visible before work starts.
Can our team configure CORS on HAPI FHIR without outside help?
Yes, many teams can, provided someone understands HTTP preflight requests, the CORS response headers and how the server's CORS settings interact with interceptors and any reverse proxy in front of it. The harder part is usually deciding the policy: which origins are allowed, whether credentials are needed, and which headers the apps send. Your team needs a clear list of app origins and an owner who can test each environment. If that ownership or security review capacity is missing, a partner can take on the design and testing while your team keeps control of the final decisions.
What makes CORS configuration more complex for a FHIR server?
Three factors drive most of the complexity: the number of allowed origins, the presence of gateways or reverse proxies that also add or strip headers, and the authentication method. Apps that send bearer tokens in the Authorization header need that header allowed explicitly and need preflight responses to succeed. Apps that rely on cookies need credentialed requests, which cannot be combined with a wildcard origin and require exact origin matching. Each additional origin, environment or proxy layer adds a place where headers can conflict or be overwritten.
Should we allow all origins with a wildcard during development?
Avoid a wildcard origin on any environment that holds real patient data, and avoid it entirely when requests carry credentials. Browsers reject a wildcard origin combined with credentials, and allowing every origin with credentials would let any website make authenticated calls using a user's session. For development, use an explicit list of local origins instead. Make sure the development configuration cannot be promoted unchanged to production, and have the security team review the final origin list.
What information do we need to provide before CORS work starts?
You need the exact origin of each browser app in each environment, including scheme, host and port, and whether each app is a single-page application, a portal or a third-party app. You also need the authentication method (bearer tokens or cookies), the identity provider details that issue tokens, the list of HTTP methods and custom headers the apps send, and the gateway or reverse proxy in front of HAPI FHIR. Access to development, staging and production environments for testing is also required, along with a named owner who can approve the origin list.
When should we bring in a partner for CORS and access setup?
Consider a partner when the number of apps and origins is growing, when cookies and tokens are both in use, when a gateway rewrites headers, or when the server holds regulated health data and the security team needs documented evidence of the policy and its testing. A partner is also useful when the team lacks time to test preflight behavior across environments. Ask for a fixed-scope written proposal that lists the origins covered, the environments tested, the retest approach and what your team must review, so the scope and deliverables are agreed before work starts.
Does a correct CORS configuration secure the FHIR server by itself?
No. CORS controls which browser origins can read responses from the server, but it does not authenticate users or limit what an authenticated user can access. Non-browser clients ignore CORS entirely, so authorization rules, token validation and audit logging still have to be in place. Treat CORS as one control among several, and confirm with your security team how it fits with the authorization interceptor and the identity provider configuration.
What DeployOpen confirms with your team before configuring CORS
Before any CORS setting changes, DeployOpen works through a written scope with your team. The point of this step is to agree which browser apps will call the HAPI FHIR server, which origins each app runs from, and how those apps obtain tokens. A CORS allow list is only as good as the origins it names, so the scope records every production, staging and development origin, including any preview or tenant-specific hostnames your front ends generate. It also records which environments the server runs in, because the same allowedOrigins value often gets copied between development and production by mistake.
The token model decides most of the CORS design. If apps send bearer tokens in the Authorization header, the server must permit that header, and the scope confirms whether browsers will ever send cookies or other credentials to the FHIR endpoint. Where credentialed requests are needed, the scope names the exact origins that may send them, because a wildcard origin cannot be combined with credentials. Your team should also confirm which identity provider issues the tokens, whether SMART on FHIR launch flows run in the browser, and which HTTP methods and custom headers each app actually uses, so that unused methods stay off the list.
DeployOpen then delivers the configuration, verification evidence and a short review of the security settings around it. Verification covers preflight OPTIONS responses from each approved origin, rejected requests from an origin that is not listed, and confirmation that the response headers match the agreed scope. The review checks the surrounding controls, including whether a reverse proxy or API gateway in front of HAPI FHIR adds or strips CORS headers, and whether error responses leak data to unapproved origins. Confirm with your team who will own the origin list after handover and how changes are requested, since that list tends to grow with each new app.
How DeployOpen works on this
DeployOpen scopes CORS for browser-based FHIR apps on HAPI FHIR as a written, fixed-scope piece of work covering allowed origins, credentials, headers and methods. The work is delivered inside your own infrastructure, with the policy tested against the browser apps that will call the server.
How we scope
- Inventory every browser app, portal and SMART launch origin that must call the FHIR server
- Record which HTTP methods, request headers and credentialed calls each app needs
- Agree in writing which environments, origins and proxy layers the CORS policy covers
- Confirm whether an API gateway or reverse proxy will set CORS headers instead of HAPI FHIR
How we build
- Configure the HAPI FHIR CORS settings explicitly, with a named origin list for each environment
- Remove wildcard origins wherever credentials, cookies or bearer tokens are sent
- Test preflight requests and credentialed responses from each approved origin and from unapproved ones
- Check that gateway, ingress and server layers do not add conflicting or duplicate CORS headers
How we hand over
- Deliver the final origin, method and header configuration as versioned configuration in your repository
- Document the test cases, expected responses and the steps to add a new approved origin
- Provide a short runbook for diagnosing browser CORS errors in production and staging
- Walk your platform and application teams through the policy and its security trade-offs
How we support
- Engagements are fixed-scope, with scope, deliverables and timelines agreed in writing before work starts
- Ongoing support, upgrades and retesting after new apps or origins are available as a retainer
- Retesting covers configuration changes made when HAPI FHIR versions or front-end domains change
- Ask any partner to confirm how origin requests are reviewed and who approves changes to the list

