Skip to content

Commit 7b572b9

Browse files
authored
Remove the CSP response header on 304 response (#49)
* Remove the CSP resp header on 304 * Remove OTP 24, add OTP 27
1 parent f7bbcd1 commit 7b572b9

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818

1919
strategy:
2020
matrix:
21-
otp_version: [24,25,26]
21+
otp_version: [25,26,27]
2222
os: [ubuntu-latest]
2323

2424
container:

src/cowmachine_controller.erl

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@
3131

3232
%% @doc Get default value by Key.
3333
-spec default(DefaultID, Context) -> Result when
34-
DefaultID:: service_available | resource_exists | auth_required | is_authorized | forbidden | upgrades_provided | allow_missing_post | malformed_request | uri_too_long | known_content_type | valid_content_headers | valid_entity_length | options | allowed_methods | known_methods | validate_content_checksum | content_types_provided | content_types_accepted | delete_resource | delete_completed | post_is_create | create_path | base_uri | process_post | language_available | charsets_provided | content_encodings_provided | transfer_encodings_provided | variances | is_conflict | multiple_choices | previously_existed | moved_permanently | moved_temporarily | last_modified | expires | generate_etag | finish_request,
35-
Context :: cowmachine_req:context(),
36-
Result :: no_charset | no_default | undefined | boolean() | list(binary()).
34+
DefaultID:: service_available | resource_exists | auth_required | is_authorized |
35+
forbidden | upgrades_provided | allow_missing_post | malformed_request |
36+
uri_too_long | known_content_type | valid_content_headers | valid_entity_length |
37+
options | allowed_methods | known_methods | validate_content_checksum |
38+
content_types_provided | content_types_accepted | delete_resource | delete_completed |
39+
post_is_create | create_path | base_uri | process_post | language_available |
40+
charsets_provided | content_encodings_provided | transfer_encodings_provided |
41+
variances | is_conflict | multiple_choices | previously_existed | moved_permanently |
42+
moved_temporarily | last_modified | expires | generate_etag | finish_request,
43+
Context :: cowmachine_req:context(),
44+
Result :: no_charset | no_default | undefined | boolean() | list(binary()).
3745
default(service_available, _Context) ->
3846
true;
3947
default(resource_exists, _Context) ->
@@ -134,8 +142,8 @@ default(_, _Context) ->
134142
%% @doc Content types that are textual and should have a charset defined.
135143

136144
-spec is_text(ContentType) -> Result when
137-
ContentType :: cow_http_hd:media_type(),
138-
Result :: boolean().
145+
ContentType :: cow_http_hd:media_type(),
146+
Result :: boolean().
139147
is_text({<<"text">>, _, _}) -> true;
140148
is_text({<<"application">>, <<"json">>, _}) -> true;
141149
is_text({<<"application">>, <<"ld+json">>, _}) -> true;
@@ -157,11 +165,11 @@ is_text(_) ->
157165
%% @doc Export and run function `Fun'.
158166

159167
-spec do(Fun, State, Context) -> Result when
160-
Fun :: atom(),
161-
State :: cmstate(),
162-
Context :: cowmachine_req:context(),
163-
Result :: {ContentType, Context},
164-
ContentType :: cow_http_hd:media_type().
168+
Fun :: atom(),
169+
State :: cmstate(),
170+
Context :: cowmachine_req:context(),
171+
Result :: {ContentType, Context},
172+
ContentType :: cow_http_hd:media_type().
165173
do(Fun, #cmstate{ controller = Controller }, Context) when is_atom(Fun) ->
166174
case erlang:function_exported(Controller, Fun, 1) of
167175
true ->
@@ -176,12 +184,12 @@ do(Fun, #cmstate{ controller = Controller }, Context) when is_atom(Fun) ->
176184
%% @doc Export and process `State' with `Context'.
177185

178186
-spec do_process(ContentType, State, Context) -> Result when
179-
ContentType :: cow_http_hd:media_type(),
180-
State :: cmstate(),
181-
Context :: cowmachine_req:context(),
182-
Result :: {Res, Context},
183-
Res :: boolean() | cowmachine_req:halt() | {error, any(), any()} | {error, any()} |
184-
cowmachine_req:resp_body().
187+
ContentType :: cow_http_hd:media_type(),
188+
State :: cmstate(),
189+
Context :: cowmachine_req:context(),
190+
Result :: {Res, Context},
191+
Res :: boolean() | cowmachine_req:halt() | {error, any(), any()} | {error, any()} |
192+
cowmachine_req:resp_body().
185193
do_process(ContentType, #cmstate{ controller = Controller }, Context) ->
186194
case erlang:function_exported(Controller, process, 4) of
187195
true ->

src/cowmachine_decision_core.erl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ d(DecisionID, State, Context) ->
112112
Context :: cowmachine_req:context(),
113113
%Result :: {State, Context}.
114114
Result :: {term(), #cmstate{}, term()}.
115-
respond(Code, State, Context) ->
115+
respond(Code, State, Context0) ->
116+
Context = maybe_drop_csp_headers(Code, Context0),
116117
{State1, Context1} = case Code of
117118
Ok when Ok >= 200, Ok =< 299 ->
118119
% Response all ok
@@ -172,6 +173,14 @@ respond(Code, Headers, State, Context) ->
172173
ContextHs = cowmachine_req:set_resp_headers(Headers, Context),
173174
respond(Code, State, ContextHs).
174175

176+
%% @doc On 304, remove the CSP header. The UA will use the cached version, with
177+
%% the CSP of the cached version. If we provide a new CSP then that one will
178+
%% supercede the cached one, which will give nonce problems.
179+
maybe_drop_csp_headers(304, Context) ->
180+
cowmachine_req:remove_resp_header(<<"content-security-policy">>, Context);
181+
maybe_drop_csp_headers(_Code, Context) ->
182+
Context.
183+
175184
%% @throws {stop_request, Code, Reason}
176185

177186
error_response(Code, Reason, State, Context) ->

0 commit comments

Comments
 (0)