Repository navigation
feat(kvs): enforce maximum key length of 32 bytes (C++) - #308
Conversation
License Check Results🚀 The license check job ran with the Bazel command: bazel run --lockfile_mode=error //:license-checkStatus: Click to expand output |
|
The created documentation from the pull request is available at: docu-html |
arkjedrz
left a comment
There was a problem hiding this comment.
Replace PR description with a human-made one. Current is both inconcise and wrong.
| auto sv = element.first.GetAsStringView(); | ||
| std::string key(sv.data(), sv.size()); | ||
|
|
||
| /* FEAT_REQ__KVS__maximum_size: keys read from persistent storage are not |
There was a problem hiding this comment.
This is simply invalid. Files are either expected to satisfy the limit or are incorrect.
BTW comp_req__kvs__key_length is the right req.
There was a problem hiding this comment.
Your points have been resolved.
| logger->LogWarn() << "Skipping key with length " << key.length() | ||
| << " exceeding maximum allowed " << KVS_MAX_KEY_LENGTH_BYTES | ||
| << " bytes while loading"; | ||
| continue; |
There was a problem hiding this comment.
This is a quiet failure based on invalid assumption. Make it an error and halt the execution.
|
|
||
| /* FEAT_REQ__KVS__maximum_size: single check for the key-length rule. | ||
| std::string_view::length() counts bytes, matching the byte-based requirement. */ | ||
| bool Kvs::is_key_length_valid(std::string_view key) |
There was a problem hiding this comment.
This helper don't have to rely on anything Kvs class related. Rework it to a function, then place it in anonymous namespace/mark it as static.
| /* Set the value for a key*/ | ||
| score::ResultBlank Kvs::set_value(const std::string_view key, const KvsValue& value) | ||
| { | ||
| /* FEAT_REQ__KVS__maximum_size: reject keys that exceed the maximum length. */ |
There was a problem hiding this comment.
Invalid req, like in other places.
|
@atarekra there are some comments, please adapt |
arkjedrz
left a comment
There was a problem hiding this comment.
Logging to be confirmed, otherwise LGTM.
| loading with an error rather than silently dropping data. */ | ||
| if (!is_key_length_valid(key)) | ||
| { | ||
| logger->LogError() << "Key length " << key.length() << " exceeds maximum allowed " |
There was a problem hiding this comment.
Please check for whitespaces - IIRC spaces are already inserted between streamed fields (e.g., "Key length " << key.length() will show Key length 1234 instead of expected Key length 1234).
| /* 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 " |
There was a problem hiding this comment.
Same here - check whitespace behavior.
|
Thanks for the pull request. This thread has been quiet for 30 days, so we are marking it as stale for now. Please take a quick look and let us know whether it is still up to date, still relevant, needs review, or is ready to merge. Any new activity will remove the stale label automatically. If nothing changes in the next 10 days, we will close it to keep the backlog current. |
| @@ -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) | |||
| { | |||
There was a problem hiding this comment.
why did not you validate also in get_value function ?
There was a problem hiding this comment.
set_value is where the limit is actually enforced , an over-length key can never get into the map, so lookups fail anyway. But the error is misleading: you get KeyNotFound when the real problem is an invalid key.
| { | ||
| logger->LogError() << "Key length" << key.length() << "exceeds maximum allowed" | ||
| << KVS_MAX_KEY_LENGTH_BYTES << "bytes while loading"; | ||
| result = score::MakeUnexpected(ErrorCode::KeyTooLong); |
There was a problem hiding this comment.
i would suggest her to return ErrorCode::KvsFileReadError with logging key string and key length
There was a problem hiding this comment.
Agreed on logging the key that's missing and should be there.
On the code: KvsFileReadError means the file couldn't be read, but here it read and parsed fine and the checksum was valid , it's the content that breaks a rule. ValidationFailed is what we already return for a bad checksum, so it may fit better, and it keeps KeyTooLong (new in this PR) meaning purely "the key you passed is invalid". What do you think?
818b207 to
e4316fe
Compare
Implement comp_req__kvs__key_length. Keys longer than KVS_MAX_KEY_LENGTH_BYTES (32) are rejected with the new ErrorCode::KeyTooLong: - set_value() refuses to store them. - get_value() reports KeyTooLong rather than KeyNotFound, since such a key can never have been stored. - Loading a file that contains one halts with KeyTooLong rather than silently dropping the entry. All three log the offending key and its length. The rule lives in one helper, is_key_length_valid(), shared by every path that reads a key. Signed-off-by: atarekra <ahmed.tarek-ramadan@valeo.com>
e4316fe to
0c329e8
Compare
Summary
Enforces the maximum key length for the C++ KVS, implementing
comp_req__kvs__key_lengthChanges
KVS_MAX_KEY_LENGTH_BYTES(32) constant and a file-localis_key_length_valid()helper used as the single source of truth for the rule.set_value()rejects keys longer than the limit and returns the newErrorCode::KeyTooLong.parse_json_data()treats an over-length key found while loading as an invalidor corrupted file: it halts loading and returns
ErrorCode::KeyTooLongratherthan silently dropping the entry.
KeyTooLongerror code and its message.Tests
set_valueaccepts a key of exactly 32 bytes and rejects a 33-byte key withKeyTooLong.parse_json_datareturnsKeyTooLongwhen a loaded object contains anover-length key.
MessageForcovers the new error code.Notes
std::string_view::length()counts bytes, matchingthe requirement.