82 lines
3.3 KiB
Ruby
82 lines
3.3 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe InternalSignatureService do
|
|
let(:secret) { "test-secret-key-base" }
|
|
|
|
it "produces a stable, cross-implementation signature" do
|
|
# Vectors are shared with the game-server (Go) test suite via
|
|
# contracts/test_signature_vectors.json — the single source of truth for
|
|
# the wire format. Do not change the formula without updating both sides.
|
|
vectors_file = Rails.root.join("..", "contracts", "test_signature_vectors.json")
|
|
vectors = JSON.parse(File.read(vectors_file))["vectors"]
|
|
expect(vectors).not_to be_empty
|
|
|
|
vectors.each do |vector|
|
|
expect(described_class.sign(vector["body"], vector["path"],
|
|
secret: vector["secret"], timestamp: vector["timestamp"]))
|
|
.to eq(vector["signature_hex"]),
|
|
"signature vector #{vector["name"].inspect} does not match"
|
|
end
|
|
end
|
|
|
|
describe ".valid?" do
|
|
def request_double(path: "/api/internal/matches/abc/result",
|
|
body: '{"status":"player1_won","duration_seconds":42}',
|
|
headers: {})
|
|
request = instance_double("ActionDispatch::Http::Request")
|
|
allow(request).to receive(:path).and_return(path)
|
|
allow(request).to receive(:raw_post).and_return(body)
|
|
allow(request).to receive(:headers).and_return(headers)
|
|
request
|
|
end
|
|
|
|
def valid_headers(secret: "test-secret-key-base", path: "/api/internal/matches/abc/result",
|
|
body: '{"status":"player1_won","duration_seconds":42}', timestamp: Time.current.to_i)
|
|
signature = described_class.sign(body, path, secret: secret, timestamp: timestamp)
|
|
{
|
|
"X-Request-Timestamp" => timestamp.to_s,
|
|
"X-Request-Signature" => "sha256=#{signature}"
|
|
}
|
|
end
|
|
|
|
it "accepts a correctly signed request" do
|
|
headers = valid_headers
|
|
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(true)
|
|
end
|
|
|
|
it "accepts a signature without the sha256= prefix" do
|
|
headers = valid_headers
|
|
headers["X-Request-Signature"] = headers["X-Request-Signature"].delete_prefix("sha256=")
|
|
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(true)
|
|
end
|
|
|
|
it "rejects missing headers" do
|
|
expect(described_class.valid?(request_double(headers: {}), secret: secret)).to be(false)
|
|
end
|
|
|
|
it "rejects a non-numeric timestamp" do
|
|
headers = valid_headers
|
|
headers["X-Request-Timestamp"] = "not-a-number"
|
|
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(false)
|
|
end
|
|
|
|
it "rejects a wrong signature" do
|
|
headers = valid_headers
|
|
headers["X-Request-Signature"] = "sha256=#{"a" * 64}"
|
|
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(false)
|
|
end
|
|
|
|
it "rejects a timestamp too far in the past" do
|
|
timestamp = (Time.current - described_class::TOLERANCE - 60).to_i
|
|
headers = valid_headers(timestamp: timestamp)
|
|
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(false)
|
|
end
|
|
|
|
it "rejects a timestamp too far in the future" do
|
|
timestamp = (Time.current + described_class::TOLERANCE + 60).to_i
|
|
headers = valid_headers(timestamp: timestamp)
|
|
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(false)
|
|
end
|
|
end
|
|
end
|