-
Notifications
You must be signed in to change notification settings - Fork 717
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
[FIXED] Validation in jetstream and KV #1613
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -344,16 +344,17 @@ const ( | |
|
||
// Regex for valid keys and buckets. | ||
var ( | ||
validBucketRe = regexp.MustCompile(`\A[a-zA-Z0-9_-]+\z`) | ||
validKeyRe = regexp.MustCompile(`\A[-/_=\.a-zA-Z0-9]+\z`) | ||
validBucketRe = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would test thse regex expressions (or better - a functin that valide those items). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests added |
||
validKeyRe = regexp.MustCompile(`^[-/_=\.a-zA-Z0-9]+$`) | ||
validSearchKeyRe = regexp.MustCompile(`^[-/_=\.a-zA-Z0-9*]*[>]?$`) | ||
) | ||
|
||
// KeyValue will lookup and bind to an existing KeyValue store. | ||
func (js *js) KeyValue(bucket string) (KeyValue, error) { | ||
if !js.nc.serverMinVersion(2, 6, 2) { | ||
return nil, errors.New("nats: key-value requires at least server version 2.6.2") | ||
} | ||
if !validBucketRe.MatchString(bucket) { | ||
if !bucketValid(bucket) { | ||
return nil, ErrInvalidBucketName | ||
} | ||
stream := fmt.Sprintf(kvBucketNameTmpl, bucket) | ||
|
@@ -381,7 +382,7 @@ func (js *js) CreateKeyValue(cfg *KeyValueConfig) (KeyValue, error) { | |
if cfg == nil { | ||
return nil, ErrKeyValueConfigRequired | ||
} | ||
if !validBucketRe.MatchString(cfg.Bucket) { | ||
if !bucketValid(cfg.Bucket) { | ||
return nil, ErrInvalidBucketName | ||
} | ||
if _, err := js.AccountInfo(); err != nil { | ||
|
@@ -507,7 +508,7 @@ func (js *js) CreateKeyValue(cfg *KeyValueConfig) (KeyValue, error) { | |
|
||
// DeleteKeyValue will delete this KeyValue store (JetStream stream). | ||
func (js *js) DeleteKeyValue(bucket string) error { | ||
if !validBucketRe.MatchString(bucket) { | ||
if !bucketValid(bucket) { | ||
return ErrInvalidBucketName | ||
} | ||
stream := fmt.Sprintf(kvBucketNameTmpl, bucket) | ||
|
@@ -547,13 +548,27 @@ func (e *kve) Created() time.Time { return e.created } | |
func (e *kve) Delta() uint64 { return e.delta } | ||
func (e *kve) Operation() KeyValueOp { return e.op } | ||
|
||
func bucketValid(bucket string) bool { | ||
if len(bucket) == 0 { | ||
return false | ||
} | ||
return validBucketRe.MatchString(bucket) | ||
} | ||
|
||
func keyValid(key string) bool { | ||
if len(key) == 0 || key[0] == '.' || key[len(key)-1] == '.' { | ||
return false | ||
} | ||
return validKeyRe.MatchString(key) | ||
} | ||
|
||
func searchKeyValid(key string) bool { | ||
if len(key) == 0 || key[0] == '.' || key[len(key)-1] == '.' { | ||
return false | ||
} | ||
return validSearchKeyRe.MatchString(key) | ||
} | ||
|
||
// Get returns the latest value for the key. | ||
func (kv *kvs) Get(key string) (KeyValueEntry, error) { | ||
e, err := kv.get(key, kvLatestRevision) | ||
|
@@ -951,6 +966,9 @@ func (kv *kvs) WatchAll(opts ...WatchOpt) (KeyWatcher, error) { | |
// Watch will fire the callback when a key that matches the keys pattern is updated. | ||
// keys needs to be a valid NATS subject. | ||
func (kv *kvs) Watch(keys string, opts ...WatchOpt) (KeyWatcher, error) { | ||
if !searchKeyValid(keys) { | ||
return nil, fmt.Errorf("%w: %s", ErrInvalidKey, "keys cannot be empty and must be a valid NATS subject") | ||
} | ||
var o watchOpts | ||
for _, opt := range opts { | ||
if opt != nil { | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now is aligned with CLI and .js clients, but I'm afraid implementing this change could break some users.
Any thoughts @ripienaar ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, perhaps the only one the server wouldnt catch really is the space right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually no - space would break the protocol there so that wouldnt have worked, I think its fine but we do need to call it out carefully
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we definately need to add space and wildcards, but
/
and\
are not that obvious IMO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/
and\
are already rejected by the server withAPI error: code=400 err_code=10128 description=Stream name can not contain path separators
(same for consumer names), so the only change here would be client-side validation instead of server-side. But I don't think we absolutely need it since this at least does not break protocol when put in subject.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
qq, when creating a consumer do we check for whether trying to create an filter subject like
foo.>.
orfoo.>.*
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't. I assume safe validations (100% non-breaking) would be checking for
>
and.
placements right? Plus spaces in subject. Anything else?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes ending with
.
need to be client side since it becomes a malformed subject so the server cannot match the JS API,foo.>.*
looks like the server does handleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added validating filter subject