|
117 | 117 | end
|
118 | 118 |
|
119 | 119 | describe "slack" do
|
120 |
| - it "receives a POST request but contains an invalid HMAC signature" do |
| 120 | + it "successfully processes a valid POST request with HMAC signature and timestamp" do |
121 | 121 | payload = { text: "Hello, Slack!" }
|
122 |
| - digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_ALT_HMAC_SECRET, payload.to_json) |
123 |
| - headers = { "Content-Type" => "application/json", "Signature-256" => "sha256=#{digest}" } |
| 122 | + timestamp = Time.now.to_i.to_s |
| 123 | + body = payload.to_json |
| 124 | + signing_payload = "v0:#{timestamp}:#{body}" |
| 125 | + digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_ALT_HMAC_SECRET, signing_payload) |
| 126 | + headers = { |
| 127 | + "Content-Type" => "application/json", |
| 128 | + "Signature-256" => "v0=#{digest}", |
| 129 | + "X-Timestamp" => timestamp |
| 130 | + } |
| 131 | + response = http.post("/webhooks/slack", body, headers) |
| 132 | + expect(response).to be_a(Net::HTTPSuccess) |
| 133 | + body = JSON.parse(response.body) |
| 134 | + expect(body["status"]).to eq("success") |
| 135 | + end |
| 136 | + |
| 137 | + it "rejects request with expired timestamp" do |
| 138 | + payload = { text: "Hello, Slack!" } |
| 139 | + # Use timestamp that's 10 minutes old (beyond the 5 minute tolerance) |
| 140 | + expired_timestamp = (Time.now.to_i - 600).to_s |
| 141 | + |
| 142 | + signing_payload = "v0:#{expired_timestamp}:#{payload.to_json}" |
| 143 | + digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_ALT_HMAC_SECRET, signing_payload) |
| 144 | + |
| 145 | + headers = { |
| 146 | + "Content-Type" => "application/json", |
| 147 | + "Signature-256" => "v0=#{digest}", |
| 148 | + "X-Timestamp" => expired_timestamp |
| 149 | + } |
| 150 | + |
124 | 151 | response = http.post("/webhooks/slack", payload.to_json, headers)
|
| 152 | + expect(response).to be_a(Net::HTTPUnauthorized) |
| 153 | + expect(response.body).to include("authentication failed") |
| 154 | + end |
| 155 | + |
| 156 | + it "rejects request with missing timestamp header" do |
| 157 | + payload = { text: "Hello, Slack!" } |
| 158 | + timestamp = Time.now.to_i.to_s |
125 | 159 |
|
| 160 | + # Create signature with timestamp but don't include timestamp header |
| 161 | + signing_payload = "v0:#{timestamp}:#{payload.to_json}" |
| 162 | + digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_ALT_HMAC_SECRET, signing_payload) |
| 163 | + |
| 164 | + headers = { |
| 165 | + "Content-Type" => "application/json", |
| 166 | + "Signature-256" => "v0=#{digest}" |
| 167 | + # Missing X-Timestamp header |
| 168 | + } |
| 169 | + |
| 170 | + response = http.post("/webhooks/slack", payload.to_json, headers) |
| 171 | + expect(response).to be_a(Net::HTTPUnauthorized) |
| 172 | + expect(response.body).to include("authentication failed") |
| 173 | + end |
| 174 | + |
| 175 | + it "rejects request with invalid timestamp format" do |
| 176 | + payload = { text: "Hello, Slack!" } |
| 177 | + invalid_timestamp = "not-a-timestamp" |
| 178 | + |
| 179 | + signing_payload = "v0:#{invalid_timestamp}:#{payload.to_json}" |
| 180 | + digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_ALT_HMAC_SECRET, signing_payload) |
| 181 | + |
| 182 | + headers = { |
| 183 | + "Content-Type" => "application/json", |
| 184 | + "Signature-256" => "v0=#{digest}", |
| 185 | + "X-Timestamp" => invalid_timestamp |
| 186 | + } |
| 187 | + |
| 188 | + response = http.post("/webhooks/slack", payload.to_json, headers) |
| 189 | + expect(response).to be_a(Net::HTTPUnauthorized) |
| 190 | + expect(response.body).to include("authentication failed") |
| 191 | + end |
| 192 | + |
| 193 | + it "successfully processes request with ISO 8601 UTC timestamp" do |
| 194 | + payload = { text: "Hello, Slack!" } |
| 195 | + iso_timestamp = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ") |
| 196 | + body = payload.to_json |
| 197 | + signing_payload = "v0:#{iso_timestamp}:#{body}" |
| 198 | + digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_ALT_HMAC_SECRET, signing_payload) |
| 199 | + headers = { |
| 200 | + "Content-Type" => "application/json", |
| 201 | + "Signature-256" => "v0=#{digest}", |
| 202 | + "X-Timestamp" => iso_timestamp |
| 203 | + } |
| 204 | + response = http.post("/webhooks/slack", body, headers) |
| 205 | + expect(response).to be_a(Net::HTTPSuccess) |
| 206 | + body = JSON.parse(response.body) |
| 207 | + expect(body["status"]).to eq("success") |
| 208 | + end |
| 209 | + |
| 210 | + it "successfully processes request with ISO 8601 UTC timestamp using +00:00 format" do |
| 211 | + payload = { text: "Hello, Slack!" } |
| 212 | + iso_timestamp = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S+00:00") |
| 213 | + body = payload.to_json |
| 214 | + signing_payload = "v0:#{iso_timestamp}:#{body}" |
| 215 | + digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_ALT_HMAC_SECRET, signing_payload) |
| 216 | + headers = { |
| 217 | + "Content-Type" => "application/json", |
| 218 | + "Signature-256" => "v0=#{digest}", |
| 219 | + "X-Timestamp" => iso_timestamp |
| 220 | + } |
| 221 | + response = http.post("/webhooks/slack", body, headers) |
| 222 | + expect(response).to be_a(Net::HTTPSuccess) |
| 223 | + body = JSON.parse(response.body) |
| 224 | + expect(body["status"]).to eq("success") |
| 225 | + end |
| 226 | + |
| 227 | + it "rejects request with non-UTC ISO 8601 timestamp" do |
| 228 | + payload = { text: "Hello, Slack!" } |
| 229 | + # Use EST timezone (non-UTC) |
| 230 | + non_utc_timestamp = Time.now.strftime("%Y-%m-%dT%H:%M:%S-05:00") |
| 231 | + |
| 232 | + signing_payload = "v0:#{non_utc_timestamp}:#{payload.to_json}" |
| 233 | + digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_ALT_HMAC_SECRET, signing_payload) |
| 234 | + |
| 235 | + headers = { |
| 236 | + "Content-Type" => "application/json", |
| 237 | + "Signature-256" => "v0=#{digest}", |
| 238 | + "X-Timestamp" => non_utc_timestamp |
| 239 | + } |
| 240 | + |
| 241 | + response = http.post("/webhooks/slack", payload.to_json, headers) |
| 242 | + expect(response).to be_a(Net::HTTPUnauthorized) |
| 243 | + expect(response.body).to include("authentication failed") |
| 244 | + end |
| 245 | + |
| 246 | + it "rejects request with timestamp manipulation attack" do |
| 247 | + payload = { text: "Hello, Slack!" } |
| 248 | + original_timestamp = Time.now.to_i.to_s |
| 249 | + manipulated_timestamp = (Time.now.to_i + 100).to_s # Future timestamp |
| 250 | + |
| 251 | + # Create signature with original timestamp |
| 252 | + signing_payload = "v0:#{original_timestamp}:#{payload.to_json}" |
| 253 | + digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_ALT_HMAC_SECRET, signing_payload) |
| 254 | + |
| 255 | + # But send manipulated timestamp in header |
| 256 | + headers = { |
| 257 | + "Content-Type" => "application/json", |
| 258 | + "Signature-256" => "v0=#{digest}", |
| 259 | + "X-Timestamp" => manipulated_timestamp |
| 260 | + } |
| 261 | + |
| 262 | + response = http.post("/webhooks/slack", payload.to_json, headers) |
126 | 263 | expect(response).to be_a(Net::HTTPUnauthorized)
|
127 | 264 | expect(response.body).to include("authentication failed")
|
128 | 265 | end
|
|
0 commit comments