Skip to content

Commit ce8037c

Browse files
namedgraphclaude
andcommitted
Move CORS headers from Java filters to nginx
Varnish caches responses without varying on Origin, so whether CORS headers appear in cached responses depends on which request first populated the cache. Moving CORS to nginx ensures the headers are always present on every response regardless of cache state. Removes JAX-RS CORSFilter and Tomcat CorsFilter (web.xml /static/*); adds Access-Control-* headers and OPTIONS preflight (204) to nginx location / blocks in both docker-compose.yml and nginx.conf.template. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 00f0d60 commit ce8037c

File tree

5 files changed

+30
-99
lines changed

5 files changed

+30
-99
lines changed

docker-compose.yml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ configs:
204204
ssl_verify_client ${NGINX_SSL_VERIFY_CLIENT:-optional_no_ca};
205205
206206
location / {
207+
add_header Access-Control-Allow-Origin "*" always;
208+
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS" always;
209+
add_header Access-Control-Allow-Headers "Accept, Content-Type, Authorization" always;
210+
add_header Access-Control-Expose-Headers "Link, Content-Location, Location" always;
211+
212+
if ($$request_method = OPTIONS) {
213+
add_header Access-Control-Allow-Origin "*";
214+
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS";
215+
add_header Access-Control-Allow-Headers "Accept, Content-Type, Authorization";
216+
add_header Access-Control-Expose-Headers "Link, Content-Location, Location";
217+
add_header Access-Control-Max-Age "1728000";
218+
return 204;
219+
}
220+
207221
proxy_pass http://linkeddatahub;
208222
#proxy_cache backcache;
209223
limit_req zone=linked_data burst=30 nodelay;
@@ -215,8 +229,6 @@ configs:
215229
216230
proxy_set_header Client-Cert '';
217231
proxy_set_header Client-Cert $$ssl_client_escaped_cert;
218-
219-
# add_header Cache-Control "public, max-age=86400";
220232
}
221233
222234
location ^~ /uploads/ {
@@ -253,6 +265,20 @@ configs:
253265
ssl_verify_client optional_no_ca;
254266
255267
location / {
268+
add_header Access-Control-Allow-Origin "*" always;
269+
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS" always;
270+
add_header Access-Control-Allow-Headers "Accept, Content-Type, Authorization" always;
271+
add_header Access-Control-Expose-Headers "Link, Content-Location, Location" always;
272+
273+
if ($$request_method = OPTIONS) {
274+
add_header Access-Control-Allow-Origin "*";
275+
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS";
276+
add_header Access-Control-Allow-Headers "Accept, Content-Type, Authorization";
277+
add_header Access-Control-Expose-Headers "Link, Content-Location, Location";
278+
add_header Access-Control-Max-Age "1728000";
279+
return 204;
280+
}
281+
256282
proxy_pass http://linkeddatahub;
257283
#proxy_cache backcache;
258284
limit_req zone=linked_data burst=30 nodelay;

http-tests/misc/cors-jaxrs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ purge_cache "$END_USER_VARNISH_SERVICE"
77
purge_cache "$ADMIN_VARNISH_SERVICE"
88
purge_cache "$FRONTEND_VARNISH_SERVICE"
99

10-
# Test JAX-RS CORSFilter on dynamic content (GET request)
10+
# Test nginx CORS headers on dynamic content (GET request)
1111

1212
response=$(curl -i -k -s \
1313
-H "Origin: https://example.com" \

src/main/java/com/atomgraph/linkeddatahub/Application.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
import com.atomgraph.linkeddatahub.server.filter.request.AuthorizationFilter;
106106
import com.atomgraph.linkeddatahub.server.filter.request.ContentLengthLimitFilter;
107107
import com.atomgraph.linkeddatahub.server.filter.request.auth.ProxiedWebIDFilter;
108-
import com.atomgraph.linkeddatahub.server.filter.response.CORSFilter;
109108
import com.atomgraph.linkeddatahub.server.filter.response.ResponseHeadersFilter;
110109
import com.atomgraph.linkeddatahub.server.filter.response.CacheInvalidationFilter;
111110
import com.atomgraph.linkeddatahub.server.filter.response.XsltExecutableFilter;
@@ -1126,7 +1125,6 @@ protected void registerContainerRequestFilters()
11261125
*/
11271126
protected void registerContainerResponseFilters()
11281127
{
1129-
register(new CORSFilter());
11301128
register(new ResponseHeadersFilter());
11311129
register(new XsltExecutableFilter());
11321130
if (isInvalidateCache()) register(new CacheInvalidationFilter());

src/main/java/com/atomgraph/linkeddatahub/server/filter/response/CORSFilter.java

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

src/main/webapp/WEB-INF/web.xml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -373,23 +373,7 @@ support@atomgraph.com]]></param-value>
373373
<servlet-name>com.atomgraph.linkeddatahub.Application</servlet-name>
374374
<url-pattern>/*</url-pattern>
375375
</servlet-mapping>
376-
<filter>
377-
<filter-name>CORS filter</filter-name>
378-
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
379-
<init-param>
380-
<param-name>cors.allowed.origins</param-name>
381-
<param-value>*</param-value>
382-
</init-param>
383-
<init-param>
384-
<param-name>cors.allowed.methods</param-name>
385-
<param-value>GET,HEAD,OPTIONS</param-value>
386-
</init-param>
387-
</filter>
388-
<filter-mapping>
389-
<filter-name>CORS filter</filter-name>
390-
<url-pattern>/static/*</url-pattern>
391-
</filter-mapping>
392-
<filter>
376+
<filter>
393377
<filter-name>HSTS filter</filter-name>
394378
<filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
395379
<init-param>

0 commit comments

Comments
 (0)