Skip to content

Commit cd43ae9

Browse files
namedgraphclaude
andcommitted
Fix CORS response headers (#286)
* 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> * Add CORS header to /static/ nginx location blocks The cors-static.sh test was failing because Access-Control-Allow-Origin was only added to location / but not location ^~ /static/. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1995c54 commit cd43ae9

File tree

5 files changed

+33
-99
lines changed

5 files changed

+33
-99
lines changed

docker-compose.yml

Lines changed: 31 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/ {
@@ -238,6 +250,7 @@ configs:
238250
proxy_pass http://linkeddatahub;
239251
limit_req zone=static_files burst=50 nodelay;
240252
253+
add_header Access-Control-Allow-Origin "*" always;
241254
add_header Cache-Control "public, max-age=604800, immutable";
242255
}
243256
}
@@ -253,6 +266,20 @@ configs:
253266
ssl_verify_client optional_no_ca;
254267
255268
location / {
269+
add_header Access-Control-Allow-Origin "*" always;
270+
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS" always;
271+
add_header Access-Control-Allow-Headers "Accept, Content-Type, Authorization" always;
272+
add_header Access-Control-Expose-Headers "Link, Content-Location, Location" always;
273+
274+
if ($$request_method = OPTIONS) {
275+
add_header Access-Control-Allow-Origin "*";
276+
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS";
277+
add_header Access-Control-Allow-Headers "Accept, Content-Type, Authorization";
278+
add_header Access-Control-Expose-Headers "Link, Content-Location, Location";
279+
add_header Access-Control-Max-Age "1728000";
280+
return 204;
281+
}
282+
256283
proxy_pass http://linkeddatahub;
257284
#proxy_cache backcache;
258285
limit_req zone=linked_data burst=30 nodelay;
@@ -269,6 +296,8 @@ configs:
269296
location ^~ /static/ {
270297
proxy_pass http://linkeddatahub;
271298
limit_req zone=static_files burst=50 nodelay;
299+
300+
add_header Access-Control-Allow-Origin "*" always;
272301
}
273302
}
274303

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)