Skip to content

Commit d05ab2a

Browse files
committed
fix(next): update cors usage
1 parent cd487e2 commit d05ab2a

3 files changed

Lines changed: 88 additions & 26 deletions

File tree

modules/next/CORS.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# CORS Configuration for Next.js Module
2+
3+
## Security Update
4+
5+
As of version 2.x (and backported to 1.x), the Next module **no longer automatically enables CORS**. Previous versions created a security vulnerability by forcing `Access-Control-Allow-Origin: *` on all requests.
6+
7+
## Do You Need CORS?
8+
9+
Most Next.js applications do **NOT** need CORS:
10+
11+
-**No CORS needed**: `getStaticProps`, `getServerSideProps`, Server Components, API routes, Server Actions
12+
-**CORS required**: Client-side `fetch()` calls directly to Drupal from the browser
13+
14+
**Recommended**: Use Next.js API routes or Server Actions as a proxy. Your client fetches from Next.js (same-origin), which fetches from Drupal server-side. More secure, no CORS needed.
15+
16+
## Configuration
17+
18+
If you need CORS, configure it in `sites/default/services.yml`:
19+
20+
```yaml
21+
parameters:
22+
cors.config:
23+
enabled: true
24+
allowedOrigins:
25+
- "https://www.your-site.com" # Your Next.js domain
26+
- "http://localhost:3000" # Local development
27+
allowedMethods: ["GET", "POST", "OPTIONS"]
28+
allowedHeaders: ["authorization", "content-type", "accept"]
29+
supportsCredentials: true # Required for auth
30+
```
31+
32+
**Never use `allowedOrigins: ['*']` in production.**
33+
34+
Then clear cache: `drush cr`
35+
36+
## Next.js-Specific Notes
37+
38+
- **Different subdomains = different origins**: `www.site.com` ≠ `cms.site.com` - list each explicitly
39+
- **Preview mode**: Uses server-side auth, doesn't need CORS
40+
- **On-demand revalidation**: Drupal → Next.js, doesn't need CORS on Drupal side
41+
- **Environment-specific config**: Use `development.services.yml` for local development (loaded via `settings.local.php`)
42+
43+
## Resources
44+
45+
- [Drupal CORS Documentation](https://www.drupal.org/node/2715637)

modules/next/next.post_update.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Post update functions for Next.
6+
*
7+
* All empty post-update hooks ensure the cache is cleared.
8+
* @see https://www.drupal.org/node/2960601
9+
*/
10+
11+
/**
12+
* SECURITY: Remove automatic CORS enablement.
13+
*
14+
* The Next module previously forced CORS to be enabled with permissive default
15+
* settings (Access-Control-Allow-Origin: *), which created a security
16+
* vulnerability.
17+
*
18+
* CORS is only required if your Next.js application makes client-side browser
19+
* requests to your Drupal API (e.g., useEffect, useSWR, client components).
20+
* Most Next.js applications use server-side data fetching and do not need CORS.
21+
*
22+
* If your site requires CORS, you must now configure it explicitly in your
23+
* sites/default/services.yml file. See the module's CORS.md file or
24+
* https://www.drupal.org/node/2715637 for proper CORS configuration.
25+
*
26+
* Example CORS configuration for Next.js sites with client-side fetching:
27+
* @code
28+
* parameters:
29+
* cors.config:
30+
* enabled: true
31+
* allowedHeaders: ['x-csrf-token', 'authorization', 'content-type', 'accept']
32+
* allowedMethods: ['GET', 'POST', 'OPTIONS']
33+
* allowedOrigins: ['https://www.your-site.com']
34+
* exposedHeaders: false
35+
* maxAge: false
36+
* supportsCredentials: true
37+
* @endcode
38+
*/
39+
function next_post_update_remove_automatic_cors() {
40+
\Drupal::messenger()->addWarning(t('SECURITY UPDATE: The Next module no longer automatically enables CORS. If your Next.js site makes client-side API requests (useEffect, useSWR, etc.), you must configure CORS explicitly in services.yml. See the module\'s CORS.md file or <a href="@url">Drupal CORS documentation</a> for details.', [
41+
'@url' => 'https://www.drupal.org/node/2715637',
42+
]));
43+
}

modules/next/src/NextServiceProvider.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)