|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require_relative "../spec_helper" |
| 3 | +FAKE_HMAC_SECRET = "octoawesome-secret" |
| 4 | +FAKE_ALT_HMAC_SECRET = "octoawesome-2-secret" |
4 | 5 |
|
5 | 6 | require "rspec" |
6 | 7 | require "net/http" |
|
90 | 91 | expect(response.body).to include("request validation failed") |
91 | 92 | end |
92 | 93 |
|
| 94 | + it "receives a POST request but it uses the wrong algo" do |
| 95 | + payload = { action: "push", repository: { name: "test-repo" } } |
| 96 | + headers = { |
| 97 | + "Content-Type" => "application/json", |
| 98 | + "X-Hub-Signature-256" => "sha512=" + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha512"), FAKE_HMAC_SECRET, payload.to_json) |
| 99 | + } |
| 100 | + response = http.post("/webhooks/github", payload.to_json, headers) |
| 101 | + expect(response).to be_a(Net::HTTPUnauthorized) |
| 102 | + expect(response.body).to include("request validation failed") |
| 103 | + end |
| 104 | + |
93 | 105 | it "successfully processes a valid POST request with HMAC signature" do |
94 | 106 | payload = { action: "push", repository: { name: "test-repo" } } |
95 | 107 | headers = { |
|
102 | 114 | expect(body["status"]).to eq("success") |
103 | 115 | end |
104 | 116 | end |
| 117 | + |
| 118 | + describe "slack" do |
| 119 | + it "receives a POST request but contains an invalid HMAC signature" do |
| 120 | + payload = { text: "Hello, Slack!" } |
| 121 | + digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), FAKE_ALT_HMAC_SECRET, payload.to_json) |
| 122 | + headers = { "Content-Type" => "application/json", "Signature-256" => "sha256=#{digest}" } |
| 123 | + response = http.post("/webhooks/slack", payload.to_json, headers) |
| 124 | + |
| 125 | + expect(response).to be_a(Net::HTTPUnauthorized) |
| 126 | + expect(response.body).to include("request validation failed") |
| 127 | + end |
| 128 | + end |
105 | 129 | end |
106 | 130 | end |
0 commit comments