MCPcopy Create free account
hub / github.com/cloudbase/garm / validateHookBody

Method validateHookBody

runner/runner.go:756–797  ·  view source on GitHub ↗
(signature, secret string, body []byte)

Source from the content-addressed store, hash-verified

754}
755
756func (r *Runner) validateHookBody(signature, secret string, body []byte) error {
757 if secret == "" {
758 return runnerErrors.NewMissingSecretError("missing secret to validate webhook signature")
759 }
760
761 if signature == "" {
762 // A secret was set in our config, but a signature was not received
763 // from Github. Authentication of the body cannot be done.
764 return runnerErrors.NewUnauthorizedError("missing github signature")
765 }
766
767 sigParts := strings.SplitN(signature, "=", 2)
768 if len(sigParts) != 2 {
769 // We expect the signature from github to be of the format:
770 // hashType=hashValue
771 // ie: sha256=1fc917c7ad66487470e466c0ad40ddd45b9f7730a4b43e1b2542627f0596bbdc
772 return runnerErrors.NewBadRequestError("invalid signature format")
773 }
774
775 var hashFunc func() hash.Hash
776 switch sigParts[0] {
777 case "sha256":
778 hashFunc = sha256.New
779 case "sha1":
780 hashFunc = sha1.New
781 default:
782 return runnerErrors.NewBadRequestError("unknown signature type")
783 }
784
785 mac := hmac.New(hashFunc, []byte(secret))
786 _, err := mac.Write(body)
787 if err != nil {
788 return fmt.Errorf("failed to compute sha256: %w", err)
789 }
790 expectedMAC := hex.EncodeToString(mac.Sum(nil))
791
792 if !hmac.Equal([]byte(sigParts[1]), []byte(expectedMAC)) {
793 return runnerErrors.NewUnauthorizedError("signature missmatch")
794 }
795
796 return nil
797}
798
799func (r *Runner) findEndpointForJob(job params.WorkflowJob, forgeType params.EndpointType) (params.ForgeEndpoint, error) {
800 uri, err := url.ParseRequestURI(job.WorkflowJob.HTMLURL)

Callers 1

DispatchWorkflowJobMethod · 0.95

Calls 1

WriteMethod · 0.45

Tested by

no test coverage detected