summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/hotlinking_detector_spec.rb
blob: 536d744c197206064a306080713eecfa58d06352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Gitlab::HotlinkingDetector do
  describe ".intercept_hotlinking?" do
    using RSpec::Parameterized::TableSyntax

    subject { described_class.intercept_hotlinking?(request) }

    let(:request) { double("request", headers: headers) }
    let(:headers) { {} }

    context "hotlinked as media" do
      where(:return_value, :accept_header) do
        # These are default formats in modern browsers, and IE
        false | "*/*"
        false | "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        false | "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
        false | "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        false | "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
        false | "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/msword, */*"
        false | "text/html, application/xhtml+xml, image/jxr, */*"
        false | "text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1"

        # These are image request formats
        true | "image/webp,*/*"
        true | "image/png,image/*;q=0.8,*/*;q=0.5"
        true | "image/webp,image/apng,image/*,*/*;q=0.8"
        true | "image/png,image/svg+xml,image/*;q=0.8, */*;q=0.5"

        # Video request formats
        true | "video/webm,video/ogg,video/*;q=0.9,application/ogg;q=0.7,audio/*;q=0.6,*/*;q=0.5"

        # Audio request formats
        true | "audio/webm,audio/ogg,audio/wav,audio/*;q=0.9,application/ogg;q=0.7,video/*;q=0.6,*/*;q=0.5"

        # CSS request formats
        true | "text/css,*/*;q=0.1"
        true | "text/css"
        true | "text/css,*/*;q=0.1"
      end

      with_them do
        let(:headers) do
          { "Accept" => accept_header }
        end

        it { is_expected.to be(return_value) }
      end
    end

    context "hotlinked as a script" do
      where(:return_value, :fetch_mode) do
        # Standard navigation fetch modes
        false | "navigate"
        false | "nested-navigate"
        false | "same-origin"

        # Fetch modes when linking as JS
        true | "cors"
        true | "no-cors"
        true | "websocket"
      end

      with_them do
        let(:headers) do
          { "Sec-Fetch-Mode" => fetch_mode }
        end

        it { is_expected.to be(return_value) }
      end
    end
  end
end