Module: PWN::JSONText

Defined in:
lib/pwn/json_text.rb

Overview

json 2.21 warns when valid UTF-8 is still tagged BINARY, and raises JSON::GeneratorError on a malformed sequence. Both show up in the session pane.

Class Method Summary collapse

Class Method Details

.authors ⇒ Object

Author(s)

0day Inc. [email protected]



38
39
40
# File 'lib/pwn/json_text.rb', line 38

public_class_method def self.authors
  "AUTHOR(S):\n  0day Inc. <[email protected]>\n"
end

.help ⇒ Object

Display Usage for this Module.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pwn/json_text.rb', line 44

public_class_method def self.help
  puts "USAGE:
    # Retag valid UTF-8 that is still tagged binary, or scrub an illegal sequence.
    #{self}.prepare(
      value: 'required - string, hash, or array that may contain binary-tagged text',
      depth: 'optional - nesting already walked; containers stop at 100'
    )

    # Print the AUTHOR(S) string for this module.
    #{self}.authors
  "
  constants.sort
end

.prepare(opts = {}) ⇒ Object

Supported Method Parameters

safe = PWN::JSONText.prepare( value: 'required - string, hash, or array that may contain binary-tagged text', depth: 'optional - nesting already walked; containers stop at 100' )



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pwn/json_text.rb', line 14

public_class_method def self.prepare(opts = {})
  value = opts[:value]
  depth = opts[:depth].to_i
  case value
  when String
    utf = value.encoding == Encoding::BINARY ? value.dup.force_encoding(Encoding::UTF_8) : value.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
    utf.valid_encoding? ? utf : utf.scrub
  when Hash
    raise JSON::NestingError, 'JSON text exceeds container nesting limit (100)' if depth >= 100

    value.each_with_object({}) do |(key, item), out|
      out[key.is_a?(String) ? prepare(value: key, depth: depth + 1) : key] = prepare(value: item, depth: depth + 1)
    end
  when Array
    raise JSON::NestingError, 'JSON text exceeds container nesting limit (100)' if depth >= 100

    value.map { |item| prepare(value: item, depth: depth + 1) }
  else
    value
  end
end