Skip to content

Commit a56985f

Browse files
Merge pull request #5057 from psrsingh/fix-github-return-url-validation
fix: validate github return url metadata
2 parents 0a747f2 + 3a1b0e8 commit a56985f

2 files changed

Lines changed: 78 additions & 17 deletions

File tree

cla-backend-go/github/github_repository.go

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,28 +2597,76 @@ func GetReturnURL(ctx context.Context, installationID, repositoryID int64, pullR
25972597
"pullRequestID": pullRequestID,
25982598
}
25992599

2600-
client, err := NewGithubAppClient(installationID)
2600+
if installationID <= 0 {
2601+
err := errors.New("invalid installation ID")
2602+
log.WithFields(f).WithError(err).Warn("invalid installation ID")
2603+
return "", err
2604+
}
2605+
if repositoryID <= 0 {
2606+
err := errors.New("invalid repository ID")
2607+
log.WithFields(f).WithError(err).Warn("invalid repository ID")
2608+
return "", err
2609+
}
2610+
if pullRequestID <= 0 {
2611+
err := errors.New("invalid pull request ID")
2612+
log.WithFields(f).WithError(err).Warn("invalid pull request ID")
2613+
return "", err
2614+
}
26012615

2616+
client, err := NewGithubAppClient(installationID)
26022617
if err != nil {
26032618
log.WithFields(f).WithError(err).Warn("unable to create Github client")
26042619
return "", err
26052620
}
26062621

26072622
log.WithFields(f).Debugf("getting github repository by id: %d", repositoryID)
2608-
repo, _, err := client.Repositories.GetByID(ctx, repositoryID)
2623+
repo, resp, err := client.Repositories.GetByID(ctx, repositoryID)
26092624
if err != nil {
2625+
if ok, wrapped := CheckAndWrapForKnownErrors(resp, err); ok {
2626+
log.WithFields(f).WithError(wrapped).Warnf("unable to get repository by ID: %d", repositoryID)
2627+
return "", wrapped
2628+
}
26102629
log.WithFields(f).WithError(err).Warnf("unable to get repository by ID: %d", repositoryID)
26112630
return "", err
26122631
}
2632+
if repo == nil {
2633+
err := fmt.Errorf("missing repository for repository ID %d", repositoryID)
2634+
log.WithFields(f).WithError(err).Warn("invalid repository metadata")
2635+
return "", err
2636+
}
2637+
2638+
owner := repo.GetOwner().GetLogin()
2639+
name := repo.GetName()
2640+
if owner == "" || name == "" {
2641+
err := fmt.Errorf("invalid repository owner/name for repository ID %d", repositoryID)
2642+
log.WithFields(f).WithError(err).Warn("invalid repository metadata")
2643+
return "", err
2644+
}
26132645

26142646
log.WithFields(f).Debugf("getting pull request by id: %d", pullRequestID)
2615-
pullRequest, _, err := client.PullRequests.Get(ctx, *repo.Owner.Login, *repo.Name, pullRequestID)
2647+
pullRequest, resp, err := client.PullRequests.Get(ctx, owner, name, pullRequestID)
26162648
if err != nil {
2649+
if ok, wrapped := CheckAndWrapForKnownErrors(resp, err); ok {
2650+
log.WithFields(f).WithError(wrapped).Warnf("unable to get pull request by ID: %d", pullRequestID)
2651+
return "", wrapped
2652+
}
26172653
log.WithFields(f).WithError(err).Warnf("unable to get pull request by ID: %d", pullRequestID)
26182654
return "", err
26192655
}
2656+
if pullRequest == nil {
2657+
err := fmt.Errorf("missing pull request %d/%s/%s", pullRequestID, owner, name)
2658+
log.WithFields(f).WithError(err).Warn("invalid pull request metadata")
2659+
return "", err
2660+
}
2661+
2662+
htmlURL := pullRequest.GetHTMLURL()
2663+
if htmlURL == "" {
2664+
err := fmt.Errorf("missing html url for pull request %d/%s/%s", pullRequestID, owner, name)
2665+
log.WithFields(f).WithError(err).Warn("invalid pull request metadata")
2666+
return "", err
2667+
}
26202668

2621-
log.WithFields(f).Debugf("returning pull request html url: %s", *pullRequest.HTMLURL)
2669+
log.WithFields(f).Debugf("returning pull request html url: %s", htmlURL)
26222670

2623-
return *pullRequest.HTMLURL, nil
2671+
return htmlURL, nil
26242672
}

cla-backend-go/v2/sign/service.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,22 @@ func getUserName(user *v1Models.User) string {
15501550
return ""
15511551
}
15521552

1553+
// metadataStringValue extracts a string value from metadata and validates it.
1554+
func metadataStringValue(metadata map[string]interface{}, key string) (string, error) {
1555+
if metadata == nil {
1556+
return "", fmt.Errorf("missing %s in metadata", key)
1557+
}
1558+
v, ok := metadata[key]
1559+
if !ok || v == nil {
1560+
return "", fmt.Errorf("missing %s in metadata", key)
1561+
}
1562+
s := strings.TrimSpace(fmt.Sprintf("%v", v))
1563+
if s == "" || s == "<nil>" {
1564+
return "", fmt.Errorf("missing %s in metadata", key)
1565+
}
1566+
return s, nil
1567+
}
1568+
15531569
func (s *service) getIndividualSignatureCallbackURLGitlab(ctx context.Context, userID string, metadata map[string]interface{}) (string, error) {
15541570
f := logrus.Fields{
15551571
"functionName": "sign.getIndividualSignatureCallbackURLGitlab",
@@ -1569,16 +1585,14 @@ func (s *service) getIndividualSignatureCallbackURLGitlab(ctx context.Context, u
15691585
}
15701586
}
15711587

1572-
if found, ok := metadata["repository_id"].(string); ok {
1573-
repositoryID = found
1574-
} else {
1588+
repositoryID, err = metadataStringValue(metadata, "repository_id")
1589+
if err != nil {
15751590
log.WithFields(f).WithError(err).Warnf("unable to get repository ID for user: %s", userID)
15761591
return "", err
15771592
}
15781593

1579-
if found, ok := metadata["merge_request_id"].(string); ok {
1580-
mergeRequestID = found
1581-
} else {
1594+
mergeRequestID, err = metadataStringValue(metadata, "merge_request_id")
1595+
if err != nil {
15821596
log.WithFields(f).WithError(err).Warnf("unable to get merge request ID for user: %s", userID)
15831597
return "", err
15841598
}
@@ -1599,6 +1613,7 @@ func (s *service) getIndividualSignatureCallbackURLGitlab(ctx context.Context, u
15991613
}
16001614

16011615
if gitlabOrg.OrganizationID == "" {
1616+
err = errors.New("missing organization ID for GitLab repository")
16021617
log.WithFields(f).WithError(err).Warnf("unable to get organization ID for repository ID: %s", repositoryID)
16031618
return "", err
16041619
}
@@ -1628,18 +1643,16 @@ func (s *service) getIndividualSignatureCallbackURL(ctx context.Context, userID
16281643
}
16291644
}
16301645

1631-
if found, ok := metadata["repository_id"].(string); ok {
1632-
repositoryID = found
1633-
} else {
1646+
repositoryID, err = metadataStringValue(metadata, "repository_id")
1647+
if err != nil {
16341648
log.WithFields(f).WithError(err).Warnf("unable to get repository ID for user: %s", userID)
16351649
return "", err
16361650
}
16371651

16381652
log.WithFields(f).Debugf("found repository ID: %s", repositoryID)
16391653

1640-
if found, ok := metadata["pull_request_id"].(string); ok {
1641-
pullRequestID = found
1642-
} else {
1654+
pullRequestID, err = metadataStringValue(metadata, "pull_request_id")
1655+
if err != nil {
16431656
log.WithFields(f).WithError(err).Warnf("unable to get pull request ID for user: %s", userID)
16441657
return "", err
16451658
}

0 commit comments

Comments
 (0)