-
Notifications
You must be signed in to change notification settings - Fork 112
veles: add SendGrid API key detector and validator #1654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
copybara-service
merged 7 commits into
google:main
from
Alearner12:add-sendgrid-detector
Feb 13, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b86518a
veles: add SendGrid API key detector and validator
Alearner12 0c873b8
Register SendGrid detector and validator in plugin lists
Alearner12 e3ab0bd
Merge remote-tracking branch 'origin/main' into add-sendgrid-detector
Alearner12 3661d47
Address PR review feedback for SendGrid detector
Alearner12 a517876
Merge branch 'main' into add-sendgrid-detector
Alearner12 0ba2d06
Fix lint errors and regenerate proto with SendGrid types
Alearner12 32acc9c
Merge branch 'main' into add-sendgrid-detector
erikvarga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package sendgrid | ||
|
|
||
| import ( | ||
| "regexp" | ||
|
|
||
| "github.com/google/osv-scalibr/veles" | ||
| "github.com/google/osv-scalibr/veles/secrets/common/simpletoken" | ||
| ) | ||
|
|
||
| var ( | ||
| // Ensure the constructor satisfies the interface at compile time. | ||
| _ veles.Detector = NewDetector() | ||
| ) | ||
|
|
||
| // SendGrid API keys are exactly 69 characters: SG.<22 chars>.<43 chars> | ||
| const maxKeyLen = 69 | ||
|
|
||
| var keyRe = regexp.MustCompile(`SG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}`) | ||
|
|
||
| // NewDetector returns a detector for SendGrid API keys (SG.xxx.yyy). | ||
| func NewDetector() veles.Detector { | ||
| return simpletoken.Detector{ | ||
| MaxLen: maxKeyLen, | ||
| Re: keyRe, | ||
| FromMatch: func(b []byte) (veles.Secret, bool) { | ||
| return APIKey{Key: string(b)}, true | ||
| }, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package sendgrid_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "github.com/google/osv-scalibr/veles" | ||
| "github.com/google/osv-scalibr/veles/secrets/sendgrid" | ||
| "github.com/google/osv-scalibr/veles/velestest" | ||
| ) | ||
|
|
||
| // Fake SendGrid API keys for testing purposes. | ||
| // These are NOT real keys and will not work with the SendGrid API. | ||
| // They follow the correct format: SG.<22 chars>.<43 chars> = 69 total characters. | ||
| const testSendGridAPIKey = "SG.aaaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" | ||
| const testSendGridAPIKey2 = "SG.XXXXXXXXXXXXXXXXXXXXXX.YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" | ||
| const testSendGridAPIKeyWithSpecialChars = "SG.abc_def-ghij12345678ab.ABC_DEF-GHIJKLMNOPQRSTUVWXYZabcdefghijk1234" | ||
|
|
||
| func TestDetectorAcceptance(t *testing.T) { | ||
| velestest.AcceptDetector( | ||
| t, | ||
| sendgrid.NewDetector(), | ||
| testSendGridAPIKey, | ||
| sendgrid.APIKey{Key: testSendGridAPIKey}, | ||
| ) | ||
| } | ||
|
|
||
| // TestDetector_truePositives tests for cases where we know the Detector | ||
| // will find SendGrid API keys. | ||
| func TestDetector_truePositives(t *testing.T) { | ||
| engine, err := veles.NewDetectionEngine([]veles.Detector{ | ||
| sendgrid.NewDetector(), | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cases := []struct { | ||
| name string | ||
| input string | ||
| want []veles.Secret | ||
| }{{ | ||
| name: "simple matching string", | ||
| input: testSendGridAPIKey, | ||
| want: []veles.Secret{ | ||
| sendgrid.APIKey{Key: testSendGridAPIKey}, | ||
| }, | ||
| }, { | ||
| name: "key with underscores and dashes", | ||
| input: testSendGridAPIKeyWithSpecialChars, | ||
| want: []veles.Secret{ | ||
| sendgrid.APIKey{Key: testSendGridAPIKeyWithSpecialChars}, | ||
| }, | ||
| }, { | ||
| name: "match at end of string", | ||
| input: "SENDGRID_API_KEY=" + testSendGridAPIKey, | ||
| want: []veles.Secret{ | ||
| sendgrid.APIKey{Key: testSendGridAPIKey}, | ||
| }, | ||
| }, { | ||
| name: "match in middle of string", | ||
| input: `api_key="` + testSendGridAPIKey + `"`, | ||
| want: []veles.Secret{ | ||
| sendgrid.APIKey{Key: testSendGridAPIKey}, | ||
| }, | ||
| }, { | ||
| name: "multiple matches", | ||
| input: testSendGridAPIKey + "\n" + testSendGridAPIKey2, | ||
| want: []veles.Secret{ | ||
| sendgrid.APIKey{Key: testSendGridAPIKey}, | ||
| sendgrid.APIKey{Key: testSendGridAPIKey2}, | ||
| }, | ||
| }, { | ||
| name: "key in JSON format", | ||
| input: `{"sendgrid_api_key": "` + testSendGridAPIKey + `"}`, | ||
| want: []veles.Secret{ | ||
| sendgrid.APIKey{Key: testSendGridAPIKey}, | ||
| }, | ||
| }, { | ||
| name: "key in environment variable style", | ||
| input: `export SENDGRID_API_KEY="` + testSendGridAPIKey + `"`, | ||
| want: []veles.Secret{ | ||
| sendgrid.APIKey{Key: testSendGridAPIKey}, | ||
| }, | ||
| }, { | ||
| name: "larger input containing key", | ||
| input: fmt.Sprintf(` | ||
| :test_api_key: do-test | ||
| :SENDGRID_API_KEY: %s | ||
| `, testSendGridAPIKey), | ||
| want: []veles.Secret{ | ||
| sendgrid.APIKey{Key: testSendGridAPIKey}, | ||
| }, | ||
| }, { | ||
| name: "key followed by extra characters", | ||
| input: testSendGridAPIKey + "extra", | ||
| want: []veles.Secret{ | ||
| sendgrid.APIKey{Key: testSendGridAPIKey}, | ||
| }, | ||
| }} | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got, err := engine.Detect(t.Context(), strings.NewReader(tc.input)) | ||
| if err != nil { | ||
| t.Errorf("Detect() error: %v, want nil", err) | ||
| } | ||
| if diff := cmp.Diff(tc.want, got, cmpopts.EquateEmpty()); diff != "" { | ||
| t.Errorf("Detect() diff (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestDetector_trueNegatives tests for cases where we know the Detector | ||
| // will not find SendGrid API keys. | ||
| func TestDetector_trueNegatives(t *testing.T) { | ||
| engine, err := veles.NewDetectionEngine([]veles.Detector{ | ||
| sendgrid.NewDetector(), | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cases := []struct { | ||
| name string | ||
| input string | ||
| want []veles.Secret | ||
| }{{ | ||
| name: "empty input", | ||
| input: "", | ||
| }, { | ||
| name: "wrong prefix - not SG.", | ||
| input: "XX.abcdefghij1234567890AB.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk1234", | ||
| }, { | ||
| name: "lowercase sg prefix should not match", | ||
| input: "sg.abcdefghij1234567890AB.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk1234", | ||
| }, { | ||
| name: "too short key_id section", | ||
| input: "SG.short.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk1234", | ||
| }, { | ||
| name: "too short key_secret section", | ||
| input: "SG.abcdefghij1234567890AB.short", | ||
| }, { | ||
| name: "invalid characters in key_id - special chars", | ||
| input: "SG.abcdefghij123456!@#$.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk1234", | ||
| }, { | ||
| name: "invalid characters in key_secret - special chars", | ||
| input: "SG.abcdefghij1234567890AB.ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$efghijk1234", | ||
| }, { | ||
| name: "missing first dot", | ||
| input: "SGabcdefghij1234567890AB.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk1234", | ||
| }, { | ||
| name: "missing second dot", | ||
| input: "SG.abcdefghij1234567890ABABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk1234", | ||
| }, { | ||
| name: "random text without any keys", | ||
| input: "this is some random text without any API keys", | ||
| }, { | ||
| name: "partial key - truncated", | ||
| input: testSendGridAPIKey[:len(testSendGridAPIKey)-1], | ||
| }} | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got, err := engine.Detect(t.Context(), strings.NewReader(tc.input)) | ||
| if err != nil { | ||
| t.Errorf("Detect() error: %v, want nil", err) | ||
| } | ||
| if diff := cmp.Diff(tc.want, got, cmpopts.EquateEmpty()); diff != "" { | ||
| t.Errorf("Detect() diff (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSendGridKeyFormat(t *testing.T) { | ||
| // Test that our fake keys are the correct length | ||
| tests := []struct { | ||
| name string | ||
| key string | ||
| }{ | ||
| {"testSendGridAPIKey", testSendGridAPIKey}, | ||
| {"testSendGridAPIKey2", testSendGridAPIKey2}, | ||
| {"testSendGridAPIKeyWithSpecialChars", testSendGridAPIKeyWithSpecialChars}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if len(tt.key) != 69 { | ||
| t.Errorf("%s has length %d, want 69", tt.name, len(tt.key)) | ||
| } | ||
| if tt.key[:3] != "SG." { | ||
| t.Errorf("%s doesn't start with 'SG.'", tt.name) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package sendgrid | ||
|
|
||
| // APIKey is a Veles Secret that holds relevant information for a | ||
| // SendGrid API key (prefix `SG.`). | ||
| // APIKey represents an API key used to authenticate requests to SendGrid. | ||
| // It implements veles.Secret. | ||
| type APIKey struct { | ||
erikvarga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Key string | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.