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
4 changes: 2 additions & 2 deletions pkg/datastore/mysql/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func (m *MySQL) GetTarget(ctx context.Context, id uuid.UUID) (*datastore.Target,
// GetTargetByScope get a target from scope
func (m *MySQL) GetTargetByScope(ctx context.Context, scope string) (*datastore.Target, error) {
var t datastore.Target
query := fmt.Sprintf(`SELECT uuid, scope, github_token, token_expired_at, resource_type, provider_url, status, status_description, created_at, updated_at FROM targets WHERE scope = "%s"`, scope)
if err := m.Conn.GetContext(ctx, &t, query); err != nil {
query := `SELECT uuid, scope, github_token, token_expired_at, resource_type, provider_url, status, status_description, created_at, updated_at FROM targets WHERE scope = ?`
if err := m.Conn.GetContext(ctx, &t, query, scope); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, datastore.ErrNotFound
}
Expand Down
24 changes: 23 additions & 1 deletion pkg/datastore/mysql/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,25 @@ func TestMySQL_GetTargetByScope(t *testing.T) {
},
err: false,
},
{
// scope contains SQL meta characters, must be treated as a literal value (not injected)
input: `owner/repo" OR "1"="1`,
want: nil,
prepare: func() error {
return testDatastore.CreateTarget(context.Background(), datastore.Target{
UUID: testTargetID,
Scope: testScopeRepo,
GitHubToken: testGitHubToken,
TokenExpiredAt: testTime,
ResourceType: datastore.ResourceTypeNano,
ProviderURL: sql.NullString{
String: testProviderURL,
Valid: true,
},
})
},
err: true,
},
}

for _, test := range tests {
Expand All @@ -328,9 +347,12 @@ func TestMySQL_GetTargetByScope(t *testing.T) {
}

got, err := testDatastore.GetTargetByScope(context.Background(), test.input)
if err != nil {
if !test.err && err != nil {
t.Fatalf("failed to get target: %+v", err)
}
if test.err && !errors.Is(err, datastore.ErrNotFound) {
t.Fatalf("want datastore.ErrNotFound, but got: %+v", err)
}
if got != nil {
got.CreatedAt = time.Time{}
got.UpdatedAt = time.Time{}
Expand Down
Loading