33require_relative "../../../../spec_helper"
44
55describe Hooks ::App ::Auth do
6+ let ( :log ) { instance_double ( Logger ) . as_null_object }
67 let ( :test_class ) do
78 Class . new do
89 include Hooks ::App ::Auth
@@ -17,6 +18,10 @@ def error!(message, code)
1718 let ( :payload ) { '{"test": "data"}' }
1819 let ( :headers ) { { "Content-Type" => "application/json" } }
1920
21+ before ( :each ) do
22+ Hooks ::Log . instance = log
23+ end
24+
2025 describe "#validate_auth!" do
2126 context "when testing security vulnerabilities" do
2227 context "with missing auth configuration" do
@@ -63,39 +68,77 @@ def error!(message, code)
6368 end
6469
6570 it "rejects request with empty string type" do
66- # TODO
71+ endpoint_config = { auth : { type : "" } }
72+
73+ expect do
74+ instance . validate_auth! ( payload , headers , endpoint_config )
75+ end . to raise_error ( StandardError , /authentication configuration missing or invalid/ )
6776 end
6877 end
6978
7079 context "with missing secret configuration" do
7180 it "rejects request with missing secret_env_key" do
72- # TODO
81+ endpoint_config = { auth : { type : "hmac" } }
82+
83+ expect do
84+ instance . validate_auth! ( payload , headers , endpoint_config )
85+ end . to raise_error ( StandardError , /authentication failed/ )
7386 end
7487
7588 it "rejects request with nil secret_env_key" do
76- # TODO
89+ endpoint_config = { auth : { type : "hmac" , secret_env_key : nil } }
90+
91+ expect do
92+ instance . validate_auth! ( payload , headers , endpoint_config )
93+ end . to raise_error ( StandardError , /authentication failed/ )
7794 end
7895
7996 it "rejects request with empty secret_env_key" do
80- # TODO
97+ endpoint_config = { auth : { type : "hmac" , secret_env_key : "" } }
98+
99+ expect do
100+ instance . validate_auth! ( payload , headers , endpoint_config )
101+ end . to raise_error ( StandardError , /authentication failed/ )
81102 end
82103
83104 it "rejects request with whitespace-only secret_env_key" do
84- # TODO
105+ endpoint_config = { auth : { type : "hmac" , secret_env_key : " " } }
106+
107+ expect do
108+ instance . validate_auth! ( payload , headers , endpoint_config )
109+ end . to raise_error ( StandardError , /authentication failed/ )
85110 end
86111
87112 it "rejects request with non-string secret_env_key" do
88- # TODO
113+ endpoint_config = { auth : { type : "hmac" , secret_env_key : 123 } }
114+
115+ expect do
116+ instance . validate_auth! ( payload , headers , endpoint_config )
117+ end . to raise_error ( StandardError , /authentication failed/ )
89118 end
90119 end
91120
92121 context "with missing environment variable" do
93122 it "uses generic error message for missing secrets" do
94- # TODO
123+ ENV . delete ( "NONEXISTENT_SECRET" )
124+ endpoint_config = { auth : { type : "hmac" , secret_env_key : "NONEXISTENT_SECRET" } }
125+
126+ expect do
127+ instance . validate_auth! ( payload , headers , endpoint_config )
128+ end . to raise_error ( StandardError , /authentication failed/ )
95129 end
96130
97131 it "does not leak the environment variable name in error" do
98- # TODO
132+ ENV . delete ( "SECRET_WEBHOOK_KEY" )
133+ endpoint_config = { auth : { type : "hmac" , secret_env_key : "SECRET_WEBHOOK_KEY" } }
134+
135+ expect do
136+ instance . validate_auth! ( payload , headers , endpoint_config )
137+ end . to raise_error do |error |
138+ # Ensure error message is generic and doesn't leak the environment variable name
139+ expect ( error . message ) . not_to include ( "SECRET_WEBHOOK_KEY" )
140+ expect ( error . message ) . to match ( /authentication failed/ )
141+ end
99142 end
100143 end
101144
0 commit comments