Skip to content

Commit ad21053

Browse files
Copilotskarim
andauthored
Guard GraphQL PR number conversion against int32 overflow (#56)
* Initial plan * fix: validate int range before GraphQL Int conversion Agent-Logs-Url: https://github.com/github/gh-stack/sessions/dbb2b50f-34fb-4957-ac08-e19c1f96ba41 Co-authored-by: skarim <1701557+skarim@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: skarim <1701557+skarim@users.noreply.github.com>
1 parent a853a21 commit ad21053

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

internal/github/github.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"math"
78

89
"github.com/cli/go-gh/v2/pkg/api"
910
graphql "github.com/cli/shurcooL-graphql"
@@ -319,6 +320,11 @@ func (c *Client) FindPRDetailsForBranch(branch string) (*PRDetails, error) {
319320

320321
// FindPRByNumber fetches a pull request by its number.
321322
func (c *Client) FindPRByNumber(number int) (*PullRequest, error) {
323+
gqlNumber, err := toGraphQLInt(number)
324+
if err != nil {
325+
return nil, err
326+
}
327+
322328
var query struct {
323329
Repository struct {
324330
PullRequest struct {
@@ -339,7 +345,7 @@ func (c *Client) FindPRByNumber(number int) (*PullRequest, error) {
339345
variables := map[string]interface{}{
340346
"owner": graphql.String(c.owner),
341347
"name": graphql.String(c.repo),
342-
"number": graphql.Int(number),
348+
"number": gqlNumber,
343349
}
344350

345351
if err := c.gql.Query("FindPRByNumber", &query, variables); err != nil {
@@ -364,6 +370,13 @@ func (c *Client) FindPRByNumber(number int) (*PullRequest, error) {
364370
}, nil
365371
}
366372

373+
func toGraphQLInt(n int) (graphql.Int, error) {
374+
if n < math.MinInt32 || n > math.MaxInt32 {
375+
return 0, fmt.Errorf("number %d is out of GraphQL Int range", n)
376+
}
377+
return graphql.Int(n), nil
378+
}
379+
367380
type RemoteStack struct {
368381
ID int `json:"id"`
369382
PullRequests []int `json:"pull_requests"`

internal/github/github_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"testing"
55

6+
graphql "github.com/cli/shurcooL-graphql"
67
"github.com/stretchr/testify/assert"
78
)
89

@@ -46,3 +47,16 @@ func TestPullRequest_IsQueued(t *testing.T) {
4647
assert.False(t, pr.IsQueued())
4748
})
4849
}
50+
51+
func TestToGraphQLInt(t *testing.T) {
52+
t.Run("in range", func(t *testing.T) {
53+
got, err := toGraphQLInt(123)
54+
assert.NoError(t, err)
55+
assert.Equal(t, graphql.Int(123), got)
56+
})
57+
58+
t.Run("out of range", func(t *testing.T) {
59+
_, err := toGraphQLInt(1 << 40)
60+
assert.Error(t, err)
61+
})
62+
}

0 commit comments

Comments
 (0)