Skip to content

Commit 907790f

Browse files
committed
Add CORS Documentation to Framework Documentation
1 parent 7b987d6 commit 907790f

File tree

4 files changed

+796
-54
lines changed

4 files changed

+796
-54
lines changed

_includes/docs-sidebar.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ <h3 class="mt-2">Framework</h3>
127127
<li><a href="/docs/framework/core-concepts">Core Concepts</a></li>
128128
<li><a href="/docs/framework/tutorial"><strong>Tutorial</strong></a></li>
129129
<li><a href="/docs/framework/services">Services</a></li>
130+
<li><a href="/docs/framework/cors">CORS Handling</a></li>
130131
<li><a href="/docs/framework/interceptors">Interceptors</a></li>
131132
<li><a href="/docs/framework/providers">Providers</a></li>
132133
<li><a href="/docs/framework/initializers">Initializers</a></li>

docs/foundations/upgrade-to-v9.adoc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ This page summarizes the new features and provides guidance on upgrading from pr
3131
|Migrated from `AsyncLoadingCache` to `LoadingCache` for improved performance and reduced overhead
3232
|GraalVM Native Image Support
3333
|RESTHeart officially listed as framework tested with GraalVM native image
34+
|Optimize CORS Headers
35+
|Reduced default CORS headers to minimum required set for improved security, requiring services to explicitly expose additional headers
3436
|===
3537

3638
==== MongoService
@@ -273,6 +275,62 @@ RESTHeart is now officially listed on GraalVM's framework compatibility page as
273275
- Unified reachability metadata configuration format
274276
- Explicit initialization of PluginsClassloader for native image execution
275277

278+
==== CORS Headers Optimization
279+
280+
RESTHeart v9 implements CORS (Cross-Origin Resource Sharing) headers following the principle of least privilege, using minimal defaults that services can extend as needed.
281+
282+
**Default CORS Headers:**
283+
284+
RESTHeart provides secure, minimal defaults for CORS headers:
285+
286+
- **Allow Headers**: `Authorization`, `Content-Type`, `X-Requested-With`, `No-Auth-Challenge`
287+
- **Expose Headers**: Empty by default - services explicitly declare what they expose
288+
- **Allow Methods**: `GET`, `PUT`, `POST`, `PATCH`, `DELETE`
289+
- **Allow Origin**: `*` (any origin)
290+
- **Allow Credentials**: `true`
291+
292+
**Customizing CORS Behavior:**
293+
294+
Services customize CORS behavior by overriding methods in the `CORSHeaders` interface:
295+
296+
- **`accessControlExposeHeaders()`** - Declares which response headers are exposed to browser clients
297+
- **`accessControlAllowMethods()`** - Specifies HTTP methods supported by the service
298+
- **`accessControlAllowHeaders()`** - Lists which request headers are allowed
299+
- **`accessControlAllowOrigin()`** - Restricts allowed origins (defaults to `*`)
300+
- **`corsEnabled()`** - Disables CORS entirely for internal APIs
301+
302+
**Example - Service exposing response headers:**
303+
304+
[source,java]
305+
----
306+
@RegisterPlugin(
307+
name = "resourceService",
308+
description = "Service that creates resources")
309+
public class ResourceService implements JsonService {
310+
@Override
311+
public void handle(JsonRequest req, JsonResponse res) {
312+
if (req.isPost()) {
313+
var id = createResource(req.getContent());
314+
res.getHeaders().add(HttpString.tryFromString("Location"),
315+
"/resources/" + id);
316+
res.setStatusCode(HttpStatus.SC_CREATED);
317+
}
318+
}
319+
320+
@Override
321+
public String accessControlExposeHeaders() {
322+
// Make Location header accessible to browser clients
323+
return "Location, ETag";
324+
}
325+
}
326+
----
327+
328+
**Key Point:**
329+
330+
Response headers like `Location`, `ETag`, or custom headers are not accessible to browser JavaScript unless explicitly exposed via `accessControlExposeHeaders()`. Services that set such headers must override this method.
331+
332+
See the link:/docs/plugins/cors[CORS Handling documentation] for complete details and examples.
333+
276334
==== Improve Aggregation Pipeline Security with Stage and Operator Blacklisting
277335

278336
RESTHeart v9 addresses a security gap where dangerous MongoDB operators could bypass protections when used in aggregation pipelines.
@@ -695,6 +753,12 @@ permissions:
695753
- Action: Update client applications to use `/token` or `/token/cookie` endpoints
696754
- Action: Remove `?set-auth-cookie` and similar query parameters from API calls
697755

756+
6. **CORS Headers Optimization**
757+
- **Exposed Headers Changed**: Default `Access-Control-Expose-Headers` is now empty (previously included `Location`, `ETag`, `Auth-Token`, etc.)
758+
- Action: Services that set response headers like `Location`, `ETag`, or custom headers must implement the `accessControlExposeHeaders()` method to explicitly expose them
759+
- Action: Review browser-based applications that rely on reading response headers and ensure services expose the necessary headers
760+
- Note: This is a critical change for services that return `Location` headers (e.g., POST operations that create resources)
761+
698762
==== Configuration Updates
699763

700764
**JWT Configuration Provider:**

0 commit comments

Comments
 (0)