mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-26 18:16:47 -05:00
fix(graph): reject empty and invisible drive names on update
Updating a drive with an empty name ("name": "") succeeded and silently
kept the old name, because the name was only validated when it was
non-empty. Validate it whenever the request contains it, so an empty
name is rejected with 400 like on create. A request without a name, or
with "name": null, still leaves the name unchanged.
Space names were trimmed with strings.TrimSpace, which only removes
Unicode whitespace. Invisible formatting characters such as the
zero-width space (U+200B), the word joiner (U+2060) or the byte order
mark (U+FEFF) aren't whitespace, so a name made only of them was
accepted on create and on update, and showed up as an empty name in
every client. Trim these characters from both ends of the name as
well. Inside a name they are still allowed, since some of them are
needed there, e.g. the zero-width joiner in emoji sequences.
Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
1 parent
83615f23e5
commit
0e265282d2
5 files changed
+164
-3
No files matched your search
@@ -12,6 +12,7 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/CiscoM31/godata"
|
||||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
||||
@@ -403,7 +404,7 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) {
|
||||
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "invalid body schema definition")
|
||||
return
|
||||
}
|
||||
spaceName := strings.TrimSpace(drive.Name)
|
||||
spaceName := trimSpaceName(drive.Name)
|
||||
if err := validateSpaceName(spaceName); err != nil {
|
||||
log.Debug().Str("name", spaceName).Err(err).Msg("could not create drive: name validation failed")
|
||||
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, fmt.Sprintf("invalid spacename: %s", err.Error()))
|
||||
@@ -580,8 +581,8 @@ func (g Graph) UpdateDrive(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if drive.GetName() != "" {
|
||||
spacename := strings.TrimSpace(drive.GetName())
|
||||
if _, ok := drive.GetNameOk(); ok {
|
||||
spacename := trimSpaceName(drive.GetName())
|
||||
if err := validateSpaceName(spacename); err != nil {
|
||||
log.Info().Err(err).Msg("could not update drive: spacename invalid")
|
||||
errorcode.GeneralException.Render(w, r, http.StatusBadRequest, err.Error())
|
||||
@@ -1178,6 +1179,18 @@ func sortSpaces(req *godata.GoDataRequest, spaces []*libregraph.Drive) ([]*libre
|
||||
return spaces, nil
|
||||
}
|
||||
|
||||
// trimSpaceName removes whitespace and invisible formatting characters from both ends of a
|
||||
// space name. strings.TrimSpace alone isn't enough: characters such as the zero-width space
|
||||
// (U+200B), the word joiner (U+2060) or the byte order mark (U+FEFF) aren't whitespace in
|
||||
// Unicode, but they are invisible, so a name made only of them would look empty in every client.
|
||||
// Formatting characters inside a name are kept, since some of them are needed there, e.g. the
|
||||
// zero-width joiner (U+200D) in emoji sequences.
|
||||
func trimSpaceName(name string) string {
|
||||
return strings.TrimFunc(name, func(r rune) bool {
|
||||
return unicode.IsSpace(r) || unicode.Is(unicode.Cf, r)
|
||||
})
|
||||
}
|
||||
|
||||
func validateSpaceName(name string) error {
|
||||
if name == "" {
|
||||
return ErrNameEmpty
|
||||
|
||||
@@ -195,3 +195,31 @@ func TestSpaceNameValidation(t *testing.T) {
|
||||
// set max length back to protect other tests
|
||||
_maxSpaceNameLength = 255
|
||||
}
|
||||
|
||||
func TestTrimSpaceName(t *testing.T) {
|
||||
// the names are written as escape sequences on purpose: written directly, these characters
|
||||
// would be invisible or look like ordinary spaces in the source code
|
||||
testCases := []struct {
|
||||
Alias string
|
||||
SpaceName string
|
||||
Expected string
|
||||
}{
|
||||
{"Unchanged", "Space", "Space"},
|
||||
{"Spaces around", " Space ", "Space"},
|
||||
{"Spaces inside are kept", "My Space", "My Space"},
|
||||
// U+00A0 no-break space, U+3000 ideographic space
|
||||
{"Unicode spaces around", "\u00a0Space\u3000", "Space"},
|
||||
// U+200B zero-width space, U+FEFF byte order mark
|
||||
{"Invisible characters around", "\u200bSpace\ufeff", "Space"},
|
||||
// U+2060 word joiner, U+2003 em space, U+200E left-to-right mark
|
||||
{"Mixed around", " \u2060\u2003Space\u200e ", "Space"},
|
||||
// U+200D zero-width joiner, as in the emoji sequence of a family
|
||||
{"Zero-width joiner inside is kept", "Team \U0001F468\u200d\U0001F469\u200d\U0001F467", "Team \U0001F468\u200d\U0001F469\u200d\U0001F467"},
|
||||
// U+200B zero-width space, U+200C zero-width non-joiner, U+00A0 no-break space
|
||||
{"Only invisible characters", "\u200b\u200c\u00a0", ""},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
require.Equal(t, tc.Expected, trimSpaceName(tc.SpaceName), tc.Alias)
|
||||
}
|
||||
}
|
||||
@@ -1177,6 +1177,67 @@ var _ = Describe("Graph", func() {
|
||||
}))
|
||||
})
|
||||
|
||||
DescribeTable("rejects an invalid name without updating the space",
|
||||
func(name string) {
|
||||
body, err := json.Marshal(map[string]string{"name": name})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/drives/{driveID}/", bytes.NewBuffer(body))
|
||||
rctx := chi.NewRouteContext()
|
||||
rctx.URLParams.Add("driveID", "spaceid")
|
||||
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, nil), chi.RouteCtxKey, rctx))
|
||||
svc.UpdateDrive(rr, r)
|
||||
Expect(rr.Code).To(Equal(http.StatusBadRequest))
|
||||
|
||||
gatewayClient.AssertNotCalled(GinkgoT(), "UpdateStorageSpace", mock.Anything, mock.Anything)
|
||||
},
|
||||
// the names are written as escape sequences on purpose: written directly, these
|
||||
// characters would be invisible or look like ordinary spaces in the source code
|
||||
Entry("empty name", ""),
|
||||
Entry("name of only spaces", " "),
|
||||
Entry("name of only tabs and line breaks", "\t\n\r"),
|
||||
// U+00A0 no-break space, U+202F narrow no-break space
|
||||
Entry("name of only no-break spaces", "\u00a0\u202f"),
|
||||
// U+2003 em space, U+2009 thin space, U+3000 ideographic space
|
||||
Entry("name of only wide spaces", "\u2003\u2009\u3000"),
|
||||
// U+200B zero-width space: invisible, but not whitespace in Unicode
|
||||
Entry("name of only zero-width spaces", "\u200b\u200b"),
|
||||
// U+200C zero-width non-joiner, U+200D zero-width joiner
|
||||
Entry("name of only zero-width joiners", "\u200c\u200d"),
|
||||
// U+2060 word joiner, U+FEFF byte order mark (zero-width no-break space)
|
||||
Entry("name of only a word joiner and a byte order mark", "\u2060\ufeff"),
|
||||
// space, U+200B zero-width space, U+00A0 no-break space, U+FEFF byte order mark, space
|
||||
Entry("name of mixed invisible characters", " \u200b\u00a0\ufeff "),
|
||||
)
|
||||
|
||||
DescribeTable("keeps the name when the request doesn't set one",
|
||||
func(jsonBody string) {
|
||||
gatewayClient.On("UpdateStorageSpace", mock.Anything, mock.Anything).Return(func(_ context.Context, req *provider.UpdateStorageSpaceRequest, _ ...grpc.CallOption) *provider.UpdateStorageSpaceResponse {
|
||||
return &provider.UpdateStorageSpaceResponse{
|
||||
Status: status.NewOK(ctx),
|
||||
StorageSpace: req.StorageSpace,
|
||||
}
|
||||
}, nil)
|
||||
gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&provider.StatResponse{
|
||||
Status: status.NewNotFound(ctx, "no special files found"),
|
||||
}, nil).Maybe()
|
||||
|
||||
r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/drives/{driveID}/", bytes.NewBufferString(jsonBody))
|
||||
rctx := chi.NewRouteContext()
|
||||
rctx.URLParams.Add("driveID", "spaceid")
|
||||
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, nil), chi.RouteCtxKey, rctx))
|
||||
svc.UpdateDrive(rr, r)
|
||||
Expect(rr.Code).To(Equal(http.StatusOK))
|
||||
|
||||
gatewayClient.AssertCalled(GinkgoT(), "UpdateStorageSpace", mock.Anything, mock.MatchedBy(func(req *provider.UpdateStorageSpaceRequest) bool {
|
||||
return req.StorageSpace.Id.OpaqueId == "spaceid" &&
|
||||
req.StorageSpace.Name == "" &&
|
||||
utils.ReadPlainFromOpaque(req.StorageSpace.Opaque, "description") == "thedescription"
|
||||
}))
|
||||
},
|
||||
Entry("no name", `{"description": "thedescription"}`),
|
||||
Entry("null name", `{"name": null, "description": "thedescription"}`),
|
||||
)
|
||||
|
||||
It("restores", func() {
|
||||
drive := libregraph.NewDrive("thename")
|
||||
driveJson, err := json.Marshal(drive)
|
||||
|
||||
@@ -1636,6 +1636,30 @@ class SpacesContext implements Context {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Like updateSpaceName, but the new name is written with JSON escape sequences,
|
||||
* e.g. "\u200b" for a zero-width space. Characters such as invisible or unusual
|
||||
* spaces can't be told apart from ordinary spaces, or can't be seen at all, when
|
||||
* written directly into a feature file.
|
||||
*
|
||||
* @param string $user
|
||||
* @param string $spaceName
|
||||
* @param string $escapedNewName
|
||||
*
|
||||
* @return void
|
||||
* @throws GuzzleException
|
||||
* @throws JsonException
|
||||
*/
|
||||
#[When('/^user "([^"]*)" (?:changes|tries to change) the name of the "([^"]*)" space to the escaped name "([^"]*)"$/')]
|
||||
public function updateSpaceNameEscaped(
|
||||
string $user,
|
||||
string $spaceName,
|
||||
string $escapedNewName
|
||||
): void {
|
||||
$newName = json_decode('"' . $escapedNewName . '"', false, 512, JSON_THROW_ON_ERROR);
|
||||
$this->updateSpaceName($user, $spaceName, $newName);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $user
|
||||
|
||||
@@ -58,6 +58,41 @@ Feature: Change data of space
|
||||
| Bob |
|
||||
|
||||
|
||||
Scenario: user with space manager role can't change the name of a space to an empty name via the Graph API
|
||||
When user "Alice" tries to change the name of the "Project Jupiter" space to ""
|
||||
Then the HTTP status code should be "400"
|
||||
And the user "Alice" should have a space called "Project Jupiter"
|
||||
|
||||
|
||||
Scenario: user with space manager role can't change the name of a space to a name of only spaces via the Graph API
|
||||
When user "Alice" tries to change the name of the "Project Jupiter" space to " "
|
||||
Then the HTTP status code should be "400"
|
||||
And the user "Alice" should have a space called "Project Jupiter"
|
||||
|
||||
|
||||
Scenario: user with space manager role can't change the name of a space to a name of only invisible or unusual spaces via the Graph API
|
||||
# \u00a0 no-break space, \u202f narrow no-break space
|
||||
When user "Alice" tries to change the name of the "Project Jupiter" space to the escaped name "\u00a0\u202f"
|
||||
Then the HTTP status code should be "400"
|
||||
# \u2003 em space, \u2009 thin space, \u3000 ideographic space
|
||||
When user "Alice" tries to change the name of the "Project Jupiter" space to the escaped name "\u2003\u2009\u3000"
|
||||
Then the HTTP status code should be "400"
|
||||
# \u200b zero-width space, \u200c zero-width non-joiner, \u200d zero-width joiner
|
||||
When user "Alice" tries to change the name of the "Project Jupiter" space to the escaped name "\u200b\u200c\u200d"
|
||||
Then the HTTP status code should be "400"
|
||||
# \u2060 word joiner, \ufeff byte order mark, mixed with a tab and ordinary spaces
|
||||
When user "Alice" tries to change the name of the "Project Jupiter" space to the escaped name " \u2060\t\ufeff "
|
||||
Then the HTTP status code should be "400"
|
||||
And the user "Alice" should have a space called "Project Jupiter"
|
||||
|
||||
|
||||
Scenario: user with space manager role can change the name of a space to a name with invisible characters around it via the Graph API
|
||||
# \u200b zero-width space, \ufeff byte order mark
|
||||
When user "Alice" changes the name of the "Project Jupiter" space to the escaped name "\u200bProject Saturn\ufeff"
|
||||
Then the HTTP status code should be "200"
|
||||
And the user "Alice" should have a space called "Project Saturn"
|
||||
|
||||
|
||||
Scenario: user with space manager role can change the description(subtitle) of a space via the Graph API
|
||||
When user "Alice" changes the description of the "Project Jupiter" space to "The Death Star is a fictional mobile space station"
|
||||
Then the HTTP status code should be "200"
|
||||
|
||||
Reference in new issue
Block a user