jsobfu 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +8 -8
  2. data/lib/jsobfu.rb +27 -14
  3. data/spec/jsobfu_spec.rb +23 -3
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzdlMDRlNzdmNTVjM2Y2NTk0NGIyNjZmOGFjMGU4YWZlOGNhNmU5OA==
4
+ M2RiZjA5MGU2NDVkNDk1NmI5NDc4ZWM2YTkyM2JhMWU3YzNjN2Q1Mg==
5
5
  data.tar.gz: !binary |-
6
- MTJjMzU2ZDJjMGU2ODZkZjhkNWNhYjBhYmQ5MWMyNDUwNzY2MWM3Ng==
6
+ MmEzZjk4NjY5MWExOWY3ODNjNTNhMGI3N2QwNDhiZWM3NjViM2VjNA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZmQ0MjBiZWMyZDhkMWJkOWY1MmU3MmRjNmQwNDhhODYyMzFmMDU2ZTY3MzNi
10
- YmQ2Y2E1MjhlNTI1ZTBhYTZlY2YzYWVlZTUwZmRmZDljNWEwM2M2ZTI0YTRk
11
- ZGM4MjQwMzhhOTZlMzc0Y2YzYmNmNDcxNmRjYzU1YzY1ZDM1ZmU=
9
+ NzUwYzZhNzIzZTQxYzcxMGE1NTkyMTYwZjMzNGJhZDViYmZmYjM0NzY5ODY4
10
+ NGFiODQxYWNkYzViMDI4NjYyYTg3OTAwNTg3YTY1NjRjNzQ2ZTE1ZDViMDdj
11
+ ZmMyMGVhZjljODlkZTU0ZTYzZTljM2M2OWE2MmU1YjQzZjQ4NDE=
12
12
  data.tar.gz: !binary |-
13
- ZmE4Yjg0YWE4OGNkMTdjYzc0ODg2MmFjZDJlMGEzZDM0YmZkNTQ2ZTAzZGMy
14
- MDE5YThmNTNlYTg2Y2Q5YjM4MmE1ZDI4Y2NiNTgxMWE4ZjhhZTBmMTYwNzVi
15
- MTE5NDczMjE0MzcyMGRhZDEyNGZlNmFkZTIxNjE2MGYwNDZkZTI=
13
+ YzhjMDk4NDY3MGU2OWQ1NzM4MGJhOWQ0NjQ2ZDk1ZGE3ZDUyOThlYjA4NmM4
14
+ NmRjOWEyMTUyMjc2YmNhYzAyYzE4Y2Q5MzExYzM1MzY1MjE2MGUwNjNmMTdk
15
+ YzQ5ZTQ2MzZjZTc4NjJlMzEzMWJmNDc2NzAzOTRiOTVlODc1Njc=
@@ -14,12 +14,9 @@ class JSObfu
14
14
 
15
15
  # Saves +code+ for later obfuscation with #obfuscate
16
16
  # @param code [#to_s] the code to obfuscate
17
- # @param opts [Hash] the options hash
18
- # @option opts [Integer] number of times to run the obfuscator on this code (1)
19
- def initialize(code, opts={})
17
+ def initialize(code)
20
18
  @code = code.to_s
21
19
  @scope = Scope.new
22
- @iterations = opts.fetch(:iterations, 1).to_i
23
20
  end
24
21
 
25
22
  # Add +str+ to the un-obfuscated code.
@@ -41,22 +38,38 @@ class JSObfu
41
38
  # Parse and obfuscate
42
39
  #
43
40
  # @param opts [Hash] the options hash
44
- # @option opts [Boolean] :strip_whitespace allow whitespace in the output code
45
- #
41
+ # @option opts [Boolean] :strip_whitespace removes unnecessary whitespace from
42
+ # the output code (true)
43
+ # @option opts [Integer] :iterations number of times to run the
44
+ # obfuscator on this code (1)
46
45
  # @return [String] if successful
47
46
  def obfuscate(opts={})
48
- @iterations.times do |i|
49
- @obfuscator = JSObfu::Obfuscator.new(scope: @scope)
50
- @code = @obfuscator.accept(ast).to_s
51
- if opts.fetch(:strip_whitespace, true)
47
+ iterations = opts.fetch(:iterations, 1).to_i
48
+ strip_whitespace = opts.fetch(:strip_whitespace, true)
49
+
50
+ iterations.times do |i|
51
+ obfuscator = JSObfu::Obfuscator.new(scope: @scope)
52
+ @code = obfuscator.accept(ast).to_s
53
+ if strip_whitespace
52
54
  @code.gsub!(/(^\s+|\s+$)/, '')
53
55
  @code.delete!("\n")
54
56
  @code.delete!("\r")
55
57
  end
56
58
 
57
- unless i == @iterations-1
59
+ new_renames = obfuscator.renames.dup
60
+ if @renames
61
+ # "patch up" the renames after each iteration
62
+ @renames.each do |key, prev_rename|
63
+ @renames[key] = new_renames[prev_rename]
64
+ end
65
+ else
66
+ # on first iteration, take the renames as-is
67
+ @renames = new_renames
68
+ end
69
+
70
+ unless i == iterations-1
58
71
  @scope = Scope.new
59
- @ast = nil
72
+ @ast = nil # force a re-parse
60
73
  end
61
74
  end
62
75
 
@@ -68,8 +81,8 @@ class JSObfu
68
81
  # @param [String] sym the name of the variable or function
69
82
  # @return [String] the obfuscated name
70
83
  def sym(sym)
71
- raise RuntimeError, "Must obfuscate before calling #sym" if @obfuscator.nil?
72
- @obfuscator.renames[sym.to_s]
84
+ raise RuntimeError, "Must obfuscate before calling #sym" if @renames.nil?
85
+ @renames[sym.to_s]
73
86
  end
74
87
 
75
88
  protected
@@ -5,9 +5,13 @@ describe JSObfu do
5
5
  TEST_STRING = 'var x; function y() {};'
6
6
 
7
7
  subject(:jsobfu) do
8
- instance = described_class.new(TEST_STRING)
9
- instance.obfuscate
10
- instance
8
+ described_class.new(TEST_STRING)
9
+ end
10
+
11
+ let(:iterations) { 1 }
12
+
13
+ before do
14
+ jsobfu.obfuscate(iterations: iterations)
11
15
  end
12
16
 
13
17
  describe '#sym' do
@@ -22,6 +26,22 @@ describe JSObfu do
22
26
  expect(jsobfu.sym('YOLOSWAG')).to be_nil
23
27
  end
24
28
  end
29
+
30
+ context 'when iterations: 2 is passed to obfuscate()' do
31
+ let(:iterations) { 2 }
32
+
33
+ context 'when given the string "x"' do
34
+ it 'returns some string' do
35
+ expect(jsobfu.sym('x')).not_to be_nil
36
+ end
37
+ end
38
+
39
+ context 'when given the string "YOLOSWAG"' do
40
+ it 'returns nil' do
41
+ expect(jsobfu.sym('YOLOSWAG')).to be_nil
42
+ end
43
+ end
44
+ end
25
45
  end
26
46
 
27
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsobfu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Lee