File tree Expand file tree Collapse file tree 2 files changed +17
-10
lines changed Expand file tree Collapse file tree 2 files changed +17
-10
lines changed Original file line number Diff line number Diff line change @@ -211,11 +211,18 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
211
211
| _ -> Lwt. return @@ Ok ()
212
212
213
213
let process_github_notification (ctx : Context.t ) headers body =
214
+ let validate_signature secrets =
215
+ let signing_key = secrets.gh_hook_token in
216
+ Github. validate_signature ?signing_key ~headers body
217
+ in
214
218
try % lwt
215
219
let secrets = Context. get_secrets_exn ctx in
216
- match Github. parse_exn ~secret: secrets.gh_hook_token headers body with
220
+ match Github. parse_exn headers body with
217
221
| exception exn -> Exn_lwt. fail ~exn " failed to parse payload"
218
222
| payload ->
223
+ match validate_signature secrets with
224
+ | Error e -> action_error e
225
+ | Ok () ->
219
226
( match % lwt refresh_repo_config ctx payload with
220
227
| Error e -> action_error e
221
228
| Ok () ->
Original file line number Diff line number Diff line change @@ -64,16 +64,16 @@ let is_valid_signature ~secret headers_sig body =
64
64
let (`Hex request_hash) = Hex. of_string request_hash in
65
65
String. equal headers_sig (sprintf " sha1=%s" request_hash)
66
66
67
+ let validate_signature ?signing_key ~headers body =
68
+ match signing_key with
69
+ | None -> Ok ()
70
+ | Some secret ->
71
+ match List.Assoc. find headers " x-hub-signature" ~equal: String. equal with
72
+ | None -> Error " unable to find header x-hub-signature"
73
+ | Some signature -> if is_valid_signature ~secret signature body then Ok () else Error " signatures don't match"
74
+
67
75
(* Parse a payload. The type of the payload is detected from the headers. *)
68
- let parse_exn ~secret headers body =
69
- begin
70
- match secret with
71
- | None -> ()
72
- | Some secret ->
73
- match List.Assoc. find headers " x-hub-signature" ~equal: String. equal with
74
- | None -> Exn. fail " unable to find header x-hub-signature"
75
- | Some req_sig -> if not @@ is_valid_signature ~secret req_sig body then failwith " request signature invalid"
76
- end ;
76
+ let parse_exn headers body =
77
77
match List.Assoc. find_exn headers " x-github-event" ~equal: String. equal with
78
78
| exception exn -> Exn. fail ~exn " unable to read x-github-event"
79
79
| "push" -> Push (commit_pushed_notification_of_string body)
You can’t perform that action at this time.
0 commit comments