Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ inbound:
github:
baseUrl: https://github.example.com/api/v3
token: ...
allowCodeAccess: false # default is false, set to true to allow Semgrep to read file contents
```

Under the hood, this config adds these allowlist items:
Expand All @@ -102,6 +103,11 @@ Under the hood, this config adds these allowlist items:
- POST `https://github.example.com/api/v3/repos/:owner/:repo/pulls/:number/comments`
- POST `https://github.example.com/api/v3/repos/:owner/:repo/issues/:number/comments`

And if `allowCodeAccess` is set, additionally:

- GET `https://github.example.com/api/v3/repos/:repo/contents/:filepath`
- GET `https://github.example.com/api/v3/repos/:repo/commits`

### GitLab

Similarly, the `gitlab` configuration section grants Semgrep access to leave MR comments.
Expand All @@ -113,7 +119,7 @@ inbound:
gitlab:
baseUrl: https://gitlab.example.com/api/v4
token: ...
allowCodeAccess: false # default is false, set to true to allow Semgrep to read file contents
allowCodeAccess: false # default is false, set to true to allow Semgrep to read file contents
```

Under the hood, this config adds these allowlist items:
Expand All @@ -123,13 +129,17 @@ Under the hood, this config adds these allowlist items:
- GET `https://gitlab.example.com/api/v4/projects/:project/merge_requests`
- GET `https://gitlab.example.com/api/v4/projects/:project/merge_requests/:number/versions`
- GET `https://gitlab.example.com/api/v4/projects/:project/merge_requests/:number/discussions`
- GET `https://gitlab.example.com/api/v4/projects/:project/repository/branches`
- GET `https://gitlab.example.com/api/v4/:entity_type/:namespace/projects`
- POST `https://gitlab.example.com/api/v4/projects/:project/merge_requests/:number/discussions`
- POST `https://gitlab.example.com/api/v4/projects/:project/merge_requests/:number/discussions/:discussion/notes`
- PUT `https://gitlab.example.com/api/v4/projects/:project/merge_requests/:number/discussions/:discussion/notes/:note`
- PUT `https://gitlab.example.com/api/v4/projects/:project/merge_requests/:number/discussions/:discussion`

And if `allowCodeAccess` is set, additionally:

- GET `https://gitlab.example.com/api/v4/projects/:project/repository/files/:filepath`
- GET `https://gitlab.example.com/api/v4/projects/:project/repository/commits`

### Bitbucket

Expand All @@ -150,6 +160,7 @@ Under the hood, this config adds these allowlist items:
- GET `https://bitbucket.example.com/rest/api/latest/projects/:project/repos/:repo/default-branch`
- GET `https://bitbucket.example.com/rest/api/latest/projects/:project/:repo/pull-requests`
- POST `https://bitbucket.example.com/rest/api/latest/projects/:project/repos/:repo/pull-requests/:number/comments`
- POST `https://bitbucket.example.com/rest/api/latest/projects/:project/repos/:repo/pull-requests/:number/blocker-comments`

### Allowlist

Expand Down
2 changes: 2 additions & 0 deletions pkg/allowlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func TestAllowlistPathMatch(t *testing.T) {
// test path matching
assertAllowlistMatch(t, allowlist, "GET", "https://foo.com/wildcard-path/a", true)
assertAllowlistMatch(t, allowlist, "GET", "https://foo.com/wildcard-path/a/b", true)
assertAllowlistMatch(t, allowlist, "GET", "https://foo.com/wildcard-path/a/b?foo=bar", true)
assertAllowlistMatch(t, allowlist, "GET", "https://foo.com/wildcard-path/a/b?foo=bar#baz", true)
assertAllowlistMatch(t, allowlist, "GET", "https://foo.com/variable-path/a", true)
assertAllowlistMatch(t, allowlist, "GET", "https://foo.com/variable-path/a/b", false)
assertAllowlistMatch(t, allowlist, "GET", "https://foo.com/hardcoded-path", true)
Expand Down
52 changes: 50 additions & 2 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ type HeartbeatConfig struct {
}

type GitHub struct {
BaseURL string `mapstructure:"baseUrl" json:"baseUrl"`
Token string `mapstructure:"token" json:"token"`
BaseURL string `mapstructure:"baseUrl" json:"baseUrl"`
Token string `mapstructure:"token" json:"token"`
AllowCodeAccess bool `mapstructure:"allowCodeAccess" json:"allowCodeAccess"`
}

type GitLab struct {
Expand Down Expand Up @@ -393,6 +394,23 @@ func LoadConfig(configFiles []string, deploymentId int) (*Config, error) {
Methods: ParseHttpMethods([]string{"GET"}),
SetRequestHeaders: headers,
})

if config.Inbound.GitHub.AllowCodeAccess {
config.Inbound.Allowlist = append(config.Inbound.Allowlist,
// get contents of file
AllowlistItem{
URL: gitHubBaseUrl.JoinPath("/repos/:repo/contents/:filepath").String(),
Methods: ParseHttpMethods([]string{"GET"}),
SetRequestHeaders: headers,
},
// Commits
AllowlistItem{
URL: gitHubBaseUrl.JoinPath("/repos/:repo/commits").String(),
Methods: ParseHttpMethods([]string{"GET"}),
SetRequestHeaders: headers,
},
)
}
}

if config.Inbound.GitLab != nil {
Expand Down Expand Up @@ -437,12 +455,30 @@ func LoadConfig(configFiles []string, deploymentId int) (*Config, error) {
Methods: ParseHttpMethods([]string{"GET"}),
SetRequestHeaders: headers,
},
// Projects
AllowlistItem{
URL: gitLabBaseUrl.JoinPath("/:entity_type/:namespace/projects").String(),
Methods: ParseHttpMethods([]string{"GET"}),
SetRequestHeaders: headers,
},
// Branches
AllowlistItem{
URL: gitLabBaseUrl.JoinPath("/projects/:project/repository/branches").String(),
Methods: ParseHttpMethods([]string{"GET"}),
SetRequestHeaders: headers,
},
// post MR comment
AllowlistItem{
URL: gitLabBaseUrl.JoinPath("/projects/:project/merge_requests/:number/discussions").String(),
Methods: ParseHttpMethods([]string{"GET", "POST"}),
SetRequestHeaders: headers,
},
// post MR comment reply
AllowlistItem{
URL: gitLabBaseUrl.JoinPath("/projects/:project/merge_requests/:number/discussions/:discussion/notes").String(),
Methods: ParseHttpMethods([]string{"POST"}),
SetRequestHeaders: headers,
},
// update MR comment
AllowlistItem{
URL: gitLabBaseUrl.JoinPath("/projects/:project/merge_requests/:number/discussions/:discussion/notes/:note").String(),
Expand All @@ -465,6 +501,12 @@ func LoadConfig(configFiles []string, deploymentId int) (*Config, error) {
Methods: ParseHttpMethods([]string{"GET"}),
SetRequestHeaders: headers,
},
// Commits
AllowlistItem{
URL: gitLabBaseUrl.JoinPath("/projects/:project/repository/commits").String(),
Methods: ParseHttpMethods([]string{"GET"}),
SetRequestHeaders: headers,
},
)
}
}
Expand Down Expand Up @@ -524,6 +566,12 @@ func LoadConfig(configFiles []string, deploymentId int) (*Config, error) {
Methods: ParseHttpMethods([]string{"POST"}),
SetRequestHeaders: headers,
},
// post blockerPR comment
AllowlistItem{
URL: bitBucketBaseUrl.JoinPath("/projects/:project/repos/:repo/pull-requests/:number/blocker-comments").String(),
Methods: ParseHttpMethods([]string{"POST"}),
SetRequestHeaders: headers,
},
)
}

Expand Down
Loading