sqreen 1.15.6 → 1.15.7.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2954f152a5de09e1e76229fb51bdd4de43c3368a0c147e9d6df5e718b8ef7db2
4
- data.tar.gz: 73675a112d80f502a9cbef995eacd48aa53aa7919064b49f0c56faaed2bfceef
3
+ metadata.gz: 4c12b98d858bc3b796b65d4cbda2f57306371958a84ef0cba7e418279527fdc2
4
+ data.tar.gz: 1d4e7f4994731ca11be8f5a9d14c0fc302072499490549676376ec9af811f024
5
5
  SHA512:
6
- metadata.gz: 984f765a0d7995d49cda4515df0d6bed2c2390b38e713e927af566ccd71e0d5cb22c05b2a8c2cde7a225e8ec146d2e42ed80ceb7775b6e11939819422250918f
7
- data.tar.gz: 0bf04fb5f2fe03e0b6614e0f2c06f9cacba288bd553c98fa12b7a5bf258eafb32bac45624cc60210a8e8d2d168a19eb6eb3d72c494d066a038eac4acd44fd355
6
+ metadata.gz: ae84a664f4676c90d088f36b57db1b5de7d324df684bb544391489ac037ed8788dcc76f64f3195b04e8b6d57da1bbb990895681eedebb10508d6c0a825c41533
7
+ data.tar.gz: dbbe4acbc9d91b8fd79f8d1cbc20223220abcd5cbcb36b505ccdebf8b664b3dc24ecd4b8c78b852c56bff64c197eeeb3aa7a21544abf0a0a2681fa8dfe00e0cf
@@ -1,4 +1,5 @@
1
1
  require 'digest'
2
+ require 'json'
2
3
 
3
4
  module Sqreen
4
5
  module Js
@@ -63,6 +64,89 @@ module Sqreen
63
64
  end
64
65
  end
65
66
 
67
+ class JsonConversion
68
+ class << self
69
+ if defined?(::JSON::Ext::Generator::State)
70
+ def convert_arguments(_args)
71
+ args = purge_dangerous_objs(_args)
72
+ new_state.generate(args)
73
+ rescue ::JSON::GeneratorError, ::Encoding::UndefinedConversionError
74
+ fixed_args = fixup_bad_encoding(args)
75
+ new_state.generate(fixed_args)
76
+ end
77
+ else
78
+ def convert_arguments(_args)
79
+ args = purge_dangerous_objs(_args)
80
+ args.to_json(new_state)
81
+ rescue ::EncodingError
82
+ fixed_args = fixup_bad_encoding(args)
83
+ fixed_args.to_json(new_state)
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def new_state
90
+ ::JSON::SAFE_STATE_PROTOTYPE.dup
91
+ end
92
+
93
+ def fixup_bad_encoding(arg, max_depth = 100)
94
+ return nil if max_depth <= 0
95
+
96
+ if arg.is_a?(::Array)
97
+ return arg.map { |it| fixup_bad_encoding(it, max_depth - 1) }
98
+ end
99
+
100
+ if arg.is_a?(::Hash)
101
+ return ::Hash[
102
+ arg.map do |k, v|
103
+ [fixup_bad_encoding(k, max_depth - 1),
104
+ fixup_bad_encoding(v, max_depth - 1)]
105
+ end
106
+ ]
107
+ end
108
+
109
+ return arg unless arg.is_a?(::String)
110
+
111
+ unless arg.valid_encoding?
112
+ return arg.dup.force_encoding(::Encoding::ISO_8859_1)
113
+ end
114
+
115
+ # encoding is valid if it reaches this point
116
+ return arg if arg.encoding == ::Encoding::UTF_8
117
+
118
+ begin
119
+ arg.encode(::Encoding::UTF_8)
120
+ rescue ::Encoding::UndefinedConversionError
121
+ arg.dup.force_encoding(::Encoding::ISO_8859_1)
122
+ end
123
+ end
124
+
125
+ def purge_dangerous_objs(arg, max_depth = 100)
126
+ return nil if max_depth <= 0
127
+
128
+ if arg.is_a?(::Array)
129
+ arg.map { |it| purge_dangerous_objs(it, max_depth - 1) }
130
+ elsif arg.is_a?(::Hash)
131
+ Hash[
132
+ arg.map do |k, v|
133
+ [purge_dangerous_objs(k, max_depth - 1),
134
+ purge_dangerous_objs(v, max_depth - 1)]
135
+ end
136
+ ]
137
+ elsif arg.is_a?(::String) || arg.nil? || arg == false ||
138
+ arg == true || arg.is_a?(::Fixnum) || arg.is_a?(::Bignum) ||
139
+ arg.is_a?(::Float)
140
+ arg
141
+ elsif arg.respond_to?(:to_json)
142
+ arg.to_s
143
+ else
144
+ arg
145
+ end
146
+ end
147
+ end
148
+ end
149
+
66
150
  class MiniRacerExecutableJs < ExecutableJs
67
151
  @@ctx_defined = false
68
152
 
@@ -93,7 +177,7 @@ module Sqreen
93
177
  ctx.add_code(@code_id, @code) unless ctx.has_code?(@code_id)
94
178
 
95
179
  begin
96
- json_args = convert_arguments(arguments)
180
+ json_args = JsonConversion.convert_arguments(arguments)
97
181
  ctx.eval_unsafe(
98
182
  "sqreen_data['#{@code_id}']['#{cb_name}'].apply(this, #{json_args})", nil, budget)
99
183
  rescue @module::ScriptTerminatedError
@@ -109,45 +193,6 @@ module Sqreen
109
193
 
110
194
  private
111
195
 
112
- def convert_arguments(args)
113
- JSON.generate(args)
114
- rescue JSON::GeneratorError, Encoding::UndefinedConversionError
115
- fixed_args = fixup_bad_encoding(args)
116
- JSON.generate(fixed_args)
117
- end
118
-
119
- def fixup_bad_encoding(arg, max_depth = 100)
120
- return nil if max_depth <= 0
121
-
122
- if arg.is_a?(Array)
123
- return arg.map { |it| fixup_bad_encoding(it, max_depth - 1) }
124
- end
125
-
126
- if arg.is_a?(Hash)
127
- return Hash[
128
- arg.map do |k, v|
129
- [fixup_bad_encoding(k, max_depth - 1),
130
- fixup_bad_encoding(v, max_depth - 1)]
131
- end
132
- ]
133
- end
134
-
135
- return arg unless arg.is_a?(String)
136
-
137
- unless arg.valid_encoding?
138
- return arg.dup.force_encoding(Encoding::ISO_8859_1)
139
- end
140
-
141
- # encoding is valid if it reaches this point
142
- return arg if arg.encoding == Encoding::UTF_8
143
-
144
- begin
145
- arg.encode(Encoding::UTF_8)
146
- rescue Encoding::UndefinedConversionError
147
- arg.dup.force_encoding(Encoding::ISO_8859_1)
148
- end
149
- end
150
-
151
196
  class << self
152
197
  def define_sqreen_context(modoole)
153
198
  # Context specialized for Sqreen usage
@@ -1,5 +1,5 @@
1
1
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
2
  # Please refer to our terms for more information: https://www.sqreen.io/terms.html
3
3
  module Sqreen
4
- VERSION = '1.15.6'.freeze
4
+ VERSION = '1.15.7.beta1'.freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqreen
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.15.6
4
+ version: 1.15.7.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sqreen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-21 00:00:00.000000000 Z
11
+ date: 2018-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sq_mini_racer
@@ -134,9 +134,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
134
  version: '0'
135
135
  required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  requirements:
137
- - - ">="
137
+ - - ">"
138
138
  - !ruby/object:Gem::Version
139
- version: '0'
139
+ version: 1.3.1
140
140
  requirements: []
141
141
  rubyforge_project:
142
142
  rubygems_version: 2.7.7