Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bytes_array_test(self):
self.t("'20000000000000000000'.encode() * 5000",
'20000000000000000000'.encode() * 5000)
with self.assertRaises(simpleeval.IterableTooLong):
self.t("'123121323123131231223'.encode() * 5000", 20)
def do_generator(gi=0):
g = node.generators[gi]
for i in self._eval(g.iter):
self._max_count += 1
if self._max_count > MAX_COMPREHENSION_LENGTH:
raise IterableTooLong('Comprehension generates too many elements')
recurse_targets(g.target, i)
if all(self._eval(iff) for iff in g.ifs):
if len(node.generators) > gi + 1:
do_generator(gi+1)
else:
to_return.append(self._eval(node.elt))
def _eval_constant(node):
if hasattr(node.value, '__len__') and len(node.value) > MAX_STRING_LENGTH:
raise IterableTooLong("Literal in statement is too long!"
" ({0}, when {1} is max)".format(len(node.value), MAX_STRING_LENGTH))
return node.value
def _eval_str(node):
if len(node.s) > MAX_STRING_LENGTH:
raise IterableTooLong("String Literal in statement is too long!"
" ({0}, when {1} is max)".format(
len(node.s), MAX_STRING_LENGTH))
return node.s
def _eval_joinedstr(self, node):
length = 0
evaluated_values = []
for n in node.values:
val = str(self._eval(n))
if len(val) + length > MAX_STRING_LENGTH:
raise IterableTooLong("Sorry, I will not evaluate something this long.")
evaluated_values.append(val)
return ''.join(evaluated_values)
def safe_add(a, b): # pylint: disable=invalid-name
""" iterable length limit again """
if hasattr(a, '__len__') and hasattr(b, '__len__'):
if len(a) + len(b) > MAX_STRING_LENGTH:
raise IterableTooLong("Sorry, adding those two together would"
" make something too long.")
return a + b
def safe_mult(a, b): # pylint: disable=invalid-name
""" limit the number of times an iterable can be repeated... """
if hasattr(a, '__len__') and b * len(a) > MAX_STRING_LENGTH:
raise IterableTooLong('Sorry, I will not evalute something that long.')
if hasattr(b, '__len__') and a * len(b) > MAX_STRING_LENGTH:
raise IterableTooLong('Sorry, I will not evalute something that long.')
return a * b
def _eval_comprehension(self, node):
iterable = self._eval(node.iter)
if len(iterable) + self._loops > MAX_ITER_LENGTH:
raise IterableTooLong("Execution limit exceeded: too many loops.")
self._loops += len(iterable)
for item in iterable:
self._assign(node.target, item, False)
if all(self._eval(stmt) for stmt in node.ifs):
yield item
def safe_range(start, stop=None, step=None):
if stop is None and step is None:
if start > MAX_ITER_LENGTH:
raise IterableTooLong("This range is too large.")
return list(range(start))
elif stop is not None and step is None:
if stop - start > MAX_ITER_LENGTH:
raise IterableTooLong("This range is too large.")
return list(range(start, stop))
elif stop is not None and step is not None:
if (stop - start) / step > MAX_ITER_LENGTH:
raise IterableTooLong("This range is too large.")
return list(range(start, stop, step))
else:
raise ValueError("Invalid arguments passed to range()")