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
1 change: 1 addition & 0 deletions cmd/api/src/api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const (
ErrorResponseMultipleCollectionScopesProvided = "may only scope collection by exactly one of OU, Domain, or All Trusted Domains"
ErrorResponsePayloadUnmarshalError = "error unmarshalling JSON payload"
ErrorResponseUserSelfDisable = "user attempted to disable themselves"
ErrorResponseAGTagWhiteSpace = "asset group tags must not contain whitespace"

FmtErrorResponseDetailsBadQueryParameters = "there are errors in the query parameters: %v"
)
Expand Down
2 changes: 2 additions & 0 deletions cmd/api/src/api/v2/agi.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ func (s Resources) CreateAssetGroup(response http.ResponseWriter, request *http.

if err := api.ReadJSONRequestPayloadLimited(&createRequest, request); err != nil {
api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusBadRequest, err.Error(), request), response)
} else if strings.Contains(createRequest.Tag, " ") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question: should we disallow all forms of whitespace? It seems like it may be a good idea just to be safe. We'd probably use a regex to test for any whitespace character, rather than enumerate whitespace checks manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I'll add this in a follow up MR.

api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusBadRequest, api.ErrorResponseAGTagWhiteSpace, request), response)
} else if newAssetGroup, err := s.DB.CreateAssetGroup(request.Context(), createRequest.Name, createRequest.Tag, false); err != nil {
api.HandleDatabaseError(request, response, err)
} else {
Expand Down
13 changes: 13 additions & 0 deletions cmd/api/src/api/v2/agi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,19 @@ func TestResources_CreateAssetGroup(t *testing.T) {
Require().
ResponseStatusCode(http.StatusBadRequest)

// Whitespace in asset group tag must error
jsonBody, err := json.Marshal(v2.CreateAssetGroupRequest{Tag: "a b"})
require.Nil(t, err)

requestTemplate.
WithContext(&ctx.Context{
Host: &url.URL{},
}).
WithBody(jsonBody).
OnHandlerFunc(resources.CreateAssetGroup).
Require().
ResponseStatusCode(http.StatusBadRequest)

// Create DB Query fails
mockDB.EXPECT().CreateAssetGroup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(model.AssetGroup{}, fmt.Errorf("exploded"))

Expand Down
18 changes: 18 additions & 0 deletions cmd/api/src/database/migration/migrations/v5.7.1.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to make this v5.8.0 due to the next version definitely being a feature update

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Copyright 2024 Specter Ops, Inc.
--
-- Licensed under the Apache License, Version 2.0
-- 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.
--
-- SPDX-License-Identifier: Apache-2.0

UPDATE asset_groups
SET tag = REPLACE(tag, ' ', '');
6 changes: 6 additions & 0 deletions cmd/api/src/docs/json/paths/v2/asset-isolation.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.ErrorWrapper"
}
},
"Error": {
"$ref": "#/components/responses/defaultError"
}
Expand Down