-
Notifications
You must be signed in to change notification settings - Fork 26
feat(kvs): enforce maximum key length of 32 bytes (C++) #308
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
Open
atarekra
wants to merge
3
commits into
eclipse-score:main
Choose a base branch
from
Valeo-S-CORE-Organization:Max-KeyLength-Feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -29,6 +29,19 @@ using namespace std; | |
| namespace score::mw::per::kvs | ||
| { | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| /* comp_req__kvs__key_length: single source of truth for the key-length rule, | ||
| shared by all paths that ingest keys (set_value, parse_json_data). | ||
| std::string_view::length() counts bytes, matching the byte-based requirement. */ | ||
| bool is_key_length_valid(std::string_view key) | ||
| { | ||
| return key.length() <= KVS_MAX_KEY_LENGTH_BYTES; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| /*********************** KVS Implementation *********************/ | ||
| Kvs::Kvs() | ||
| : filesystem(std::make_unique<score::filesystem::Filesystem>( | ||
|
|
@@ -108,6 +121,18 @@ score::Result<std::unordered_map<std::string, KvsValue>> Kvs::parse_json_data(co | |
| auto sv = element.first.GetAsStringView(); | ||
| std::string key(sv.data(), sv.size()); | ||
|
|
||
| /* comp_req__kvs__key_length: a stored file must satisfy the key-length | ||
| limit; an over-length key means the file is invalid/corrupted. Halt | ||
| loading with an error rather than silently dropping data. */ | ||
| if (!is_key_length_valid(key)) | ||
| { | ||
| logger->LogError() << "Key length " << key.length() << " exceeds maximum allowed " | ||
| << KVS_MAX_KEY_LENGTH_BYTES << " bytes while loading"; | ||
| result = score::MakeUnexpected(ErrorCode::KeyTooLong); | ||
| error = true; | ||
| break; | ||
| } | ||
|
|
||
| auto conv = any_to_kvsvalue(element.second); | ||
| if (!conv) | ||
| { | ||
|
|
@@ -432,6 +457,14 @@ score::Result<bool> Kvs::is_value_default(const std::string_view key) const | |
| /* Set the value for a key*/ | ||
| score::ResultBlank Kvs::set_value(const std::string_view key, const KvsValue& value) | ||
| { | ||
| /* comp_req__kvs__key_length: reject keys that exceed the maximum length. */ | ||
| if (!is_key_length_valid(key)) | ||
| { | ||
| logger->LogError() << "Key length " << key.length() << " exceeds maximum allowed " | ||
|
Contributor
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. Same here - check whitespace behavior. |
||
| << KVS_MAX_KEY_LENGTH_BYTES << " bytes"; | ||
| return score::MakeUnexpected(ErrorCode::KeyTooLong); | ||
| } | ||
|
|
||
| score::ResultBlank result = score::MakeUnexpected(ErrorCode::UnmappedError); | ||
| std::unique_lock<std::mutex> lock(kvs_mutex, std::try_to_lock); | ||
| if (lock.owns_lock()) | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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.
Please check for whitespaces - IIRC spaces are already inserted between streamed fields (e.g.,
"Key length " << key.length()will showKey length 1234instead of expectedKey length 1234).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.
Ok working on it