From 258893a6dd58a0f1797d9ec644e6404b297c5980 Mon Sep 17 00:00:00 2001 From: Roman Perekhod Date: Fri, 11 Apr 2025 09:41:15 +0200 Subject: [PATCH] limited the length of tags (cherry picked from commit 74cf69b5ece6665ad183649197245e307d288d9d) --- changelog/unreleased/add-tags-validation.md | 5 + go.mod | 2 +- go.sum | 4 +- services/frontend/pkg/config/config.go | 5 + .../pkg/config/defaults/defaultconfig.go | 3 + services/frontend/pkg/revaconfig/config.go | 3 + services/graph/pkg/config/config.go | 6 + .../pkg/config/defaults/defaultconfig.go | 3 + services/graph/pkg/service/v0/tags.go | 7 +- .../reva/v2/pkg/owncloud/ocs/capabilities.go | 6 + .../utils/decomposedfs/upload/upload.go | 11 ++ .../cs3org/reva/v2/pkg/tags/tags.go | 127 ++++++++++++------ vendor/modules.txt | 4 +- 13 files changed, 142 insertions(+), 44 deletions(-) create mode 100644 changelog/unreleased/add-tags-validation.md diff --git a/changelog/unreleased/add-tags-validation.md b/changelog/unreleased/add-tags-validation.md new file mode 100644 index 00000000000..6528f86830f --- /dev/null +++ b/changelog/unreleased/add-tags-validation.md @@ -0,0 +1,5 @@ +Enhancement: Limit length of tags + +We limited the length of tags to avoid DOS attacks against the ocis server. + +https://github.com/owncloud/ocis/pull/11231 diff --git a/go.mod b/go.mod index 401796143e5..de656cb0d42 100644 --- a/go.mod +++ b/go.mod @@ -354,4 +354,4 @@ replace go-micro.dev/v4 => github.com/kobergj/go-micro/v4 v4.0.0-20250117084952- // see https://github.com/mattn/go-sqlite3/issues/965 for more details exclude github.com/mattn/go-sqlite3 v2.0.3+incompatible -replace github.com/cs3org/reva/v2 => github.com/owncloud/reva/v2 v2.0.0-20250228075939-dfd964e1fe58 +replace github.com/cs3org/reva/v2 => github.com/owncloud/reva/v2 v2.0.0-20250526202834-eda90d6d516a diff --git a/go.sum b/go.sum index 84e07f2ddc8..93cade564e8 100644 --- a/go.sum +++ b/go.sum @@ -881,8 +881,8 @@ github.com/orcaman/concurrent-map v1.0.0/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CF github.com/ovh/go-ovh v1.1.0/go.mod h1:AxitLZ5HBRPyUd+Zl60Ajaag+rNTdVXWIkzfrVuTXWA= github.com/owncloud/libre-graph-api-go v1.0.5-0.20250217093259-fa3804be6c27 h1:ID8s5lGBntmrlI6TbDAjTzRyHucn3bVM2wlW+HBplv4= github.com/owncloud/libre-graph-api-go v1.0.5-0.20250217093259-fa3804be6c27/go.mod h1:+gT+x62AS9u2Farh9wE2uYmgdvTg0MQgsSI62D+xoRg= -github.com/owncloud/reva/v2 v2.0.0-20250228075939-dfd964e1fe58 h1:q1FVV97Ri+uf8c/2WlCYuJ8t3FX921a/5BCGSJxhdmM= -github.com/owncloud/reva/v2 v2.0.0-20250228075939-dfd964e1fe58/go.mod h1:1H26PMXoa1rDrIoZ7lGOerq1Bg07/5srYfRaKfxBSsc= +github.com/owncloud/reva/v2 v2.0.0-20250526202834-eda90d6d516a h1:qiZSmdJaEIULParVAIgskmADKHfISTJj8P4frohT2K8= +github.com/owncloud/reva/v2 v2.0.0-20250526202834-eda90d6d516a/go.mod h1:1H26PMXoa1rDrIoZ7lGOerq1Bg07/5srYfRaKfxBSsc= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0= github.com/pablodz/inotifywaitgo v0.0.7 h1:1ii49dGBnRn0t1Sz7RGZS6/NberPEDQprwKHN49Bv6U= diff --git a/services/frontend/pkg/config/config.go b/services/frontend/pkg/config/config.go index b4e3c721f26..ad8e151fbe9 100644 --- a/services/frontend/pkg/config/config.go +++ b/services/frontend/pkg/config/config.go @@ -57,6 +57,7 @@ type Config struct { ServiceAccount ServiceAccount `yaml:"service_account"` PasswordPolicy PasswordPolicy `yaml:"password_policy"` + Validation Validation `yaml:"validation"` ConfigurableNotifications bool `yaml:"configurable_notifications" env:"FRONTEND_CONFIGURABLE_NOTIFICATIONS" desc:"Allow configuring notifications via web client." introductionVersion:"7.1"` @@ -193,3 +194,7 @@ type PasswordPolicy struct { MinSpecialCharacters int `yaml:"min_special_characters" env:"OCIS_PASSWORD_POLICY_MIN_SPECIAL_CHARACTERS;FRONTEND_PASSWORD_POLICY_MIN_SPECIAL_CHARACTERS" desc:"Define the minimum number of characters from the special characters list to be present. Defaults to 1 if not set." introductionVersion:"5.0"` BannedPasswordsList string `yaml:"banned_passwords_list" env:"OCIS_PASSWORD_POLICY_BANNED_PASSWORDS_LIST;FRONTEND_PASSWORD_POLICY_BANNED_PASSWORDS_LIST" desc:"Path to the 'banned passwords list' file. This only impacts public link password validation. See the documentation for more details." introductionVersion:"5.0"` } + +type Validation struct { + MaxTagLength int `yaml:"max_tag_length" env:"OCIS_MAX_TAG_LENGTH" desc:"Define the maximum tag length. Defaults to 100 if not set. Set to 0 to not limit the tag length. Changes only impact the validation of new tags." introductionVersion:"%%NEXT%%"` +} diff --git a/services/frontend/pkg/config/defaults/defaultconfig.go b/services/frontend/pkg/config/defaults/defaultconfig.go index 47523b51608..692415eedfa 100644 --- a/services/frontend/pkg/config/defaults/defaultconfig.go +++ b/services/frontend/pkg/config/defaults/defaultconfig.go @@ -138,6 +138,9 @@ func DefaultConfig() *config.Config { MinDigits: 1, MinSpecialCharacters: 1, }, + Validation: config.Validation{ + MaxTagLength: 100, + }, } } diff --git a/services/frontend/pkg/revaconfig/config.go b/services/frontend/pkg/revaconfig/config.go index 3cd7ad87add..f14176d7470 100644 --- a/services/frontend/pkg/revaconfig/config.go +++ b/services/frontend/pkg/revaconfig/config.go @@ -225,6 +225,9 @@ func FrontendConfigFromStruct(cfg *config.Config, logger log.Logger) (map[string "delete_disabled": !cfg.LDAPServerWriteEnabled, "change_password_self_disabled": changePasswordDisabled, }, + "tags": map[string]interface{}{ + "max_tag_length": cfg.Validation.MaxTagLength, + }, }, "checksums": map[string]interface{}{ "supported_types": cfg.Checksums.SupportedTypes, diff --git a/services/graph/pkg/config/config.go b/services/graph/pkg/config/config.go index 497f5ab053a..df47de8ecec 100644 --- a/services/graph/pkg/config/config.go +++ b/services/graph/pkg/config/config.go @@ -36,6 +36,8 @@ type Config struct { Keycloak Keycloak `yaml:"keycloak"` ServiceAccount ServiceAccount `yaml:"service_account"` + Validation Validation `yaml:"validation"` + Context context.Context `yaml:"-"` } @@ -153,3 +155,7 @@ type ServiceAccount struct { ServiceAccountID string `yaml:"service_account_id" env:"OCIS_SERVICE_ACCOUNT_ID;GRAPH_SERVICE_ACCOUNT_ID" desc:"The ID of the service account the service should use. See the 'auth-service' service description for more details." introductionVersion:"5.0"` ServiceAccountSecret string `yaml:"service_account_secret" env:"OCIS_SERVICE_ACCOUNT_SECRET;GRAPH_SERVICE_ACCOUNT_SECRET" desc:"The service account secret." introductionVersion:"5.0"` } + +type Validation struct { + MaxTagLength int `yaml:"max_tag_length" env:"OCIS_MAX_TAG_LENGTH" desc:"Define the maximum tag length. Defaults to 100 if not set. Set to 0 to not limit the tag length. Changes only impact the validation of new tags." introductionVersion:"%%NEXT%%"` +} diff --git a/services/graph/pkg/config/defaults/defaultconfig.go b/services/graph/pkg/config/defaults/defaultconfig.go index a4034fcabe4..b467137d0d6 100644 --- a/services/graph/pkg/config/defaults/defaultconfig.go +++ b/services/graph/pkg/config/defaults/defaultconfig.go @@ -127,6 +127,9 @@ func DefaultConfig() *config.Config { UnifiedRoles: config.UnifiedRoles{ AvailableRoles: nil, // will be populated with defaults in EnsureDefaults }, + Validation: config.Validation{ + MaxTagLength: 100, + }, } } diff --git a/services/graph/pkg/service/v0/tags.go b/services/graph/pkg/service/v0/tags.go index b8310c970e6..79ebe92d47f 100644 --- a/services/graph/pkg/service/v0/tags.go +++ b/services/graph/pkg/service/v0/tags.go @@ -109,7 +109,12 @@ func (g Graph) AssignTags(w http.ResponseWriter, r *http.Request) { } allTags := tags.New(currentTags) - if !allTags.Add(assignment.Tags...) { + ok, err := allTags.AddValidated(tags.MaxLengthValidator(g.config.Validation.MaxTagLength), assignment.Tags...) + if err != nil { + errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, err.Error()) + return + } + if !ok { errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "no new tags in createtagsrequest or maximum reached") return } diff --git a/vendor/github.com/cs3org/reva/v2/pkg/owncloud/ocs/capabilities.go b/vendor/github.com/cs3org/reva/v2/pkg/owncloud/ocs/capabilities.go index 8d754cd7504..f8606cfb683 100644 --- a/vendor/github.com/cs3org/reva/v2/pkg/owncloud/ocs/capabilities.go +++ b/vendor/github.com/cs3org/reva/v2/pkg/owncloud/ocs/capabilities.go @@ -114,6 +114,7 @@ type CapabilitiesCore struct { type CapabilitiesGraph struct { PersonalDataExport ocsBool `json:"personal-data-export" xml:"personal-data-export" mapstructure:"personal_data_export"` Users CapabilitiesGraphUsers `json:"users" xml:"users" mapstructure:"users"` + Tags CapabilitiesGraphTags `json:"tags" xml:"tags" mapstructure:"tags"` } // CapabilitiesPasswordPolicy hold the password policy capabilities @@ -135,6 +136,11 @@ type CapabilitiesGraphUsers struct { ChangePasswordSelfDisabled ocsBool `json:"change_password_self_disabled" xml:"change_password_self_disabled" mapstructure:"change_password_self_disabled"` } +// CapabilitiesGraphTags holds the graph tags capabilities +type CapabilitiesGraphTags struct { + MaxTagLength int `json:"max_tag_length" xml:"max_tag_length" mapstructure:"max_tag_length"` +} + // Status holds basic status information type Status struct { Installed ocsBool `json:"installed" xml:"installed"` diff --git a/vendor/github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/upload/upload.go b/vendor/github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/upload/upload.go index 4a55035e961..363ee124200 100644 --- a/vendor/github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/upload/upload.go +++ b/vendor/github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/upload/upload.go @@ -66,7 +66,10 @@ func (session *OcisSession) WriteChunk(ctx context.Context, offset int64, src io _, subspan := tracer.Start(ctx, "os.OpenFile") file, err := os.OpenFile(session.binPath(), os.O_WRONLY|os.O_APPEND, defaultFilePerm) subspan.End() + + log := appctx.GetLogger(ctx) if err != nil { + log.Error().Err(err).Msg("WriteChunk: error opening upload file") return 0, err } defer file.Close() @@ -84,6 +87,7 @@ func (session *OcisSession) WriteChunk(ctx context.Context, offset int64, src io // However, for the ocis driver it's not important whether the stream has ended // on purpose or accidentally. if err != nil && err != io.ErrUnexpectedEOF { + log.Error().Err(err).Msg("WriteChunk: error copying data to upload file") return n, err } @@ -112,6 +116,13 @@ func (session *OcisSession) GetReader(ctx context.Context) (io.ReadCloser, error func (session *OcisSession) FinishUpload(ctx context.Context) error { err := session.FinishUploadDecomposed(ctx) + if err != nil { + // this is part of the tusd integration and we might be able to + // log the error in another place + log := appctx.GetLogger(ctx) + log.Error().Err(err).Msg("failed to finish upload") + } + // we need to return a tusd error here to make the tusd handler return the correct status code switch err.(type) { case errtypes.AlreadyExists: diff --git a/vendor/github.com/cs3org/reva/v2/pkg/tags/tags.go b/vendor/github.com/cs3org/reva/v2/pkg/tags/tags.go index 5113b3f8cfd..62a92e3d47c 100644 --- a/vendor/github.com/cs3org/reva/v2/pkg/tags/tags.go +++ b/vendor/github.com/cs3org/reva/v2/pkg/tags/tags.go @@ -19,7 +19,9 @@ package tags import ( + "fmt" "strings" + "unicode/utf8" ) var ( @@ -42,35 +44,45 @@ type Tags struct { // New creates a Tag struct from a slice of tags, e.g. ["tag1", "tag2"] or a list of tags, e.g. "tag1,tag2" func New(ts ...string) *Tags { t := &Tags{sep: _tagsep, maxtags: _maxtags, exists: make(map[string]bool), t: make([]string, 0)} - t.addTags(ts) + t.addTags(t.normalize(ts)) return t } // Add appends a list of new tags and returns true if at least one was appended func (t *Tags) Add(ts ...string) bool { - return len(t.addTags(ts)) > 0 + return len(t.addTags(t.normalize(ts))) > 0 +} + +// AddValidated appends a list of new tags and validates them using the provided validator function +// It returns true if at least one was appended and an error if the validation failed +func (t *Tags) AddValidated(validator func([]string) error, ts ...string) (bool, error) { + newTags := t.normalize(ts) + err := validator(newTags) + if err != nil { + return false, err + } + return len(t.addTags(newTags)) > 0, nil } // Remove removes a list of tags and returns true if at least one was removed func (t *Tags) Remove(s ...string) bool { var removed bool + tags := t.normalize(s) - for _, tt := range s { - for _, tag := range strings.Split(tt, t.sep) { - if !t.exists[tag] { - continue - } - - for i, tt := range t.t { - if tt == tag { - t.t = append(t.t[:i], t.t[i+1:]...) - break - } - } - - delete(t.exists, tag) - removed = true + for _, tag := range tags { + if !t.exists[tag] { + continue } + + for i, tt := range t.t { + if tt == tag { + t.t = append(t.t[:i], t.t[i+1:]...) + break + } + } + + delete(t.exists, tag) + removed = true } return removed } @@ -86,31 +98,70 @@ func (t *Tags) AsSlice() []string { } // adds the tags and returns a list of added tags +// the function receiving a normalized slice of tags, e.g.["tag1", "tag2"] func (t *Tags) addTags(s []string) []string { - added := make([]string, 0) - for _, tt := range s { - for _, tag := range strings.Split(tt, t.sep) { - if tag == "" { - // ignore empty tags - continue - } - - if t.exists[tag] { - // tag is already existing - continue - } - - if t.numtags >= t.maxtags { - // max number of tags reached. We return silently without warning anyone - break - } - - added = append(added, tag) - t.exists[tag] = true - t.numtags++ + added := make([]string, 0, len(t.t)+len(s)) + for _, tag := range s { + if tag == "" { + // ignore empty tags + continue } + + if t.exists[tag] { + // tag is already existing + continue + } + + if t.numtags >= t.maxtags { + // max number of tags reached. We return silently without warning anyone + break + } + + added = append(added, tag) + t.exists[tag] = true + t.numtags++ } t.t = append(added, t.t...) return added } + +// normalize splits the tags and removes empty tags +// the function receiving a slice of tags, e.g.["tag1", "tag2"] or a list of tags, e.g."tag1,tag2" or mixed. +func (t *Tags) normalize(s []string) []string { + res := make([]string, 0, t.maxtags/2) + for _, tt := range s { + for _, tag := range strings.Split(tt, t.sep) { + ttr := strings.TrimSpace(tag) + if ttr == "" { + // ignore empty tags + continue + } + res = append(res, ttr) + } + } + return res +} + +// MaxLengthValidator returns a function that validates the length of each tag in a slice +func MaxLengthValidator(maxTagLength int) func([]string) error { + if maxTagLength <= 0 { + return func(tags []string) error { return nil } + } + return func(tags []string) error { + t := make([]string, 0, maxTagLength) + for _, tag := range tags { + if !utf8.ValidString(tag) { + return fmt.Errorf("tag [%s] contains invalid characters", tag) + } + if utf8.RuneCount([]byte(tag)) > maxTagLength { + t = append(t, tag) + continue + } + } + if len(t) > 0 { + return fmt.Errorf("tag [%s] too long, max length is %d", strings.Join(t, ", "), maxTagLength) + } + return nil + } +} diff --git a/vendor/modules.txt b/vendor/modules.txt index ade701312ff..b1a0fb1ed5d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -371,7 +371,7 @@ github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1 github.com/cs3org/go-cs3apis/cs3/storage/registry/v1beta1 github.com/cs3org/go-cs3apis/cs3/tx/v1beta1 github.com/cs3org/go-cs3apis/cs3/types/v1beta1 -# github.com/cs3org/reva/v2 v2.27.7 => github.com/owncloud/reva/v2 v2.0.0-20250228075939-dfd964e1fe58 +# github.com/cs3org/reva/v2 v2.27.7 => github.com/owncloud/reva/v2 v2.0.0-20250526202834-eda90d6d516a ## explicit; go 1.22.7 github.com/cs3org/reva/v2/cmd/revad/internal/grace github.com/cs3org/reva/v2/cmd/revad/runtime @@ -2408,4 +2408,4 @@ stash.kopano.io/kgol/rndm # github.com/unrolled/secure => github.com/DeepDiver1975/secure v0.0.0-20240611112133-abc838fb797c # github.com/go-micro/plugins/v4/store/nats-js-kv => github.com/kobergj/plugins/v4/store/nats-js-kv v0.0.0-20240807130109-f62bb67e8c90 # go-micro.dev/v4 => github.com/kobergj/go-micro/v4 v4.0.0-20250117084952-d07d30666b7c -# github.com/cs3org/reva/v2 => github.com/owncloud/reva/v2 v2.0.0-20250228075939-dfd964e1fe58 +# github.com/cs3org/reva/v2 => github.com/owncloud/reva/v2 v2.0.0-20250526202834-eda90d6d516a