private_address_check 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of private_address_check might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +10 -0
- data/lib/private_address_check.rb +38 -0
- data/lib/private_address_check/tcpsocket_ext.rb +29 -0
- data/lib/private_address_check/version.rb +3 -0
- data/test/private_address_check_test.rb +38 -0
- data/test/tcpsocket_ext_test.rb +22 -0
- data/test/test_helper.rb +4 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 69dffe5d95058be5d6d131dd7e27843b02e736ac
|
4
|
+
data.tar.gz: 12043ad0e982f7bfc4e95050151b69a8fb3575f9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 230e58a7c57213f83ee7975b05329aaa17b0a5c2f99d55325ff50fc1b9d76faeb616e4ce4ce24aa4b722bf819743e2ec75b36d2c113a91e1d89a929d63d58192
|
7
|
+
data.tar.gz: f93358263bdd8fd37a62495a33402dc149a51d67c0a00928f54f1aaf0073e748654a36ba37f8b3b5f49ba1c04ee5bf7b233a35e633ff37a1bde9e834040e1935
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at jdowney@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 John Downey
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# PrivateAddressCheck
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/jtdowney/private_address_check.svg?branch=master)](https://travis-ci.org/jtdowney/private_address_check)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/jtdowney/private_address_check/badges/gpa.svg)](https://codeclimate.com/github/jtdowney/private_address_check)
|
5
|
+
|
6
|
+
Checks if a URL or hostname would cause a request to a private network (RFC 1918). This is useful in preventing attacks like [Server Side Request Forgery](https://cwe.mitre.org/data/definitions/918.html).
|
7
|
+
|
8
|
+
## Requirements
|
9
|
+
|
10
|
+
* Ruby >= 2.0
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'private_address_check'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install private_address_check
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require "private_address_check"
|
32
|
+
|
33
|
+
PrivateAddressCheck.private_address?("8.8.8.8") # => false
|
34
|
+
PrivateAddressCheck.private_address?("10.10.10.2") # => true
|
35
|
+
PrivateAddressCheck.private_address?("127.0.0.1") # => true
|
36
|
+
PrivateAddressCheck.private_address?("172.16.2.10") # => true
|
37
|
+
PrivateAddressCheck.private_address?("192.168.1.10") # => true
|
38
|
+
PrivateAddressCheck.private_address?("fd00::2") # => true
|
39
|
+
PrivateAddressCheck.resolves_to_private_address?("github.com") # => false
|
40
|
+
PrivateAddressCheck.resolves_to_private_address?("localhost") # => true
|
41
|
+
|
42
|
+
require "private_address_check/tcpsocket_ext"
|
43
|
+
require "net/http"
|
44
|
+
require "require "uri""
|
45
|
+
|
46
|
+
Net::HTTP.get_response(URI.parse("http://192.168.1.1")) # => attempts connection like normal
|
47
|
+
|
48
|
+
PrivateAddressCheck.only_public_connections do
|
49
|
+
Net::HTTP.get_response(URI.parse("http://192.168.1.1"))
|
50
|
+
end
|
51
|
+
# => raises PrivateAddressCheck::PrivateConnectionAttemptedError
|
52
|
+
```
|
53
|
+
|
54
|
+
## Development
|
55
|
+
|
56
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
57
|
+
|
58
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jtdowney/private_address_check. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
63
|
+
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
68
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "ipaddr"
|
2
|
+
require "resolv"
|
3
|
+
|
4
|
+
require "private_address_check/version"
|
5
|
+
|
6
|
+
module PrivateAddressCheck
|
7
|
+
module_function
|
8
|
+
|
9
|
+
CIDR_LIST = [
|
10
|
+
# Loopback
|
11
|
+
IPAddr.new("127.0.0.0/8"),
|
12
|
+
IPAddr.new("::1/64"),
|
13
|
+
|
14
|
+
# Link Local,
|
15
|
+
IPAddr.new("169.254.0.0/16"),
|
16
|
+
|
17
|
+
# RFC 1918
|
18
|
+
IPAddr.new("10.0.0.0/8"),
|
19
|
+
IPAddr.new("172.16.0.0/12"),
|
20
|
+
IPAddr.new("192.168.0.0/16"),
|
21
|
+
|
22
|
+
# RFC 4193
|
23
|
+
IPAddr.new("fc00::/7"),
|
24
|
+
]
|
25
|
+
|
26
|
+
def private_address?(address)
|
27
|
+
CIDR_LIST.any? do |cidr|
|
28
|
+
cidr.include?(address)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def resolves_to_private_address?(hostname)
|
33
|
+
ips = Resolv.getaddresses(hostname)
|
34
|
+
ips.any? do |ip|
|
35
|
+
private_address?(ip)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module PrivateAddressCheck
|
2
|
+
PrivateConnectionAttemptedError = Class.new(StandardError)
|
3
|
+
|
4
|
+
module_function
|
5
|
+
|
6
|
+
def only_public_connections
|
7
|
+
Thread.current[:private_address_check] = true
|
8
|
+
yield
|
9
|
+
ensure
|
10
|
+
Thread.current[:private_address_check] = false
|
11
|
+
end
|
12
|
+
|
13
|
+
module TCPSocketExt
|
14
|
+
def initialize(remote_host, remote_port, local_host = nil, local_port = nil)
|
15
|
+
if Thread.current[:private_address_check]
|
16
|
+
ip = Resolv.getaddress(remote_host)
|
17
|
+
if PrivateAddressCheck.private_address?(ip)
|
18
|
+
raise PrivateAddressCheck::PrivateConnectionAttemptedError
|
19
|
+
end
|
20
|
+
|
21
|
+
super(ip, remote_port, local_host, local_port)
|
22
|
+
else
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
TCPSocket.send(:prepend, PrivateAddressCheck::TCPSocketExt)
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PrivateAddressCheckTest < Minitest::Test
|
4
|
+
def test_private_address_for_public_addresses
|
5
|
+
refute PrivateAddressCheck.private_address?("192.30.253.113")
|
6
|
+
refute PrivateAddressCheck.private_address?("8.8.8.8")
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_private_address_for_rfc1918_addresses
|
10
|
+
assert PrivateAddressCheck.private_address?("10.10.10.2")
|
11
|
+
assert PrivateAddressCheck.private_address?("172.16.2.10")
|
12
|
+
assert PrivateAddressCheck.private_address?("192.168.1.10")
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_private_address_for_rfc4193_addresses
|
16
|
+
assert PrivateAddressCheck.private_address?("fc00::a")
|
17
|
+
assert PrivateAddressCheck.private_address?("fd00::2")
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_private_address_for_loopback_addresses
|
21
|
+
assert PrivateAddressCheck.private_address?("127.0.0.1")
|
22
|
+
assert PrivateAddressCheck.private_address?("127.2.2.2")
|
23
|
+
assert PrivateAddressCheck.private_address?("::1")
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_private_address_for_link_local_addresses
|
27
|
+
assert PrivateAddressCheck.private_address?("169.254.2.5")
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_private_hostname_for_public_addresses
|
31
|
+
refute PrivateAddressCheck.resolves_to_private_address?("github.com")
|
32
|
+
refute PrivateAddressCheck.resolves_to_private_address?("example.com")
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_private_hostname_for_private_addresses
|
36
|
+
assert PrivateAddressCheck.resolves_to_private_address?("localhost")
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'private_address_check/tcpsocket_ext'
|
3
|
+
|
4
|
+
class TCPSocketExtTest < Minitest::Test
|
5
|
+
def test_private_address
|
6
|
+
assert_raises PrivateAddressCheck::PrivateConnectionAttemptedError do
|
7
|
+
PrivateAddressCheck.only_public_connections do
|
8
|
+
TCPSocket.new("localhost", 80)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_public_address
|
14
|
+
connected = false
|
15
|
+
PrivateAddressCheck.only_public_connections do
|
16
|
+
TCPSocket.new("example.com", 80)
|
17
|
+
connected = true
|
18
|
+
end
|
19
|
+
|
20
|
+
assert connected
|
21
|
+
end
|
22
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: private_address_check
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Downey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Checks if a IP or hostname would cause a request to a private network
|
56
|
+
(RFC 1918)
|
57
|
+
email:
|
58
|
+
- jdowney@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- CODE_OF_CONDUCT.md
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/private_address_check.rb
|
69
|
+
- lib/private_address_check/tcpsocket_ext.rb
|
70
|
+
- lib/private_address_check/version.rb
|
71
|
+
- test/private_address_check_test.rb
|
72
|
+
- test/tcpsocket_ext_test.rb
|
73
|
+
- test/test_helper.rb
|
74
|
+
homepage: https://github.com/jtdowney/private_address_check
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 2.0.0
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.5.1
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Prevent Server Side Request Forgery attacks by checking the destination
|
98
|
+
test_files: []
|