@@ -6,108 +6,56 @@ class RequestForgeryProtectionTest < ActionDispatch::IntegrationTest
66
77 @original_allow_forgery_protection = ActionController ::Base . allow_forgery_protection
88 ActionController ::Base . allow_forgery_protection = true
9+
10+ @original_force_ssl = Rails . configuration . force_ssl
911 end
1012
1113 teardown do
1214 ActionController ::Base . allow_forgery_protection = @original_allow_forgery_protection
15+ Rails . configuration . force_ssl = @original_force_ssl
1316 end
1417
15- test "fails if Sec-Fetch-Site is cross-site" do
16- assert_no_difference -> { Board . count } do
17- post boards_path ,
18- params : { board : { name : "Test Board" } } ,
19- headers : { "Sec-Fetch-Site" => "cross-site" }
20- end
21-
22- assert_response :unprocessable_entity
23- end
24-
25- test "succeeds with same-origin Sec-Fetch-Site" do
18+ test "JSON request succeeds with missing Sec-Fetch-Site header" do
2619 assert_difference -> { Board . count } , +1 do
2720 post boards_path ,
2821 params : { board : { name : "Test Board" } } ,
29- headers : { "Sec-Fetch-Site" => "same-origin" }
22+ as : :json
3023 end
3124
32- assert_response :redirect
25+ assert_response :created
3326 end
3427
35- test "succeeds with same-site Sec-Fetch-Site" do
28+ test "HTTP request succeeds with missing Sec-Fetch-Site header when force_ssl is disabled" do
29+ Rails . configuration . force_ssl = false
30+
3631 assert_difference -> { Board . count } , +1 do
3732 post boards_path ,
38- params : { board : { name : "Test Board" } } ,
39- headers : { "Sec-Fetch-Site" => "same-site" }
33+ params : { board : { name : "Test Board" } }
4034 end
4135
4236 assert_response :redirect
4337 end
4438
45- test "fails with none Sec-Fetch-Site" do
46- assert_no_difference -> { Board . count } do
47- post boards_path ,
48- params : { board : { name : "Test Board" } } ,
49- headers : { "Sec-Fetch-Site" => "none" }
50- end
39+ test "HTTP request fails with missing Sec-Fetch-Site header when force_ssl is enabled" do
40+ Rails . configuration . force_ssl = true
5141
52- assert_response :unprocessable_entity
53- end
54-
55- test "fails when Sec-Fetch-Site header is missing" do
5642 assert_no_difference -> { Board . count } do
57- post boards_path , params : { board : { name : "Test Board" } }
43+ post boards_path ,
44+ params : { board : { name : "Test Board" } }
5845 end
5946
6047 assert_response :unprocessable_entity
6148 end
6249
63- test "GET requests succeed regardless of Sec-Fetch-Site header" do
64- get board_path ( boards ( :writebook ) ) , headers : { "Sec-Fetch-Site" => "cross-site" }
50+ test "HTTPS request fails with missing Sec-Fetch-Site header" do
51+ Rails . configuration . force_ssl = false
6552
66- assert_response :success
67- end
68-
69- test "appends Sec-Fetch-Site to Vary header on GET requests" do
70- get board_path ( boards ( :writebook ) )
71-
72- assert_response :success
73- assert_includes response . headers [ "Vary" ] , "Sec-Fetch-Site"
74- end
75-
76- test "appends Sec-Fetch-Site to Vary header on POST requests" do
77- post boards_path ,
78- params : { board : { name : "Test Board" } } ,
79- headers : { "Sec-Fetch-Site" => "same-origin" }
80-
81- assert_response :redirect
82- assert_includes response . headers [ "Vary" ] , "Sec-Fetch-Site"
83- end
84-
85- test "JSON request succeeds with missing Sec-Fetch-Site" do
86- assert_difference -> { Board . count } , +1 do
87- post boards_path ,
88- params : { board : { name : "Test Board" } } ,
89- as : :json
90- end
91-
92- assert_response :created
93- end
94-
95- test "JSON request fails with cross-site Sec-Fetch-Site" do
9653 assert_no_difference -> { Board . count } do
9754 post boards_path ,
9855 params : { board : { name : "Test Board" } } ,
99- headers : { "Sec-Fetch-Site" => "cross-site" } ,
100- as : :json
56+ headers : { "X-Forwarded-Proto" => "https" }
10157 end
10258
10359 assert_response :unprocessable_entity
10460 end
105-
106- private
107- def csrf_token
108- @csrf_token ||= begin
109- get new_board_path
110- response . body [ /name="authenticity_token" value="([^"]+)"/ , 1 ]
111- end
112- end
11361end
0 commit comments