CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sanitizer.cc
Go to the documentation of this file.
1 
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 CharRange::CharRange(const char range_begin, const char range_end) {
21  range_begin_ = range_begin;
22  range_end_ = range_end;
23 }
24 
25 
26 CharRange::CharRange(const char single_char) {
27  range_begin_ = range_end_ = single_char;
28 }
29 
30 
31 bool CharRange::InRange(const char c) const {
32  return (c >= range_begin_) && (c <= range_end_);
33 }
34 
35 
36 //------------------------------------------------------------------------------
37 
38 
39 InputSanitizer::InputSanitizer(const string &whitelist) : max_length_(-1) {
40  InitValidRanges(whitelist);
41 }
42 
43 
44 InputSanitizer::InputSanitizer(const string &whitelist, int max_length)
45  : max_length_(max_length) {
46  InitValidRanges(whitelist);
47 }
48 
49 
50 void InputSanitizer::InitValidRanges(const std::string &whitelist) {
51  // Parse the whitelist
52  const unsigned length = whitelist.length();
53  unsigned pickup_pos = 0;
54  for (unsigned i = 0; i < length; ++i) {
55  if ((i + 1 >= length) || (whitelist[i + 1] == ' ') || (i == length - 1)) {
56  const string range = whitelist.substr(pickup_pos, i - pickup_pos + 1);
57  switch (range.length()) {
58  case 1:
59  valid_ranges_.push_back(CharRange(range[0]));
60  break;
61  case 2:
62  valid_ranges_.push_back(CharRange(range[0], range[1]));
63  break;
64  default:
65  assert(false);
66  }
67  ++i;
68  pickup_pos = i + 1;
69  }
70  }
71 }
72 
73 
74 bool InputSanitizer::Sanitize(std::string::const_iterator begin,
75  std::string::const_iterator end,
76  std::string *filtered_output) const {
77  int pos = 0;
78  bool is_sane = true;
79  for (; begin != end; ++begin) {
80  if (CheckRanges(*begin)) {
81  if ((max_length_ >= 0) && (pos >= max_length_)) {
82  is_sane = false;
83  break;
84  }
85  filtered_output->push_back(*begin);
86  pos++;
87  } else {
88  is_sane = false;
89  }
90  }
91  return is_sane;
92 }
93 
94 
95 bool InputSanitizer::CheckRanges(const char chr) const {
96  for (unsigned j = 0; j < valid_ranges_.size(); ++j) {
97  if (valid_ranges_[j].InRange(chr)) {
98  return true;
99  }
100  }
101  return false;
102 }
103 
104 
105 string InputSanitizer::Filter(const std::string &input) const {
106  string filtered_output;
107  Sanitize(input, &filtered_output);
108  return filtered_output;
109 }
110 
111 
112 bool InputSanitizer::IsValid(const std::string &input) const {
113  string dummy;
114  return Sanitize(input, &dummy);
115 }
116 
117 
118 bool IntegerSanitizer::Sanitize(std::string::const_iterator begin,
119  std::string::const_iterator end,
120  std::string *filtered_output) const {
121  if (std::distance(begin, end) == 0) {
122  return false;
123  }
124 
125  if (*begin == '-') {
126  // minus is allowed as the first character!
127  filtered_output->push_back('-');
128  begin++;
129  }
130 
131  return InputSanitizer::Sanitize(begin, end, filtered_output);
132 }
133 
134 
135 bool PositiveIntegerSanitizer::Sanitize(std::string::const_iterator begin,
136  std::string::const_iterator end,
137  std::string *filtered_output) const {
138  if (std::distance(begin, end) == 0) {
139  return false;
140  }
141 
142  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
bool CheckRanges(const char chr) const
Definition: sanitizer.cc:95
void InitValidRanges(const std::string &whitelist)
Definition: sanitizer.cc:50
InputSanitizer(const std::string &whitelist)
virtual bool Sanitize(std::string::const_iterator begin, std::string::const_iterator end, std::string *filtered_output) const
Definition: sanitizer.cc:118
bool Sanitize(const std::string &input, std::string *filtered_output) const
Definition: sanitizer.h:41
assert((mem||(size==0))&&"Out Of Memory")
bool IsValid(const std::string &input) const
Definition: sanitizer.cc:112
std::vector< CharRange > valid_ranges_
Definition: sanitizer.h:53
const whitelist::Whitelist * whitelist() const
Definition: repository.h:124
virtual bool Sanitize(std::string::const_iterator begin, std::string::const_iterator end, std::string *filtered_output) const
Definition: sanitizer.cc:135
std::string Filter(const std::string &input) const
Definition: sanitizer.cc:105