|
2 | 2 |
|
3 | 3 | require 'spec_helper' |
4 | 4 |
|
5 | | -def request_function(style, address) |
6 | | - case style |
7 | | - when 'ZAF' |
8 | | - "function request() { client.request('#{address}') }" |
9 | | - when 'jQuery' |
10 | | - "function request() { jQuery.get('#{address}') }" |
11 | | - when 'jQuery$' |
12 | | - "function request() { $.ajax('#{address}') }" |
13 | | - when 'XMLHttpRequest' |
14 | | - "function request() { xhr.open('get', '#{address}') }" |
15 | | - when 'fetch' |
16 | | - "function request() { fetch('#{address}') }" |
17 | | - end |
18 | | -end |
19 | | - |
20 | | -request_function_styles = %w[ZAF jQuery jQuery$ XMLHttpRequest fetch] |
| 5 | +describe ZendeskAppsSupport::Validations::Requests do |
| 6 | + let(:package) { double('Package', warnings: [], html_files: []) } |
| 7 | + let(:app_file) { double('AppFile', relative_path: 'app_file.js') } |
21 | 8 |
|
22 | | -shared_examples 'an insecure request' do |file_path, function_style| |
23 | | - address = 'http://foo.com' |
24 | | - let(:markup) { request_function(function_style, address) } |
| 9 | + before { allow(package).to receive(:js_files) { [app_file] } } |
25 | 10 |
|
26 | | - it "and raise a warning inside #{function_style} style requests" do |
27 | | - errors = subject.call(package) |
28 | | - expect(package.warnings[0]).to include('insecure HTTP request', address, file_path) |
29 | | - expect(errors).to be_empty |
30 | | - end |
31 | | -end |
| 11 | + context 'http protocols check' do |
| 12 | + it 'returns no warnings for files that contain https urls' do |
| 13 | + allow(app_file).to receive(:read) { "client.instance(\"https://foo-bar.com\");\r\n\t" } |
32 | 14 |
|
33 | | -blocked_ips = { |
34 | | - private: { |
35 | | - range: '10.0.0.0/8, 172.16.0.0/12, or 192.168.0.0/16', |
36 | | - example: '192.168.0.1' |
37 | | - }, |
38 | | - loopback: { |
39 | | - range: '127.0.0.0/8', |
40 | | - example: '127.0.0.1' |
41 | | - }, |
42 | | - link_local: { |
43 | | - range: '169.254.0.0/16', |
44 | | - example: '169.254.0.1' |
45 | | - } |
46 | | -} |
| 15 | + subject.call(package) |
| 16 | + expect(package.warnings).to be_empty |
| 17 | + end |
47 | 18 |
|
48 | | -shared_examples 'a blocked ip' do |file_path, function_style, ip_type, ip| |
49 | | - let(:markup) { request_function(function_style, "https://#{ip}") } |
| 19 | + it 'returns warning with request information when files contain http url' do |
| 20 | + allow(app_file).to receive(:read) { "client.instance(\"http://foo-bar.com\");\r\n\t" } |
50 | 21 |
|
51 | | - it "and throw a #{ip_type} ip error inside #{function_style} style request calls" do |
52 | | - errors = subject.call(package) |
53 | | - expect(package.warnings).to be_empty |
54 | | - expect(errors[0]).to include("request to a #{ip_type} ip", ip, file_path) |
| 22 | + subject.call(package) |
| 23 | + expect(package.warnings[0]).to include( |
| 24 | + 'Possible insecure HTTP request', |
| 25 | + 'foo-bar.com', |
| 26 | + 'in app_file.js', |
| 27 | + 'Consider using the HTTPS protocol instead.' |
| 28 | + ) |
| 29 | + end |
55 | 30 | end |
56 | | -end |
57 | 31 |
|
58 | | -describe ZendeskAppsSupport::Validations::Requests do |
59 | | - app_js_path = 'assets/app.js' |
60 | | - let(:app_js) { double('AppFile', relative_path: app_js_path, read: markup) } |
61 | | - let(:subject) { ZendeskAppsSupport::Validations::Requests } |
62 | | - let(:package) { double('Package', js_files: [app_js], html_files: [], warnings: []) } |
| 32 | + context 'IPs check' do |
| 33 | + it 'returns no validation error when scanning regular IP' do |
| 34 | + allow(app_file).to receive(:read) { "client.instance(\"64.233.191.255\");\r\n\t" } |
| 35 | + expect(subject.call(package).flatten).to be_empty |
| 36 | + end |
63 | 37 |
|
64 | | - describe 'using the http protocol' do |
65 | | - request_function_styles.each { |function_style| it_behaves_like 'an insecure request', app_js_path, function_style } |
66 | | - end |
| 38 | + it 'returns a validation error when scanning private IP' do |
| 39 | + allow(app_file).to receive(:read) { "//var x = '192.168.0.1'\r\n \tclient.get(x)" } |
| 40 | + expect(subject.call(package).flatten[0]).to include('request to a private ip 192.168.0.1') |
| 41 | + end |
| 42 | + |
| 43 | + it 'returns a validation error when scanning loopback IP' do |
| 44 | + allow(app_file).to receive(:read) { "//var x = '127.0.0.1'\r\n \tclient.get(x)" } |
| 45 | + expect(subject.call(package).flatten[0]).to include('request to a loopback ip 127.0.0.1') |
| 46 | + end |
67 | 47 |
|
68 | | - blocked_ips.each do |type, ip| |
69 | | - describe "to #{ip[:range]} range ips" do |
70 | | - request_function_styles.each do |function_style| |
71 | | - type_localised = ZendeskAppsSupport::I18n.t("txt.apps.admin.error.app_build.blocked_request_#{type}") |
72 | | - it_behaves_like 'a blocked ip', app_js_path, function_style, type_localised, ip[:example] |
73 | | - end |
| 48 | + it 'returns a validation error when scanning link_local IP' do |
| 49 | + allow(app_file).to receive(:read) { "//var x = '169.254.0.1'\r\n \tclient.get(x)" } |
| 50 | + expect(subject.call(package).flatten[0]).to include('request to a link-local ip 169.254.0.1') |
74 | 51 | end |
75 | 52 | end |
76 | 53 | end |
0 commit comments