Module: SastBox::Snippet

Included in:
Scanner
Defined in:
lib/sastbox-sdk/snippet.rb

Instance Method Summary collapse

Instance Method Details

#filename_relative(filename) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/sastbox-sdk/snippet.rb', line 6

def filename_relative(filename)
  filename_path = File.expand_path(filename)
  codebase_path = File.expand_path(@opts.codebase)

  if filename_path.start_with?(codebase_path)
    filename_path.sub!(codebase_path, '')
    filename_path = filename_path[1..-1] if filename_path.start_with?('/')
    return filename_path
  else
    return nil
  end
end

#snippet_calculate_hashes(snippet) ⇒ Object



19
20
21
22
# File 'lib/sastbox-sdk/snippet.rb', line 19

def snippet_calculate_hashes(snippet)
  snippet[:evidence_line][:hash] = Digest::SHA256.hexdigest(snippet[:evidence_line][:content])
  snippet[:evidence_full][:hash] = Digest::SHA256.hexdigest(snippet[:evidence_full][:content])
end

#snippet_read(filename, line, context = 5) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sastbox-sdk/snippet.rb', line 24

def snippet_read(filename, line, context=5)
  snippet = {
    evidence_line:    { content: '', start_line: 0, end_line: 0, hash: '' },
    evidence_full:    { content: '', start_line: 0, end_line: 0, hash: '' },
    read_success: false
  }

  if File.file?(filename)
    snippet[:read_success] = true
    lines = File.open(filename).readlines
    begin_code = [1, line.to_i - context].max
    end_code = [line.to_i + context, lines.length].min

    if end_code > lines.length or line.to_i > lines.length
      snippet[:read_success] = false
      return snippet
    end

    snippet[:evidence_line][:start_line] = line.to_i
    snippet[:evidence_line][:end_line]   = line.to_i
    snippet[:evidence_line][:content] = lines[line.to_i - 1].chomp.force_encoding('ISO-8859-1').encode('UTF-8')

    snippet[:evidence_full][:start_line] = begin_code
    snippet[:evidence_full][:end_line]   = end_code

    begin_code.upto(end_code) do |pos|
      snippet[:evidence_full][:content] << lines[pos - 1].force_encoding('ISO-8859-1').encode('UTF-8')
    end
    snippet_calculate_hashes(snippet)
  end

  snippet
end

#snippet_read_range(filename, start_line, end_line, context = 5) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sastbox-sdk/snippet.rb', line 58

def snippet_read_range(filename, start_line, end_line, context=5)
  snippet = {
    evidence_line:    { content: '', start_line: 0, end_line: 0, hash: '' },
    evidence_full:    { content: '', start_line: 0, end_line: 0, hash: '' },
    read_success: false
  }

  if File.file?(filename)
    snippet[:read_success] = true
    lines = File.open(filename).readlines
    num_lines = lines.length

    if !start_line.between?(1, num_lines) || !end_line.between?(1, num_lines) || start_line > end_line
      snippet[:read_success] = false
      return snippet
    end

    begin_code = [1, start_line.to_i - context].max
    end_code = [end_line.to_i + context, num_lines].min

    if end_code > num_lines
      snippet[:read_success] = false
      return snippet
    end

    snippet[:evidence_line][:start_line] = start_line.to_i
    snippet[:evidence_line][:end_line]   = end_line.to_i

    start_line.upto(end_line) do |pos|
      snippet[:evidence_line][:content] << lines[pos - 1].force_encoding('ISO-8859-1').encode('UTF-8')
    end

    snippet[:evidence_full][:start_line] = begin_code
    snippet[:evidence_full][:end_line]   = end_code

    begin_code.upto(end_code) do |pos|
      snippet[:evidence_full][:content] << lines[pos - 1].force_encoding('ISO-8859-1').encode('UTF-8')
    end

    snippet_calculate_hashes(snippet)
  end

  snippet
end