cassette_guard 0.1.0
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 +7 -0
- data/CHANGELOG.md +15 -0
- data/LICENSE.txt +21 -0
- data/README.md +454 -0
- data/exe/cassette_guard +6 -0
- data/lib/cassette_guard/baseline.rb +59 -0
- data/lib/cassette_guard/body.rb +100 -0
- data/lib/cassette_guard/cassette.rb +81 -0
- data/lib/cassette_guard/cli.rb +309 -0
- data/lib/cassette_guard/config.rb +106 -0
- data/lib/cassette_guard/detectors/age.rb +46 -0
- data/lib/cassette_guard/detectors/pii.rb +45 -0
- data/lib/cassette_guard/detectors/secret.rb +83 -0
- data/lib/cassette_guard/detectors/size.rb +28 -0
- data/lib/cassette_guard/detectors/usage.rb +45 -0
- data/lib/cassette_guard/encodings.rb +79 -0
- data/lib/cassette_guard/fake_value.rb +103 -0
- data/lib/cassette_guard/fields.rb +74 -0
- data/lib/cassette_guard/finding.rb +33 -0
- data/lib/cassette_guard/finding_group.rb +37 -0
- data/lib/cassette_guard/format.rb +37 -0
- data/lib/cassette_guard/headers.rb +33 -0
- data/lib/cassette_guard/line_locator.rb +37 -0
- data/lib/cassette_guard/py_yaml.rb +228 -0
- data/lib/cassette_guard/reporters/json.rb +24 -0
- data/lib/cassette_guard/reporters/sarif.rb +67 -0
- data/lib/cassette_guard/reporters/text.rb +76 -0
- data/lib/cassette_guard/rules.rb +127 -0
- data/lib/cassette_guard/scanner.rb +131 -0
- data/lib/cassette_guard/scrubber.rb +138 -0
- data/lib/cassette_guard/trimmer.rb +70 -0
- data/lib/cassette_guard/usage.rb +47 -0
- data/lib/cassette_guard/version.rb +5 -0
- data/lib/cassette_guard.rb +40 -0
- metadata +84 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CassetteGuard
|
|
4
|
+
module Detectors
|
|
5
|
+
class Size
|
|
6
|
+
def initialize(config)
|
|
7
|
+
@config = config
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def file(path, cassette)
|
|
11
|
+
size = File.size(path)
|
|
12
|
+
return [] if size <= @config.max_file_size
|
|
13
|
+
|
|
14
|
+
message = "cassette is #{Format.bytes(size)} (limit #{Format.bytes(@config.max_file_size)})"
|
|
15
|
+
[Finding.new(rule: :size, kind: "file", cassette:, message:)]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def body(body, cassette, interaction, location)
|
|
19
|
+
size = body.size
|
|
20
|
+
return [] if size <= @config.max_body_size
|
|
21
|
+
|
|
22
|
+
encoding = body.base64? ? " base64" : ""
|
|
23
|
+
message = "#{Format.bytes(size)}#{encoding} (#{body.content_type || "no content type"})"
|
|
24
|
+
[Finding.new(rule: :size, kind: "body", cassette:, interaction:, location:, message:)]
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CassetteGuard
|
|
4
|
+
module Detectors
|
|
5
|
+
# Reads the files written by CassetteGuard::Usage.track! during a test run.
|
|
6
|
+
class Usage
|
|
7
|
+
def initialize(config)
|
|
8
|
+
@config = config
|
|
9
|
+
@dir = File.expand_path(config.usage_dir, config.root)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def available?
|
|
13
|
+
files.any?
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call(paths)
|
|
17
|
+
paths.reject { |path| used.include?(canonical(path)) }.map do |path|
|
|
18
|
+
Finding.new(
|
|
19
|
+
rule: :unused, kind: "unused", cassette: @config.relative(path),
|
|
20
|
+
message: "not loaded by any example in the tracked run",
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def files
|
|
28
|
+
@files ||= Dir.glob(File.join(@dir, "usage-*.txt"))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def used
|
|
32
|
+
@used ||= files.flat_map { |file| File.readlines(file, chomp: true) }
|
|
33
|
+
.reject(&:empty?)
|
|
34
|
+
.to_set { |line| canonical(File.expand_path(line, @config.root)) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Symlinked checkouts (and macOS's /var -> /private/var) must not make a used cassette look unused.
|
|
38
|
+
def canonical(path)
|
|
39
|
+
File.realpath(path)
|
|
40
|
+
rescue SystemCallError
|
|
41
|
+
File.expand_path(path)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CassetteGuard
|
|
4
|
+
# The same value can be written several ways inside a cassette; scrubbing must find all of them.
|
|
5
|
+
module Encodings
|
|
6
|
+
XML_ENTITIES = { "amp" => "&", "lt" => "<", "gt" => ">", "quot" => '"', "apos" => "'" }.freeze
|
|
7
|
+
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
def json(value, ascii_only: false)
|
|
11
|
+
encoded = JSON.generate(value.to_s, ascii_only:)[1..-2]
|
|
12
|
+
ascii_only ? python_json(encoded) : encoded
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Python's json.dumps escapes DEL along with everything outside printable ASCII; Ruby's ascii_only
|
|
16
|
+
# leaves it raw. A raw DEL can only sit inside a JSON string, so this is safe on whole documents.
|
|
17
|
+
def python_json(encoded)
|
|
18
|
+
encoded.gsub("\x7F", "\\u007f")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def xml(value)
|
|
22
|
+
value.to_s.gsub("&", "&").gsub("<", "<").gsub(">", ">").gsub('"', """)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def xml_unescape(value)
|
|
26
|
+
value.gsub(/&(amp|lt|gt|quot|apos|#\d+|#x\h+);/) do
|
|
27
|
+
name = Regexp.last_match(1)
|
|
28
|
+
if name.start_with?("#x") then name[2..].to_i(16).chr(Encoding::UTF_8)
|
|
29
|
+
elsif name.start_with?("#") then name[1..].to_i.chr(Encoding::UTF_8)
|
|
30
|
+
else XML_ENTITIES.fetch(name)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
rescue RangeError
|
|
34
|
+
value
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def www_form(value)
|
|
38
|
+
URI.encode_www_form_component(value.to_s)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def uri_component(value)
|
|
42
|
+
URI.encode_uri_component(value.to_s)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# [[original, replacement], ...] for every encoding the value may appear in.
|
|
46
|
+
def variants(value, replacement)
|
|
47
|
+
[
|
|
48
|
+
[value, replacement],
|
|
49
|
+
[json(value), json(replacement)],
|
|
50
|
+
[json(value, ascii_only: true), json(replacement, ascii_only: true)],
|
|
51
|
+
[json(value).gsub("/", "\\/"), json(replacement).gsub("/", "\\/")],
|
|
52
|
+
[www_form(value), www_form(replacement)],
|
|
53
|
+
[uri_component(value), uri_component(replacement)],
|
|
54
|
+
[xml(value), xml(replacement)],
|
|
55
|
+
].uniq(&:first).reject { |original, _| original.empty? }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Patterns that only match the value where it sits next to its key, each capturing the prefix in group 1.
|
|
59
|
+
def keyed_patterns(key, value, replacement)
|
|
60
|
+
quoted_key = Regexp.escape(key)
|
|
61
|
+
patterns = [false, true].uniq { |ascii_only| json(value, ascii_only:) }.map do |ascii_only|
|
|
62
|
+
[/("#{quoted_key}"\s*:\s*")#{Regexp.escape(json(value, ascii_only:))}(?=")/, json(replacement, ascii_only:)]
|
|
63
|
+
end
|
|
64
|
+
patterns += [
|
|
65
|
+
[%r{(<(?:[\w.-]+:)?#{quoted_key}(?:\s[^<>]*)?>\s*)#{Regexp.escape(xml(value))}(?=\s*</)}, xml(replacement)],
|
|
66
|
+
[/(\s(?:[\w.-]+:)?#{quoted_key}\s*=\s*["'])#{Regexp.escape(xml(value))}(?=["'])/, xml(replacement)],
|
|
67
|
+
]
|
|
68
|
+
if value.match?(/\A-?\d+\z/)
|
|
69
|
+
patterns << [/("#{quoted_key}"\s*:\s*)#{Regexp.escape(value)}(?=\s*[,}\]])/, replacement]
|
|
70
|
+
end
|
|
71
|
+
[key, www_form(key), uri_component(key)].uniq.each do |encoded_key|
|
|
72
|
+
[[www_form(value), www_form(replacement)], [uri_component(value), uri_component(replacement)]].uniq.each do |from, to|
|
|
73
|
+
patterns << [/((?:\A|[?&;])#{Regexp.escape(encoded_key)}=)#{Regexp.escape(from)}(?=[&;#]|\z)/, to]
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
patterns
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
|
|
5
|
+
module CassetteGuard
|
|
6
|
+
# Deterministic, format-preserving fakes: the same input and salt always give the same output,
|
|
7
|
+
# so a value shared between cassettes (a booking reference, a token) stays consistent.
|
|
8
|
+
class FakeValue
|
|
9
|
+
DATE_FORMATS = [
|
|
10
|
+
[/\A\d{4}-\d{2}-\d{2}(.*)\z/m, ->(year, month, day) { format("%04d-%02d-%02d", year, month, day) }],
|
|
11
|
+
[%r{\A\d{2}/\d{2}/\d{4}(.*)\z}m, ->(year, month, day) { format("%02d/%02d/%04d", day, month, year) }],
|
|
12
|
+
[/\A\d{2}\.\d{2}\.\d{4}(.*)\z/m, ->(year, month, day) { format("%02d.%02d.%04d", day, month, year) }],
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
15
|
+
def initialize(salt)
|
|
16
|
+
@salt = salt.to_s
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def for(value, rule:)
|
|
20
|
+
value = value.to_s
|
|
21
|
+
return secret(value) if rule == :secret
|
|
22
|
+
return email(value) if Rules.email?(value)
|
|
23
|
+
return iban(value) if Rules.iban?(value)
|
|
24
|
+
return card(value) if value.delete(" -").length >= 13 && Rules.luhn?(value)
|
|
25
|
+
|
|
26
|
+
date(value) || charwise(value)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def secret(value)
|
|
30
|
+
length = [value.length - Rules::SCRUB_PREFIX.length, 8].max
|
|
31
|
+
"#{Rules::SCRUB_PREFIX}#{hex(value, length)}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def email(value)
|
|
35
|
+
"user-#{hex(value, 8)}@example.com"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def iban(value)
|
|
39
|
+
compact = value.delete(" ").upcase
|
|
40
|
+
country = compact[0, 2]
|
|
41
|
+
bban = charwise(compact[4..], seed: value)
|
|
42
|
+
check = 98 - Rules.iban_remainder("#{bban}#{country}00")
|
|
43
|
+
fake = "#{country}#{format("%02d", check)}#{bban}"
|
|
44
|
+
value.include?(" ") ? fake.scan(/.{1,4}/).join(" ") : fake
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def card(value)
|
|
48
|
+
digits = charwise(value.delete(" -"))[0..-2]
|
|
49
|
+
digits += (0..9).find { |check| Rules.luhn?("#{digits}#{check}") }.to_s
|
|
50
|
+
remaining = digits.each_char
|
|
51
|
+
value.gsub(/\d/) { remaining.next }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Day and month both stay within 1..12, so the date is valid whichever order the format uses.
|
|
55
|
+
def date(value)
|
|
56
|
+
DATE_FORMATS.each do |pattern, build|
|
|
57
|
+
match = pattern.match(value)
|
|
58
|
+
next unless match
|
|
59
|
+
|
|
60
|
+
random = bytes(value, 3)
|
|
61
|
+
year = 1940 + (random.getbyte(0) % 66)
|
|
62
|
+
month = 1 + (random.getbyte(1) % 12)
|
|
63
|
+
day = 1 + (random.getbyte(2) % 12)
|
|
64
|
+
return "#{build.call(year, month, day)}#{match[1]}"
|
|
65
|
+
end
|
|
66
|
+
nil
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def charwise(value, seed: value)
|
|
70
|
+
3.times do |attempt|
|
|
71
|
+
random = bytes("#{seed}#{"!" * attempt}", value.length)
|
|
72
|
+
fake = value.each_char.with_index.map { |char, index| fake_char(char, index, random.getbyte(index)) }.join
|
|
73
|
+
return fake unless fake == value
|
|
74
|
+
end
|
|
75
|
+
value
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
def fake_char(char, index, byte)
|
|
81
|
+
case char
|
|
82
|
+
when /[0-9]/ then index.zero? && char != "0" ? (1 + (byte % 9)).to_s : (byte % 10).to_s
|
|
83
|
+
when /[A-Z]/, /[[:upper:]]/ then (65 + (byte % 26)).chr
|
|
84
|
+
when /[[:lower:]]/ then (97 + (byte % 26)).chr
|
|
85
|
+
else char
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def bytes(seed, count)
|
|
90
|
+
out = String.new(capacity: count, encoding: Encoding::BINARY)
|
|
91
|
+
counter = 0
|
|
92
|
+
while out.bytesize < count
|
|
93
|
+
out << OpenSSL::HMAC.digest("SHA256", @salt, "#{counter}:#{seed}")
|
|
94
|
+
counter += 1
|
|
95
|
+
end
|
|
96
|
+
out.byteslice(0, count)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def hex(seed, length)
|
|
100
|
+
bytes(seed, (length + 1) / 2).unpack1("H*")[0, length]
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CassetteGuard
|
|
4
|
+
Field = Data.define(:path, :key, :value)
|
|
5
|
+
|
|
6
|
+
# Splits a body into (path, key, value) leaves so detectors can use key names as a signal.
|
|
7
|
+
module Fields
|
|
8
|
+
ELEMENT = %r{<((?:[\w.-]+:)?([\w.-]+))(?:\s[^<>]*)?>([^<>]*)</\1\s*>}
|
|
9
|
+
OPEN_TAG = %r{<[\w.:-]+((?:\s+[^<>]*?)?)/?>}
|
|
10
|
+
ATTRIBUTE = /((?:[\w.-]+:)?([\w.-]+))\s*=\s*"([^"]*)"/
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def from_text(text, content_type = nil)
|
|
15
|
+
return [] if text.nil? || text.empty?
|
|
16
|
+
|
|
17
|
+
json = parse_json(text, content_type)
|
|
18
|
+
return walk(json, "$", nil, []) unless json.nil?
|
|
19
|
+
return form(text) if content_type.to_s.include?("x-www-form-urlencoded")
|
|
20
|
+
return xml(text) if xml?(text, content_type)
|
|
21
|
+
|
|
22
|
+
[Field.new(path: "", key: nil, value: text)]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def parse_json(text, content_type)
|
|
26
|
+
return nil unless content_type.to_s.include?("json") || text.lstrip.start_with?("{", "[")
|
|
27
|
+
|
|
28
|
+
JSON.parse(text)
|
|
29
|
+
rescue JSON::ParserError
|
|
30
|
+
nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def walk(node, path, key, out)
|
|
34
|
+
case node
|
|
35
|
+
when Hash then node.each { |child_key, child| walk(child, "#{path}#{segment(child_key)}", child_key, out) }
|
|
36
|
+
when Array then node.each_with_index { |child, index| walk(child, "#{path}[#{index}]", key, out) }
|
|
37
|
+
when String, Integer then out << Field.new(path:, key:, value: node.to_s)
|
|
38
|
+
end
|
|
39
|
+
out
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def segment(key)
|
|
43
|
+
key.to_s.match?(/\A[A-Za-z_][\w-]*\z/) ? ".#{key}" : "[#{key.to_s.to_json}]"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def form(text)
|
|
47
|
+
URI.decode_www_form(text).map { |key, value| Field.new(path: "form.#{key}", key:, value:) }
|
|
48
|
+
rescue ArgumentError
|
|
49
|
+
[Field.new(path: "", key: nil, value: text)]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def xml?(text, content_type)
|
|
53
|
+
return false if content_type.to_s.include?("html")
|
|
54
|
+
|
|
55
|
+
content_type.to_s.include?("xml") || text.lstrip.start_with?("<")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def xml(text)
|
|
59
|
+
fields = []
|
|
60
|
+
text.scan(ELEMENT) do |_qualified, local, value|
|
|
61
|
+
decoded = Encodings.xml_unescape(value).strip
|
|
62
|
+
fields << Field.new(path: "xml <#{local}>", key: local, value: decoded) unless decoded.empty?
|
|
63
|
+
end
|
|
64
|
+
text.scan(OPEN_TAG) do
|
|
65
|
+
Regexp.last_match(1).to_s.scan(ATTRIBUTE) do |qualified, local, value|
|
|
66
|
+
next if qualified.start_with?("xmlns")
|
|
67
|
+
|
|
68
|
+
fields << Field.new(path: "xml @#{local}", key: local, value: Encodings.xml_unescape(value))
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
fields
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CassetteGuard
|
|
4
|
+
Finding = Data.define(:rule, :kind, :cassette, :interaction, :location, :message, :value, :key) do
|
|
5
|
+
def initialize(rule:, kind:, cassette:, interaction: nil, location: nil, message: nil, value: nil, key: nil)
|
|
6
|
+
super
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# Stable across re-orderings of interactions; changes when the recorded value changes.
|
|
10
|
+
def fingerprint
|
|
11
|
+
value_digest = value && Digest::SHA256.hexdigest(value.to_s)
|
|
12
|
+
Digest::SHA256.hexdigest([rule, kind, cassette, location, value_digest].join("\0"))[0, 16]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def sensitive?
|
|
16
|
+
%i[secret pii].include?(rule)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def detail
|
|
20
|
+
sensitive? ? "#{kind} #{Format.mask(value)}" : message.to_s
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def where
|
|
24
|
+
[interaction && "##{interaction}", location].compact.join(" ")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def as_json
|
|
28
|
+
{
|
|
29
|
+
rule: rule.to_s, kind:, cassette: cassette.to_s.scrub, interaction:, location:, detail:, fingerprint:,
|
|
30
|
+
}.compact
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CassetteGuard
|
|
4
|
+
# One finding as reports show it: the same value at the same place in every interaction of a cassette
|
|
5
|
+
# (the same password sent with each of six requests) is one finding with six occurrences.
|
|
6
|
+
FindingGroup = Data.define(:finding, :interactions) do
|
|
7
|
+
def self.from(findings)
|
|
8
|
+
findings.group_by { |finding| [finding.cassette, finding.fingerprint] }.map do |_, group|
|
|
9
|
+
new(finding: group.first, interactions: group.filter_map(&:interaction).uniq.sort)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def interaction = interactions.first
|
|
14
|
+
def rule = finding.rule
|
|
15
|
+
def kind = finding.kind
|
|
16
|
+
def cassette = finding.cassette
|
|
17
|
+
def location = finding.location
|
|
18
|
+
def value = finding.value
|
|
19
|
+
def detail = finding.detail
|
|
20
|
+
def fingerprint = finding.fingerprint
|
|
21
|
+
def sensitive? = finding.sensitive?
|
|
22
|
+
|
|
23
|
+
def occurrences
|
|
24
|
+
[interactions.size, 1].max
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def where
|
|
28
|
+
return location.to_s if interactions.empty?
|
|
29
|
+
|
|
30
|
+
["##{Format.ranges(interactions)}", location].compact.join(" ")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def as_json
|
|
34
|
+
finding.as_json.except(:interaction).merge(interactions:, occurrences:).compact
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CassetteGuard
|
|
4
|
+
module Format
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def bytes(count)
|
|
8
|
+
return "#{count} B" if count < 1024
|
|
9
|
+
return "#{(count / 1024.0).round} KB" if count < 1024 * 1024
|
|
10
|
+
|
|
11
|
+
"#{(count / 1024.0 / 1024).round(1)} MB"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def mask(value)
|
|
15
|
+
text = value.to_s
|
|
16
|
+
prefix = text.length > 4 ? text[0, 2] : ""
|
|
17
|
+
"#{prefix}… (#{text.length} chars)"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def number(count)
|
|
21
|
+
count.to_s.reverse.scan(/\d{1,3}/).join(",").reverse
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# [0, 1, 2, 5, 7, 8] -> "0-2,5,7-8"; past four runs the rest is elided.
|
|
25
|
+
def ranges(numbers)
|
|
26
|
+
runs = numbers.sort.uniq.slice_when { |a, b| b != a + 1 }.map do |run|
|
|
27
|
+
run.size > 1 ? "#{run.first}-#{run.last}" : run.first.to_s
|
|
28
|
+
end
|
|
29
|
+
text = runs.first(4).join(",")
|
|
30
|
+
runs.size > 4 ? "#{text},…" : text
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def plural(count, word)
|
|
34
|
+
"#{number(count)} #{count == 1 ? word : "#{word}s"}"
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CassetteGuard
|
|
4
|
+
module Headers
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def get(headers, name)
|
|
8
|
+
return nil unless headers.is_a?(Hash)
|
|
9
|
+
|
|
10
|
+
pair = headers.find { |key, _| key.to_s.casecmp?(name) }
|
|
11
|
+
pair && Array(pair.last).first&.to_s
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def delete(headers, name)
|
|
15
|
+
return unless headers.is_a?(Hash)
|
|
16
|
+
|
|
17
|
+
headers.delete_if { |key, _| key.to_s.casecmp?(name) }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Only touches Content-Length when it described the body we are replacing.
|
|
21
|
+
def update_content_length(headers, old_size, new_size)
|
|
22
|
+
return unless headers.is_a?(Hash)
|
|
23
|
+
|
|
24
|
+
name = headers.keys.find { |key| key.to_s.casecmp?("content-length") }
|
|
25
|
+
return unless name
|
|
26
|
+
|
|
27
|
+
current = headers[name]
|
|
28
|
+
return unless Array(current).first.to_s == old_size.to_s
|
|
29
|
+
|
|
30
|
+
headers[name] = current.is_a?(Array) ? [new_size.to_s] : new_size.to_s
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CassetteGuard
|
|
4
|
+
# Best-effort line numbers for SARIF annotations.
|
|
5
|
+
class LineLocator
|
|
6
|
+
INTERACTION = /^- request:|"request"\s*:/
|
|
7
|
+
|
|
8
|
+
def initialize(text)
|
|
9
|
+
@text = text.to_s
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def line_for(finding)
|
|
13
|
+
if finding.value
|
|
14
|
+
offset = [finding.value.to_s, Encodings.json(finding.value)].uniq.filter_map { |needle| @text.index(needle) }.min
|
|
15
|
+
return line_at(offset) if offset
|
|
16
|
+
end
|
|
17
|
+
return interaction_line(finding.interaction) if finding.interaction
|
|
18
|
+
|
|
19
|
+
1
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def line_at(offset)
|
|
25
|
+
@text[0, offset].count("\n") + 1
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def interaction_line(index)
|
|
29
|
+
offset = -1
|
|
30
|
+
(index + 1).times do
|
|
31
|
+
offset = @text.index(INTERACTION, offset + 1)
|
|
32
|
+
return 1 unless offset
|
|
33
|
+
end
|
|
34
|
+
line_at(offset)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|