enumpath 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 470c663d2026145fc65af532db8c91cc8735d780b1e04a9a6504df3258555ff1
4
- data.tar.gz: 48033720373583fc8eeb7f93a788ba25b719ec5491b76505bb4448da04114ed8
3
+ metadata.gz: 4a759aab90deef7e5fe16fb2373738f61d26247efc4d20553740b86c025e1e29
4
+ data.tar.gz: f6dddd9c820e26fdde6eedd8999dae7fd94ab5bf578f33c81f60d56f08b0c559
5
5
  SHA512:
6
- metadata.gz: 69aa4c2efc793ddd006cde16adae4d71619d1fb32d7f27f0c588f58f1525951173ac1dc084dd33907234d4dd1fb8164dd7903f3754e19439e1174ce2e4e14ef0
7
- data.tar.gz: e9cf4de00bd088bd96004955132e2b04009cdf61d90467087c849d32e8c0b5ddbfd553f662b8336d0f5ef5c27bdddc251f227a97d015c2de8db4f6636ef1972a
6
+ metadata.gz: 3984dc1111b7435e76868a400685144b7eb7467e1a61b66fe65b1447aac3c82c93c722fa6a8ff1ff84c8c0cef1dfabf15c6f1b7b291a0b678e733ae2fb435be4
7
+ data.tar.gz: 6be4ed326e7dcd4fb9c00d2cefda0ded4efc8ac57d1e0535bc307ca289bb30ad0a705d5d4f4654c955f5d6443c549c1b395bed331a3ddcc4ea68edf1cc1af03c
data/README.md CHANGED
@@ -8,7 +8,7 @@ Enumpath lets you describe a path through nested Ruby objects as a string, using
8
8
 
9
9
  Paths are safe by default: a path can read your data but cannot call mutating or destructive methods such as `clear` or `destroy`. See [`:property_resolver`](#property_resolver) to allow more. See [Object traversal](#object-traversal) for examples, and [Alternatives](#alternatives) for when another gem is the better choice.
10
10
 
11
- Only accept paths from sources you trust. The default resolver stops a path from calling methods that change your data, but a hostile path can still be expensive to evaluate.
11
+ Only accept paths from sources you trust. The default resolver stops a path from calling methods that change your data, but a hostile path can still be expensive to evaluate: a regular expression in a filter can take exponential time to match (see [Known limitations](#known-limitations)).
12
12
 
13
13
  Enumpath path expressions look like this:
14
14
 
@@ -244,6 +244,15 @@ The following are all valid subscript expressions:
244
244
  - `[(length / 2)]`: the subscript becomes the index at half the length of the enumerable
245
245
  - `[(@.type)]`: the subscript becomes the value at the `type` key, member, or property of the enumerable
246
246
 
247
+ An expression that cannot be evaluated against the data, such as a division by zero or a string plus a number, matches nothing instead of raising an error. So does an expression that could use an unbounded amount of CPU or memory, because the operand comes from the path. The limits are:
248
+
249
+ - an integer operand must fit in 64 bits,
250
+ - repeating a string or array with `*` may produce at most 10,000 characters or elements,
251
+ - an integer raised to a power with `**` must produce a result of at most 1,024 bits, and
252
+ - `%` only applies to numbers (on a string it would be `format`, which can build a huge string).
253
+
254
+ These are far above anything useful for an index or a key.
255
+
247
256
  ## Usage Examples
248
257
 
249
258
  Given the same store example from the JSONPath project:
@@ -563,7 +572,7 @@ These are tracked as issues, and contributions are welcome.
563
572
  - A negative slice step such as `[::-1]` matches nothing ([issue 44](https://github.com/chrisbloom7/enumpath/issues/44)).
564
573
  - Malformed paths are not rejected: `a[` is read as `a` ([issue 45](https://github.com/chrisbloom7/enumpath/issues/45)).
565
574
  - Recursive descent raises `SystemStackError` on data nested a few thousand levels deep ([issue 46](https://github.com/chrisbloom7/enumpath/issues/46)).
566
- - Subscript arithmetic with `*` and `**` can use a lot of CPU or memory, so only accept paths you trust ([issue 52](https://github.com/chrisbloom7/enumpath/issues/52)).
575
+ - A regular expression in a filter can take exponential time to match, so only accept paths you trust ([issue 60](https://github.com/chrisbloom7/enumpath/issues/60)).
567
576
 
568
577
  ## Alternatives
569
578
 
@@ -8,6 +8,21 @@ module Enumpath
8
8
  ARITHMETIC_OPERATOR_REGEX = %r{(\+|-|\*\*|\*|\/|%)}
9
9
  OPERATOR_REGEX = /^\((.*)\)$/
10
10
 
11
+ # The operand of an arithmetic expression comes from the path, so an expression that could use an unbounded
12
+ # amount of CPU or memory is not evaluated and matches nothing. A subscript is an index or a key, so none of
13
+ # these limits get in the way of a useful expression.
14
+ #
15
+ # The widest integer operand allowed, in bits
16
+ MAX_OPERAND_BITS = 64
17
+ # The most elements or characters that repeating a string or array (`*`) may produce
18
+ MAX_REPEAT_SIZE = 10_000
19
+ # The widest result allowed for an integer raised to a power (`**`), in bits
20
+ MAX_POWER_BITS = 1_024
21
+
22
+ # Errors from evaluating an expression against data that does not fit it, such as a string plus a number or a
23
+ # division by zero. The expression matches nothing in these cases.
24
+ EVALUATION_ERRORS = [NoMethodError, TypeError, ArgumentError, ZeroDivisionError, RangeError].freeze
25
+
11
26
  class << self
12
27
  # Whether the operator matches {Enumpath::Operator::SubscriptExpression::OPERATOR_REGEX}
13
28
  #
@@ -58,13 +73,18 @@ module Enumpath
58
73
  else
59
74
  evaluate_with_operator(operator, operand, value)
60
75
  end
61
- rescue NoMethodError
76
+ rescue *EVALUATION_ERRORS
62
77
  Enumpath.log('Subscript could not be evaluated') { { subscript: nil } }
63
78
  nil
64
79
  end
65
80
 
66
81
  def evaluate_with_operator(operator, operand, value)
67
82
  typecast_operand = variable_typecaster(operand)
83
+ unless within_limits?(operator, typecast_operand, value)
84
+ Enumpath.log('Subscript exceeds the arithmetic limits') { { operator: operator, operand: typecast_operand } }
85
+ return nil
86
+ end
87
+
68
88
  result = value.public_send(operator.to_sym, typecast_operand)
69
89
  Enumpath.log('Evaluated subscript') do
70
90
  { value: value, operator: operator, operand: typecast_operand, result: result }
@@ -72,6 +92,35 @@ module Enumpath
72
92
  result
73
93
  end
74
94
 
95
+ # Whether evaluating `value <operator> operand` stays inside the limits above, before any work is done
96
+ def within_limits?(operator, operand, value)
97
+ return false if operand.is_a?(Integer) && operand.abs.bit_length > MAX_OPERAND_BITS
98
+
99
+ case operator
100
+ when '*' then repetition_within_limit?(value, operand)
101
+ when '**' then power_within_limit?(value, operand)
102
+ when '%' then value.is_a?(Numeric) # `String#%` is `format`, which can build a huge string
103
+ else true
104
+ end
105
+ end
106
+
107
+ # Repeating a string or array by an integer multiplies its size. (`Array#*` with a string joins instead.)
108
+ def repetition_within_limit?(value, operand)
109
+ return true unless operand.is_a?(Integer) && (value.is_a?(String) || value.is_a?(Array))
110
+
111
+ operand >= 0 && value.size * operand <= MAX_REPEAT_SIZE
112
+ end
113
+
114
+ # A float overflows to Infinity at once. An integer power is only allowed if its result fits in MAX_POWER_BITS,
115
+ # estimated as the bit length of the base times the exponent. Any other kind of number is not allowed.
116
+ def power_within_limit?(value, operand)
117
+ case value
118
+ when Integer then operand.is_a?(Integer) && value.abs.bit_length * operand.abs <= MAX_POWER_BITS
119
+ when Float then true
120
+ else false
121
+ end
122
+ end
123
+
75
124
  def variable_typecaster(variable)
76
125
  if variable =~ /\A('|").+\1\z/ || variable =~ /^:.+/
77
126
  # It quacks like a string or symbol
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enumpath
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumpath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Bloom