Module: Tripwire::Server::GateDelivery
- Defined in:
- lib/tripwire/server/gate_delivery.rb
Constant Summary collapse
- GATE_DELIVERY_VERSION =
1- GATE_DELIVERY_ALGORITHM =
"x25519-hkdf-sha256/aes-256-gcm"- GATE_AGENT_TOKEN_ENV_SUFFIX =
"_GATE_AGENT_TOKEN"- BLOCKED_GATE_ENV_VAR_KEYS =
%w[ BASH_ENV BROWSER CDPATH DYLD_INSERT_LIBRARIES DYLD_LIBRARY_PATH EDITOR ENV GIT_ASKPASS GIT_SSH_COMMAND HOME LD_LIBRARY_PATH LD_PRELOAD NODE_OPTIONS NODE_PATH PATH PERL5OPT PERLLIB PROMPT_COMMAND PYTHONHOME PYTHONPATH PYTHONSTARTUP RUBYLIB RUBYOPT SHELLOPTS SSH_ASKPASS VISUAL XDG_CONFIG_HOME ].freeze
- BLOCKED_GATE_ENV_VAR_PREFIXES =
%w[NPM_CONFIG_ BUN_CONFIG_ GIT_CONFIG_].freeze
- WEBHOOK_EVENT_TYPES =
%w[ session.fingerprint.calculated session.result.persisted gate.session.approved webhook.test ].freeze
- GATE_DELIVERY_HKDF_INFO =
"tripwire-gate-delivery:v1".b.freeze
- X25519_SPKI_PREFIX =
["302a300506032b656e032100"].pack("H*").freeze
Class Method Summary collapse
- .create_delivery_key_pair ⇒ Object
- .create_encrypted_delivery_response(input) ⇒ Object
- .create_gate_approved_webhook_response(input) ⇒ Object
- .decrypt_gate_delivery_envelope(private_key, envelope) ⇒ Object
- .derive_gate_agent_token_env_key(service_id) ⇒ Object
- .encrypt_gate_delivery_payload(delivery, payload) ⇒ Object
- .export_delivery_private_key_pkcs8(private_key) ⇒ Object
- .import_delivery_private_key_pkcs8(value) ⇒ Object
- .is_blocked_gate_env_var_key(key) ⇒ Object
- .is_gate_managed_env_var_key(key) ⇒ Object
- .key_id_for_raw_x25519_public_key(raw_public_key) ⇒ Object
- .parse_webhook_event(raw_body) ⇒ Object
- .raw_x25519_public_key_from_key_object(public_key) ⇒ Object
- .validate_encrypted_gate_delivery_envelope(value) ⇒ Object
- .validate_gate_approved_webhook_payload(value) ⇒ Object
- .validate_gate_delivery_request(value) ⇒ Object
- .verify_and_parse_webhook_event(secret:, timestamp:, raw_body:, signature:, max_age_seconds: 300, now_seconds: nil) ⇒ Object
- .verify_gate_webhook_signature(secret:, timestamp:, raw_body:, signature:, max_age_seconds: 300, now_seconds: nil) ⇒ Object
Class Method Details
.create_delivery_key_pair ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/tripwire/server/gate_delivery.rb', line 83 def create_delivery_key_pair CryptoSupport.ensure_supported_runtime! private_key = OpenSSL::PKey.generate_key("X25519") raw_public_key = raw_x25519_public_key_from_key_object(private_key.public_key) { delivery: { version: GATE_DELIVERY_VERSION, algorithm: GATE_DELIVERY_ALGORITHM, key_id: key_id_for_raw_x25519_public_key(raw_public_key), public_key: Base64.urlsafe_encode64(raw_public_key, padding: false) }, private_key: private_key } end |
.create_encrypted_delivery_response(input) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/tripwire/server/gate_delivery.rb', line 127 def create_encrypted_delivery_response(input) { encrypted_delivery: encrypt_gate_delivery_payload( input.fetch(:delivery), { version: GATE_DELIVERY_VERSION, outputs: input.fetch(:outputs) } ) } end |
.create_gate_approved_webhook_response(input) ⇒ Object
139 140 141 |
# File 'lib/tripwire/server/gate_delivery.rb', line 139 def create_gate_approved_webhook_response(input) create_encrypted_delivery_response(input) end |
.decrypt_gate_delivery_envelope(private_key, envelope) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/tripwire/server/gate_delivery.rb', line 190 def decrypt_gate_delivery_envelope(private_key, envelope) CryptoSupport.ensure_supported_runtime! validated = validate_encrypted_gate_delivery_envelope(envelope) shared_secret = private_key.derive( OpenSSL::PKey.read(X25519_SPKI_PREFIX + b64url_decode(validated[:ephemeral_public_key], "encrypted_delivery.ephemeral_public_key")) ) key = OpenSSL::KDF.hkdf( shared_secret, salt: b64url_decode(validated[:salt], "encrypted_delivery.salt"), info: GATE_DELIVERY_HKDF_INFO, length: 32, hash: "SHA256" ) cipher = OpenSSL::Cipher.new("aes-256-gcm") cipher.decrypt cipher.key = key cipher.iv = b64url_decode(validated[:iv], "encrypted_delivery.iv") cipher.auth_tag = b64url_decode(validated[:tag], "encrypted_delivery.tag") cipher.auth_data = "" plaintext = cipher.update(b64url_decode(validated[:ciphertext], "encrypted_delivery.ciphertext")) + cipher.final payload = JSON.parse(plaintext) raise ArgumentError, "encrypted_delivery payload must be an object" unless payload.is_a?(Hash) symbolize(payload).tap do |candidate| raise ArgumentError, "encrypted_delivery payload version must be 1" unless candidate[:version] == GATE_DELIVERY_VERSION raise ArgumentError, "encrypted_delivery payload outputs must be an object" unless candidate[:outputs].is_a?(Hash) candidate[:outputs].each do |key_name, item| raise ArgumentError, "encrypted_delivery output #{key_name} must be a string" unless item.is_a?(String) end end rescue JSON::ParserError raise ArgumentError, "encrypted_delivery decrypted to invalid JSON" end |
.derive_gate_agent_token_env_key(service_id) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/tripwire/server/gate_delivery.rb', line 53 def derive_gate_agent_token_env_key(service_id) normalized = service_id.to_s.strip.gsub(/[^A-Za-z0-9]+/, "_").gsub(/^_+|_+$/, "").gsub(/_+/, "_").upcase raise ArgumentError, "service_id is required to derive a Gate agent token env key" if normalized.empty? "#{normalized}#{GATE_AGENT_TOKEN_ENV_SUFFIX}" end |
.encrypt_gate_delivery_payload(delivery, payload) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/tripwire/server/gate_delivery.rb', line 158 def encrypt_gate_delivery_payload(delivery, payload) CryptoSupport.ensure_supported_runtime! validated_delivery = validate_gate_delivery_request(delivery) payload = symbolize(payload) raise ArgumentError, "Gate delivery payload version must be 1" unless payload[:version] == GATE_DELIVERY_VERSION recipient_public_key = OpenSSL::PKey.read(X25519_SPKI_PREFIX + b64url_decode(validated_delivery[:public_key], "delivery.public_key")) ephemeral_private_key = OpenSSL::PKey.generate_key("X25519") shared_secret = ephemeral_private_key.derive(recipient_public_key) salt = OpenSSL::Random.random_bytes(32) iv = OpenSSL::Random.random_bytes(12) key = OpenSSL::KDF.hkdf(shared_secret, salt: salt, info: GATE_DELIVERY_HKDF_INFO, length: 32, hash: "SHA256") cipher = OpenSSL::Cipher.new("aes-256-gcm") cipher.encrypt cipher.key = key cipher.iv = iv ciphertext = cipher.update(JSON.generate(compact_payload(payload))) + cipher.final tag = cipher.auth_tag { version: GATE_DELIVERY_VERSION, algorithm: GATE_DELIVERY_ALGORITHM, key_id: validated_delivery[:key_id], ephemeral_public_key: Base64.urlsafe_encode64(raw_x25519_public_key_from_key_object(ephemeral_private_key.public_key), padding: false), salt: Base64.urlsafe_encode64(salt, padding: false), iv: Base64.urlsafe_encode64(iv, padding: false), ciphertext: Base64.urlsafe_encode64(ciphertext, padding: false), tag: Base64.urlsafe_encode64(tag, padding: false) } end |
.export_delivery_private_key_pkcs8(private_key) ⇒ Object
98 99 100 101 |
# File 'lib/tripwire/server/gate_delivery.rb', line 98 def export_delivery_private_key_pkcs8(private_key) CryptoSupport.ensure_supported_runtime! Base64.urlsafe_encode64(private_key.private_to_der, padding: false) end |
.import_delivery_private_key_pkcs8(value) ⇒ Object
103 104 105 106 |
# File 'lib/tripwire/server/gate_delivery.rb', line 103 def import_delivery_private_key_pkcs8(value) CryptoSupport.ensure_supported_runtime! OpenSSL::PKey.read(b64url_decode(value, "delivery.private_key_pkcs8")) end |
.is_blocked_gate_env_var_key(key) ⇒ Object
64 65 66 67 |
# File 'lib/tripwire/server/gate_delivery.rb', line 64 def is_blocked_gate_env_var_key(key) normalized = key.to_s.strip.upcase BLOCKED_GATE_ENV_VAR_KEYS.include?(normalized) || BLOCKED_GATE_ENV_VAR_PREFIXES.any? { |prefix| normalized.start_with?(prefix) } end |
.is_gate_managed_env_var_key(key) ⇒ Object
60 61 62 |
# File 'lib/tripwire/server/gate_delivery.rb', line 60 def is_gate_managed_env_var_key(key) key == "TRIPWIRE_AGENT_TOKEN" || key.to_s.end_with?(GATE_AGENT_TOKEN_ENV_SUFFIX) end |
.key_id_for_raw_x25519_public_key(raw_public_key) ⇒ Object
77 78 79 80 81 |
# File 'lib/tripwire/server/gate_delivery.rb', line 77 def key_id_for_raw_x25519_public_key(raw_public_key) raise ArgumentError, "X25519 public key must be 32 bytes" unless raw_public_key.bytesize == 32 Base64.urlsafe_encode64(Digest::SHA256.digest(raw_public_key), padding: false) end |
.parse_webhook_event(raw_body) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/tripwire/server/gate_delivery.rb', line 263 def parse_webhook_event(raw_body) envelope = symbolize(JSON.parse(raw_body)) raise ArgumentError, "webhook event object must be webhook_event" unless envelope[:object] == "webhook_event" raise ArgumentError, "webhook event id is required" if envelope[:id].to_s.empty? raise ArgumentError, "webhook event type is required" if envelope[:type].to_s.empty? raise ArgumentError, "unsupported webhook event type: #{envelope[:type]}" unless WEBHOOK_EVENT_TYPES.include?(envelope[:type]) raise ArgumentError, "webhook event created timestamp is required" if envelope[:created].to_s.empty? raise ArgumentError, "webhook event data must be an object" unless envelope[:data].is_a?(Hash) envelope[:data] = validate_gate_approved_webhook_payload(envelope[:data]) if envelope[:type] == "gate.session.approved" envelope end |
.raw_x25519_public_key_from_key_object(public_key) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/tripwire/server/gate_delivery.rb', line 69 def raw_x25519_public_key_from_key_object(public_key) der = public_key.public_to_der raise ArgumentError, "Unexpected X25519 public key encoding" unless der.bytesize == X25519_SPKI_PREFIX.bytesize + 32 raise ArgumentError, "Unexpected X25519 public key prefix" unless der.byteslice(0, X25519_SPKI_PREFIX.bytesize) == X25519_SPKI_PREFIX der.byteslice(X25519_SPKI_PREFIX.bytesize, 32) end |
.validate_encrypted_gate_delivery_envelope(value) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/tripwire/server/gate_delivery.rb', line 143 def validate_encrypted_gate_delivery_envelope(value) candidate = symbolize(value) raise ArgumentError, "encrypted_delivery.version must be 1" unless candidate[:version] == GATE_DELIVERY_VERSION raise ArgumentError, "encrypted_delivery.algorithm must be #{GATE_DELIVERY_ALGORITHM}" unless candidate[:algorithm] == GATE_DELIVERY_ALGORITHM %i[key_id ephemeral_public_key salt iv ciphertext tag].each do |field| raise ArgumentError, "encrypted_delivery.#{field} is required" if candidate[field].to_s.empty? end raise ArgumentError, "encrypted_delivery.ephemeral_public_key must be 32 bytes" unless b64url_decode(candidate[:ephemeral_public_key], "encrypted_delivery.ephemeral_public_key").bytesize == 32 raise ArgumentError, "encrypted_delivery.salt must be 32 bytes" unless b64url_decode(candidate[:salt], "encrypted_delivery.salt").bytesize == 32 raise ArgumentError, "encrypted_delivery.iv must be 12 bytes" unless b64url_decode(candidate[:iv], "encrypted_delivery.iv").bytesize == 12 raise ArgumentError, "encrypted_delivery.tag must be 16 bytes" unless b64url_decode(candidate[:tag], "encrypted_delivery.tag").bytesize == 16 candidate end |
.validate_gate_approved_webhook_payload(value) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/tripwire/server/gate_delivery.rb', line 224 def validate_gate_approved_webhook_payload(value) candidate = symbolize(value) raise ArgumentError, "webhook payload must not include event; use the webhook event envelope type" if candidate.key?(:event) raise ArgumentError, "service_id is required" if candidate[:service_id].to_s.empty? raise ArgumentError, "gate_session_id is required" if candidate[:gate_session_id].to_s.empty? raise ArgumentError, "gate_account_id is required" if candidate[:gate_account_id].to_s.empty? raise ArgumentError, "account_name is required" if candidate[:account_name].to_s.empty? raise ArgumentError, "metadata must be an object or null" unless candidate[:metadata].nil? || candidate[:metadata].is_a?(Hash) raise ArgumentError, "tripwire must be an object" unless candidate[:tripwire].is_a?(Hash) verdict = candidate[:tripwire][:verdict] raise ArgumentError, "tripwire.verdict is invalid" unless %w[bot human inconclusive].include?(verdict) score = candidate[:tripwire][:score] raise ArgumentError, "tripwire.score must be a number or null" unless score.nil? || score.is_a?(Numeric) { service_id: candidate[:service_id], gate_session_id: candidate[:gate_session_id], gate_account_id: candidate[:gate_account_id], account_name: candidate[:account_name], metadata: candidate[:metadata]&.dup, tripwire: { verdict: verdict, score: score }, delivery: validate_gate_delivery_request(candidate[:delivery]) } end |
.validate_gate_delivery_request(value) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/tripwire/server/gate_delivery.rb', line 108 def validate_gate_delivery_request(value) candidate = symbolize(value) raise ArgumentError, "delivery.version must be 1" unless candidate[:version] == GATE_DELIVERY_VERSION raise ArgumentError, "delivery.algorithm must be #{GATE_DELIVERY_ALGORITHM}" unless candidate[:algorithm] == GATE_DELIVERY_ALGORITHM raise ArgumentError, "delivery.public_key is required" if candidate[:public_key].to_s.empty? raise ArgumentError, "delivery.key_id is required" if candidate[:key_id].to_s.empty? raw_public_key = b64url_decode(candidate[:public_key], "delivery.public_key") raise ArgumentError, "delivery.public_key must be a raw X25519 public key" unless raw_public_key.bytesize == 32 raise ArgumentError, "delivery.key_id does not match delivery.public_key" unless key_id_for_raw_x25519_public_key(raw_public_key) == candidate[:key_id] { version: GATE_DELIVERY_VERSION, algorithm: GATE_DELIVERY_ALGORITHM, key_id: candidate[:key_id], public_key: candidate[:public_key] } end |
.verify_and_parse_webhook_event(secret:, timestamp:, raw_body:, signature:, max_age_seconds: 300, now_seconds: nil) ⇒ Object
276 277 278 279 280 281 |
# File 'lib/tripwire/server/gate_delivery.rb', line 276 def verify_and_parse_webhook_event(secret:, timestamp:, raw_body:, signature:, max_age_seconds: 300, now_seconds: nil) unless verify_gate_webhook_signature(secret: secret, timestamp: , raw_body: raw_body, signature: signature, max_age_seconds: max_age_seconds, now_seconds: now_seconds) raise ArgumentError, "Invalid Tripwire webhook signature" end parse_webhook_event(raw_body) end |
.verify_gate_webhook_signature(secret:, timestamp:, raw_body:, signature:, max_age_seconds: 300, now_seconds: nil) ⇒ Object
252 253 254 255 256 257 258 259 260 261 |
# File 'lib/tripwire/server/gate_delivery.rb', line 252 def verify_gate_webhook_signature(secret:, timestamp:, raw_body:, signature:, max_age_seconds: 300, now_seconds: nil) = Integer() current = now_seconds || Time.now.to_i return false if (current - ).abs > max_age_seconds expected = OpenSSL::HMAC.hexdigest("SHA256", secret, "#{}.#{raw_body}") secure_compare(expected, signature) rescue ArgumentError false end |