| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/sanitizer.cc |
| Date: | 2025-11-23 02:35:30 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 69 | 71 | 97.2% |
| Branches: | 40 | 49 | 81.6% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | * | ||
| 4 | * Provides input data sanitizer in the form of whitelist of character ranges. | ||
| 5 | */ | ||
| 6 | |||
| 7 | |||
| 8 | #include "sanitizer.h" | ||
| 9 | |||
| 10 | #include <cassert> | ||
| 11 | |||
| 12 | using namespace std; // NOLINT | ||
| 13 | |||
| 14 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 15 | namespace CVMFS_NAMESPACE_GUARD { | ||
| 16 | #endif | ||
| 17 | |||
| 18 | namespace sanitizer { | ||
| 19 | |||
| 20 | 32187 | CharRange::CharRange(const char range_begin, const char range_end) { | |
| 21 | 32187 | range_begin_ = range_begin; | |
| 22 | 32187 | range_end_ = range_end; | |
| 23 | 32187 | } | |
| 24 | |||
| 25 | |||
| 26 | 28808 | CharRange::CharRange(const char single_char) { | |
| 27 | 28808 | range_begin_ = range_end_ = single_char; | |
| 28 | 28808 | } | |
| 29 | |||
| 30 | |||
| 31 | 369769 | bool CharRange::InRange(const char c) const { | |
| 32 |
4/4✓ Branch 0 taken 300752 times.
✓ Branch 1 taken 69017 times.
✓ Branch 2 taken 204409 times.
✓ Branch 3 taken 96343 times.
|
369769 | return (c >= range_begin_) && (c <= range_end_); |
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | //------------------------------------------------------------------------------ | ||
| 37 | |||
| 38 | |||
| 39 | 28272 | InputSanitizer::InputSanitizer(const string &whitelist) : max_length_(-1) { | |
| 40 |
1/2✓ Branch 1 taken 28272 times.
✗ Branch 2 not taken.
|
28272 | InitValidRanges(whitelist); |
| 41 | 28272 | } | |
| 42 | |||
| 43 | |||
| 44 | 148 | InputSanitizer::InputSanitizer(const string &whitelist, int max_length) | |
| 45 | 148 | : max_length_(max_length) { | |
| 46 |
1/2✓ Branch 1 taken 148 times.
✗ Branch 2 not taken.
|
148 | InitValidRanges(whitelist); |
| 47 | 148 | } | |
| 48 | |||
| 49 | |||
| 50 | 28420 | void InputSanitizer::InitValidRanges(const std::string &whitelist) { | |
| 51 | // Parse the whitelist | ||
| 52 | 28420 | const unsigned length = whitelist.length(); | |
| 53 | 28420 | unsigned pickup_pos = 0; | |
| 54 |
2/2✓ Branch 0 taken 93182 times.
✓ Branch 1 taken 28420 times.
|
121602 | for (unsigned i = 0; i < length; ++i) { |
| 55 |
7/8✓ Branch 0 taken 64797 times.
✓ Branch 1 taken 28385 times.
✓ Branch 3 taken 32187 times.
✓ Branch 4 taken 32610 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32187 times.
✓ Branch 7 taken 60995 times.
✓ Branch 8 taken 32187 times.
|
93182 | if ((i + 1 >= length) || (whitelist[i + 1] == ' ') || (i == length - 1)) { |
| 56 |
1/2✓ Branch 1 taken 60995 times.
✗ Branch 2 not taken.
|
60995 | const string range = whitelist.substr(pickup_pos, i - pickup_pos + 1); |
| 57 |
2/3✓ Branch 1 taken 28808 times.
✓ Branch 2 taken 32187 times.
✗ Branch 3 not taken.
|
60995 | switch (range.length()) { |
| 58 | 28808 | case 1: | |
| 59 |
1/2✓ Branch 3 taken 28808 times.
✗ Branch 4 not taken.
|
28808 | valid_ranges_.push_back(CharRange(range[0])); |
| 60 | 28808 | break; | |
| 61 | 32187 | case 2: | |
| 62 |
1/2✓ Branch 4 taken 32187 times.
✗ Branch 5 not taken.
|
32187 | valid_ranges_.push_back(CharRange(range[0], range[1])); |
| 63 | 32187 | break; | |
| 64 | ✗ | default: | |
| 65 | ✗ | assert(false); | |
| 66 | } | ||
| 67 | 60995 | ++i; | |
| 68 | 60995 | pickup_pos = i + 1; | |
| 69 | 60995 | } | |
| 70 | } | ||
| 71 | 28420 | } | |
| 72 | |||
| 73 | |||
| 74 | 30534 | bool InputSanitizer::Sanitize(std::string::const_iterator begin, | |
| 75 | std::string::const_iterator end, | ||
| 76 | std::string *filtered_output) const { | ||
| 77 | 30534 | int pos = 0; | |
| 78 | 30534 | bool is_sane = true; | |
| 79 |
2/2✓ Branch 2 taken 250505 times.
✓ Branch 3 taken 30394 times.
|
280899 | for (; begin != end; ++begin) { |
| 80 |
2/2✓ Branch 2 taken 204409 times.
✓ Branch 3 taken 46096 times.
|
250505 | if (CheckRanges(*begin)) { |
| 81 |
4/4✓ Branch 0 taken 1418 times.
✓ Branch 1 taken 202991 times.
✓ Branch 2 taken 140 times.
✓ Branch 3 taken 1278 times.
|
204409 | if ((max_length_ >= 0) && (pos >= max_length_)) { |
| 82 | 140 | is_sane = false; | |
| 83 | 140 | break; | |
| 84 | } | ||
| 85 | 204269 | filtered_output->push_back(*begin); | |
| 86 | 204269 | pos++; | |
| 87 | } else { | ||
| 88 | 46096 | is_sane = false; | |
| 89 | } | ||
| 90 | } | ||
| 91 | 30534 | return is_sane; | |
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | 250505 | bool InputSanitizer::CheckRanges(const char chr) const { | |
| 96 |
2/2✓ Branch 1 taken 369769 times.
✓ Branch 2 taken 46096 times.
|
415865 | for (unsigned j = 0; j < valid_ranges_.size(); ++j) { |
| 97 |
2/2✓ Branch 2 taken 204409 times.
✓ Branch 3 taken 165360 times.
|
369769 | if (valid_ranges_[j].InRange(chr)) { |
| 98 | 204409 | return true; | |
| 99 | } | ||
| 100 | } | ||
| 101 | 46096 | return false; | |
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | 924 | string InputSanitizer::Filter(const std::string &input) const { | |
| 106 | 924 | string filtered_output; | |
| 107 |
1/2✓ Branch 1 taken 924 times.
✗ Branch 2 not taken.
|
924 | Sanitize(input, &filtered_output); |
| 108 | 924 | return filtered_output; | |
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | 29680 | bool InputSanitizer::IsValid(const std::string &input) const { | |
| 113 | 29680 | string dummy; | |
| 114 |
1/2✓ Branch 1 taken 29680 times.
✗ Branch 2 not taken.
|
59360 | return Sanitize(input, &dummy); |
| 115 | 29680 | } | |
| 116 | |||
| 117 | |||
| 118 | 1082 | bool IntegerSanitizer::Sanitize(std::string::const_iterator begin, | |
| 119 | std::string::const_iterator end, | ||
| 120 | std::string *filtered_output) const { | ||
| 121 |
2/2✓ Branch 1 taken 35 times.
✓ Branch 2 taken 1047 times.
|
1082 | if (std::distance(begin, end) == 0) { |
| 122 | 35 | return false; | |
| 123 | } | ||
| 124 | |||
| 125 |
2/2✓ Branch 1 taken 70 times.
✓ Branch 2 taken 977 times.
|
1047 | if (*begin == '-') { |
| 126 | // minus is allowed as the first character! | ||
| 127 | 70 | filtered_output->push_back('-'); | |
| 128 | 70 | begin++; | |
| 129 | } | ||
| 130 | |||
| 131 | 1047 | return InputSanitizer::Sanitize(begin, end, filtered_output); | |
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | 350 | bool PositiveIntegerSanitizer::Sanitize(std::string::const_iterator begin, | |
| 136 | std::string::const_iterator end, | ||
| 137 | std::string *filtered_output) const { | ||
| 138 |
2/2✓ Branch 1 taken 35 times.
✓ Branch 2 taken 315 times.
|
350 | if (std::distance(begin, end) == 0) { |
| 139 | 35 | return false; | |
| 140 | } | ||
| 141 | |||
| 142 | 315 | return InputSanitizer::Sanitize(begin, end, filtered_output); | |
| 143 | } | ||
| 144 | |||
| 145 | } // namespace sanitizer | ||
| 146 | |||
| 147 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 148 | } // namespace CVMFS_NAMESPACE_GUARD | ||
| 149 | #endif | ||
| 150 |