File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
require "uri"
4
+ require "ipaddr"
5
+ require "resolv"
4
6
5
7
module Sentry
6
8
class DSN
7
9
PORT_MAP = { "http" => 80 , "https" => 443 } . freeze
8
10
REQUIRED_ATTRIBUTES = %w[ host path public_key project_id ] . freeze
11
+ LOCALHOST_NAMES = %w[ localhost 127.0.0.1 ::1 [::1] ] . freeze
12
+ LOCALHOST_PATTERN = /\. local(host|domain)?$/i
9
13
10
14
attr_reader :scheme , :secret_key , :port , *REQUIRED_ATTRIBUTES
11
15
@@ -49,5 +53,33 @@ def csp_report_uri
49
53
def envelope_endpoint
50
54
"#{ path } /api/#{ project_id } /envelope/"
51
55
end
56
+
57
+ def local?
58
+ @local ||= ( localhost? || private_ip? || resolved_ips_private? )
59
+ end
60
+
61
+ def localhost?
62
+ LOCALHOST_NAMES . include? ( host . downcase ) || LOCALHOST_PATTERN . match? ( host )
63
+ end
64
+
65
+ def private_ip?
66
+ @private_ip ||= begin
67
+ begin
68
+ IPAddr . new ( host ) . private?
69
+ rescue IPAddr ::InvalidAddressError
70
+ false
71
+ end
72
+ end
73
+ end
74
+
75
+ def resolved_ips_private?
76
+ @resolved_ips_private ||= begin
77
+ begin
78
+ Resolv . getaddresses ( host ) . any? { |ip | IPAddr . new ( ip ) . private? }
79
+ rescue Resolv ::ResolvError , IPAddr ::InvalidAddressError
80
+ false
81
+ end
82
+ end
83
+ end
52
84
end
53
85
end
Original file line number Diff line number Diff line change 37
37
expect ( subject . csp_report_uri ) . to eq ( "http://sentry.localdomain:3000/api/42/security/?sentry_key=12345" )
38
38
end
39
39
end
40
+
41
+ describe "#local?" do
42
+ it "returns true for localhost" do
43
+ expect ( described_class . new ( "http://12345:67890@localhost/sentry/42" ) . local? ) . to eq ( true )
44
+ end
45
+
46
+ it "returns true for 127.0.0.1" do
47
+ expect ( described_class . new ( "http://12345:67890@127.0.0.1/sentry/42" ) . local? ) . to eq ( true )
48
+ end
49
+ it "returns true for ::1" do
50
+ expect ( described_class . new ( "http://12345:67890@[::1]/sentry/42" ) . local? ) . to eq ( true )
51
+ end
52
+
53
+ it "returns true for private IP" do
54
+ expect ( described_class . new ( "http://12345:67890@192.168.0.1/sentry/42" ) . local? ) . to eq ( true )
55
+ end
56
+
57
+ it "returns true for private IP with port" do
58
+ expect ( described_class . new ( "http://12345:67890@192.168.0.1:3000/sentry/42" ) . local? ) . to eq ( true )
59
+ end
60
+
61
+ it "returns false for non-local domain" do
62
+ expect ( described_class . new ( "http://12345:67890@sentry.io/sentry/42" ) . local? ) . to eq ( false )
63
+ end
64
+ end
40
65
end
You can’t perform that action at this time.
0 commit comments