Module: Arachni::Element::Capabilities::Auditable::LineBuffered

Included in:
Arachni::Element::Cookie, Form, Header, JSON, Link, LinkTemplate, NestedCookie, XML
Defined in:
lib/arachni/element/capabilities/auditable/line_buffered.rb

Overview

Author:

Constant Summary collapse

DEFAULT_LINE_BUFFER_SIZE =
1_000

Instance Method Summary collapse

Instance Method Details

#line_buffered_audit(payloads, options = {}, &block) ⇒ Object



18
19
20
21
22
23
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
57
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
# File 'lib/arachni/element/capabilities/auditable/line_buffered.rb', line 18

def line_buffered_audit( payloads, options = {}, &block )
    fail ArgumentError, 'Missing block.' if !block_given?

    options     = options.dup
    buffer_size = options[:buffer_size] || DEFAULT_LINE_BUFFER_SIZE

    print_debug_level_2 "About to audit #{buffer_size} lines at a time: #{audit_id}"

    buffers = {}

    options[:submit] ||= {}
    options[:submit][:on_body_lines] = proc do |lines, response|
        # In case of redirection or runtime scope changes.
        if !response.parsed_url.seed_in_host? && response.scope.out?
            print_debug_level_3 "Response out of scope for #{audit_id}: #{response.url}"
            print_debug_level_3 'Aborting...'
            next :abort
        end

        print_debug_level_3 "Got lines for: #{audit_id}"
        print_debug_level_4 lines

        request = response.request

        buffers[request.id] ||= {
            data:    '',
            counter: 0
        }
        buffer = buffers[request.id]

        buffer[:data]    << lines
        buffer[:counter] += lines.count( "\n" )

        print_debug_level_3 "Buffer is at: #{buffer[:counter]}/#{buffer_size}"
        next if buffer[:counter] < buffer_size

        print_debug_level_3 'Buffer full, setting response body.'
        print_debug_level_4 buffer[:data]

        response.body = buffer[:data]

        print_debug_level_3 "Calling: #{block}"

        # `false` means we're still buffering.
        r = block.call( response, request.performer, false )

        print_debug_level_3 "Block returned: #{r}"
        print_debug_level_3 'Emptying buffer.'

        # Create a new object, we don't want to mess with reference issues.
        buffer[:data]    = ''
        buffer[:counter] = 0

        r
    end

    audit( payloads, options ) do |response|
        print_debug_level_3 "Line buffering completed for: #{audit_id}"

        request = response.request
        buffer  = buffers[request.id]

        # The response body can include remnants from the HTTP line buffer
        # and our own buffer could have lines that didn't exceed the flush
        # threshold, hence we combine them
        if buffer && !buffer[:data].empty?
            b = response.body
            response.body = buffer[:data]
            response.body << b
        end

        print_debug_level_3 "Calling: #{block}"

        # `true` means we've read the entire response.
        block.call response, request.performer, true

        print_debug_level_3 'Deleted buffer.'
        buffers.delete( request.id )
    end
end