redirect_safely 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b4a20e0d74c1fde24f68ea6e94cd81e004ab8592a4fbcc54c01e1ffcf2e6c19b
4
+ data.tar.gz: 32ad7b69b7ee644e469cd71e0dde434b20dca8582bbb7d0852106172b8a08219
5
+ SHA512:
6
+ metadata.gz: ea6447a88e462c0d1f952c2e12ebacedc9981e31115277ef866ab903120dfa93df67da57a27abcfd7530475cf501d7025267bb5f9ce66ddb3ed874c4e92ea9ac
7
+ data.tar.gz: 663634436f759243ca42b1fe197bb9d2afbbc1c817771227a7fe527d11aabcefc4cf08dda2174455a036efe0c671031264407f78eacce04e7fcfaf0200402942
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .byebug_history
File without changes
@@ -0,0 +1,46 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ ## Our Standards
8
+
9
+ Examples of behavior that contributes to creating a positive environment include:
10
+
11
+ * Using welcoming and inclusive language
12
+ * Being respectful of differing viewpoints and experiences
13
+ * Gracefully accepting constructive criticism
14
+ * Focusing on what is best for the community
15
+ * Showing empathy towards other community members
16
+
17
+ Examples of unacceptable behavior by participants include:
18
+
19
+ * The use of sexualized language or imagery and unwelcome sexual attention or advances
20
+ * Trolling, insulting/derogatory comments, and personal or political attacks
21
+ * Public or private harassment
22
+ * Publishing others' private information, such as a physical or electronic address, without explicit permission
23
+ * Other conduct which could reasonably be considered inappropriate in a professional setting
24
+
25
+ ## Our Responsibilities
26
+
27
+ Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28
+
29
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30
+
31
+ ## Scope
32
+
33
+ This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34
+
35
+ ## Enforcement
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at opensource@shopify.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38
+
39
+ Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40
+
41
+ ## Attribution
42
+
43
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44
+
45
+ [homepage]: http://contributor-covenant.org
46
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,22 @@
1
+ # How to Contribute
2
+ ## Things we will merge
3
+ - Bug fixes
4
+ - Performance improvements
5
+ - Features which are likely to be useful to the majority of users
6
+
7
+ ## Things we won't merge
8
+ - Code which adds no significant value to the library
9
+ - Code which comes without tests
10
+ - Code which breaks existing tests
11
+
12
+ ## Workflow
13
+ 1. Fork it ( https://github.com/shopify/buildkit/fork )
14
+ 2. Create your feature branch (git checkout -b my-new-feature)
15
+ 3. Commit your changes (git commit -am 'Add some feature')
16
+ 4. Push to the branch (git push origin my-new-feature)
17
+ 5. Create a new Pull Request
18
+
19
+ Please keep the following in mind:
20
+
21
+ Add a short entry to the "unreleased" section in [RedirectSafely](./RedirectSafely) describing your changes.
22
+ Do not change RedirectSafely::VERSION; this is done as part of the release process.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in redirect_safely.gemspec
4
+ gemspec
5
+
6
+ gem 'pry-byebug'
7
+ gem 'rubocop'
8
+ gem 'rake'
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ redirect_safely (1.0.0)
5
+ activemodel
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (3.2.22.5)
11
+ activesupport (= 3.2.22.5)
12
+ builder (~> 3.0.0)
13
+ activesupport (3.2.22.5)
14
+ i18n (~> 0.6, >= 0.6.4)
15
+ multi_json (~> 1.0)
16
+ ast (2.4.0)
17
+ builder (3.0.4)
18
+ byebug (11.1.1)
19
+ coderay (1.1.2)
20
+ concurrent-ruby (1.1.6)
21
+ i18n (0.9.5)
22
+ concurrent-ruby (~> 1.0)
23
+ jaro_winkler (1.5.4)
24
+ method_source (0.9.2)
25
+ multi_json (1.14.1)
26
+ parallel (1.19.1)
27
+ parser (2.7.1.1)
28
+ ast (~> 2.4.0)
29
+ power_assert (1.1.6)
30
+ pry (0.12.2)
31
+ coderay (~> 1.1.0)
32
+ method_source (~> 0.9.0)
33
+ pry-byebug (3.8.0)
34
+ byebug (~> 11.0)
35
+ pry (~> 0.10)
36
+ rainbow (3.0.0)
37
+ rake (13.0.1)
38
+ rexml (3.2.4)
39
+ rubocop (0.82.0)
40
+ jaro_winkler (~> 1.5.1)
41
+ parallel (~> 1.10)
42
+ parser (>= 2.7.0.1)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ rexml
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 2.0)
47
+ ruby-progressbar (1.10.1)
48
+ test-unit (3.3.5)
49
+ power_assert
50
+ unicode-display_width (1.7.0)
51
+
52
+ PLATFORMS
53
+ ruby
54
+
55
+ DEPENDENCIES
56
+ activesupport (~> 3.0)
57
+ bundler (~> 1.14)
58
+ pry-byebug
59
+ rake
60
+ redirect_safely!
61
+ rubocop
62
+ test-unit (~> 3.0)
63
+
64
+ BUNDLED WITH
65
+ 1.17.3
@@ -0,0 +1,7 @@
1
+ Copyright 2020-present, Shopify Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ # RedirectSafely
2
+
3
+ Sanitize `return_to`-style URLs, including some edge cases that you probably missed.
4
+
5
+ `RedirectSafely` is used in production and extracted from Shopify.
6
+
7
+ ## Installation
8
+
9
+ Add these lines to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'redirect_safely', '~> 1.0'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ ## Usage
20
+
21
+ - `RedirectSafely.safe?(url, options)`
22
+
23
+ Return true if the URL is considered "safe", false otherwise.
24
+
25
+ ### Parameters:
26
+ - `url` String (required) - The URL to test
27
+
28
+ ### Options:
29
+ - `path_match` Regexp (optional) - Match the path portion of the URL against a regexp
30
+ - `require_absolute` Boolean (optional) - If true, require an absolute URL (domain must be included in `whitelist`)
31
+ - `require_ssl` Boolean (optional) - If true, and an absolute URL is provided, require a URL starting with `https://`
32
+ - `whitelist` String[] (optional) - Whitelisted domains for checking absolute URLs
33
+ - `subdomains` String[] (optional) - Whitelisted subdomains for checking absolute URLs. Must start with a leading `.`.
34
+
35
+ - `RedirectSafely.make_safe(url, default, options)`
36
+
37
+ Return `url` if it's safe, otherwise return `default`.
38
+
39
+ Shares options with `safe?`, and is roughly equivalent to:
40
+
41
+ ```ruby
42
+ safe_url = RedirectSafely.safe?(url) ? url : default
43
+ ```
44
+
45
+ - `RedirectSafelyValidator`
46
+
47
+ If you persist a redirect URL on a model, you can validate that it is `safe?`:
48
+
49
+ ```ruby
50
+ class Request
51
+ validates :return_to, redirect_safely: true
52
+ end
53
+ ```
54
+
55
+ You can pass any options supported by `safe?` (but *not* those added by `make_safe`). In the event that you need more
56
+ control over the options (ie, dynamically producting a whitelist based on other model attributes), write a custom
57
+ `validate` method:
58
+
59
+ ```ruby
60
+ class Request
61
+ validates :store_url, presence: true
62
+ validate :return_to, presence: true
63
+
64
+ validate :return_to_is_safe
65
+
66
+ private
67
+
68
+ def return_to_is_safe
69
+ errors.add(:return_to, :invalid) unless RedirectSafely.safe?(return_to, whitelist: URI.parse(store_uri).host)
70
+ end
71
+ end
72
+ ```
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redirect_safely"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+ require "version"
3
+ require "redirect_safely_validator"
4
+
5
+ module RedirectSafely
6
+ extend self
7
+
8
+ def make_safe(uri, default, options = {})
9
+ if uri.present? && safe?(uri, options)
10
+ uri
11
+ else
12
+ default
13
+ end
14
+ end
15
+
16
+ def safe?(uri_string, options = {})
17
+ return false if uri_string =~ %r{///}
18
+
19
+ uri = URI.parse(uri_string.to_s)
20
+
21
+ if uri.path
22
+ return false unless uri.path.start_with?('/')
23
+ return false if uri.path =~ %r{[/\\][/\\]}
24
+ end
25
+ return false unless uri.scheme.nil? || ['http', 'https'].include?(uri.scheme)
26
+ return false unless uri.userinfo.nil?
27
+ return false if options[:path_match] &&
28
+ (uri.path !~ options[:path_match] || File.absolute_path(uri.path) !~ options[:path_match])
29
+ return false if options[:require_absolute] && uri.host.nil?
30
+ return false if options[:require_ssl] && uri.scheme && uri.scheme != 'https'
31
+ return false unless valid_host?(uri.host, options[:whitelist], options[:subdomains])
32
+
33
+ true
34
+ rescue URI::InvalidURIError
35
+ false
36
+ end
37
+
38
+ private
39
+
40
+ def valid_host?(host, whitelist, subdomains)
41
+ raise ArgumentError, "subdomains must start with ." if subdomains && !subdomains.all? { |s| s.start_with?('.') }
42
+
43
+ return true if host.nil?
44
+ return true if whitelist&.include?(host)
45
+ return true if subdomains && host.end_with?(*subdomains)
46
+
47
+ false
48
+ end
49
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ require 'active_model/validations'
3
+
4
+ class RedirectSafelyValidator < ::ActiveModel::EachValidator
5
+ def validate_each(record, attribute, value)
6
+ record.errors.add(attribute, options[:message] || :invalid) unless RedirectSafely.safe?(value, options)
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module RedirectSafely
3
+ VERSION = "1.0.0"
4
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ #
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require './lib/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'redirect_safely'
10
+ spec.version = RedirectSafely::VERSION
11
+ spec.authors = ['Shopify']
12
+ spec.email = ['gems@shopify.com']
13
+
14
+ spec.summary = %q{Sanitize redirect_to URLs}
15
+ spec.description = %q{Sanitize redirect_to URLs}
16
+ spec.homepage = 'https://github.com/shopify/redirect_safely'
17
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
18
+
19
+ spec.metadata["source_code_uri"] = "https://github.com/shopify/redirect_safely"
20
+ spec.metadata["changelog_uri"] = "https://github.com/shopify/redirect_safely/blob/master/CHANGELOG.md"
21
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
22
+
23
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
24
+ f.match(%r{^(test|spec|features)/})
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'activemodel'
31
+
32
+ spec.add_development_dependency 'activesupport', '~>3.0'
33
+ spec.add_development_dependency 'test-unit', '~>3.0'
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.14'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redirect_safely
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Shopify
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description: Sanitize redirect_to URLs
84
+ email:
85
+ - gems@shopify.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - CHANGELOG.md
92
+ - CODE_OF_CONDUCT.md
93
+ - CONTRIBUTION.md
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.md
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/redirect_safely.rb
102
+ - lib/redirect_safely_validator.rb
103
+ - lib/version.rb
104
+ - redirect_safely.gemspec
105
+ homepage: https://github.com/shopify/redirect_safely
106
+ licenses: []
107
+ metadata:
108
+ source_code_uri: https://github.com/shopify/redirect_safely
109
+ changelog_uri: https://github.com/shopify/redirect_safely/blob/master/CHANGELOG.md
110
+ allowed_push_host: https://rubygems.org
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: 2.3.0
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.0.3
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Sanitize redirect_to URLs
130
+ test_files: []