rack-relations 0.1.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: aa6e3b9e72d7ae0b3c446cedcf560afaad79243c05b5f1217ae33dd6754e9bce
4
+ data.tar.gz: 3024e1fcc29027e639920e2945f3c13516f6d40d92eaa338e0dfe51ab2aa0666
5
+ SHA512:
6
+ metadata.gz: ad9a5f3ace43e54ca9e1e4e258038b64109fcdb9b77630184a3bf4c97d84782aa12a171327df8728c433a3a18759fe4bfa1671344ca63db50e093e530660c500
7
+ data.tar.gz: a246162059cbe66680b630986d8a4c45743ce1c99cf3e3d9228e4bcff838d3612022b22eabd17dcc398545088da1ee3ee0879bca1f05c6accef5098c60679517
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,15 @@
1
+ version: v1.0
2
+ name: Initial Pipeline
3
+ agent:
4
+ machine:
5
+ type: e1-standard-2
6
+ os_image: ubuntu1804
7
+ blocks:
8
+ - name: 'Block #1'
9
+ task:
10
+ jobs:
11
+ - name: 'Run tests'
12
+ commands:
13
+ - checkout
14
+ - bundle
15
+ - rake test
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,5 @@
1
+ v0.1.0
2
+ ======
3
+
4
+ * Initial release
5
+ * Rack middleware to add `rel="nofollow noopener noreferrer"` to any link not safelisted.
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at 34053+pbyrne@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rack-relations.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-relations (0.1.0)
5
+ nokogiri (~> 1.10)
6
+ rack (>= 2.0, < 3)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ coderay (1.1.3)
12
+ method_source (1.0.0)
13
+ mini_portile2 (2.4.0)
14
+ minitest (5.14.1)
15
+ mocha (1.11.2)
16
+ nokogiri (1.10.9)
17
+ mini_portile2 (~> 2.4.0)
18
+ pry (0.13.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rack (2.2.3)
22
+ rack-test (1.1.0)
23
+ rack (>= 1.0, < 3)
24
+ rake (12.3.3)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ minitest (~> 5.0)
31
+ mocha
32
+ pry
33
+ rack-relations!
34
+ rack-test
35
+ rake (~> 12.0)
36
+
37
+ BUNDLED WITH
38
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Patrick Byrne
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.
@@ -0,0 +1,57 @@
1
+ # Rack::Relations
2
+
3
+ Dynamically add `rel="nofollow noopener noreferrer"` to links on your page, so you don't have to remember to do it yourself. This is useful if you include links in your app to user-submitted URLs, to which you don't want to confer SEO benefits nor expose your users to potential phishing via `window.opener`.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rack-relations'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rack-relations
20
+
21
+ ## Usage
22
+
23
+ Add to your Rack app like so:
24
+
25
+ ```ruby
26
+ # Rack
27
+ use Rack::Relations::Middleware
28
+
29
+ # Rails, config/application.rb
30
+ config.middleware.use Rack::Relations::Middleware
31
+ ```
32
+
33
+ Optionally pass it domains to safelist and not apply this attribute, as an array of strings (for exact domain matches) or regular expressions (for more fine-grained specificity):
34
+
35
+ ```ruby
36
+ config.middleware.use Rack::Relations::Middleware,
37
+ safelist_domains: ["example.com", /\.org\Z/]
38
+ ```
39
+
40
+ ## Development
41
+
42
+ 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.
43
+
44
+ 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).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rack-relations. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rack-relations/blob/master/CODE_OF_CONDUCT.md).
49
+
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
54
+
55
+ ## Code of Conduct
56
+
57
+ Everyone interacting in the Rack::Relations project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rack-relations/blob/master/CODE_OF_CONDUCT.md).
@@ -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 "rack/relations"
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,12 @@
1
+ require "rack"
2
+ require "rack/relations/version"
3
+
4
+ require "rack/relations/anchor"
5
+ require "rack/relations/middleware"
6
+ require "rack/relations/processor"
7
+
8
+ module Rack
9
+ module Relations
10
+ Error = Class.new(StandardError)
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ require "uri"
2
+
3
+ module Rack
4
+ module Relations
5
+ class Anchor
6
+ attr_accessor :node, :safelist_domains, :uri
7
+
8
+ def initialize(node, safelist_domains:)
9
+ @node = node
10
+ @safelist_domains = safelist_domains
11
+ @uri = URI(node.attr("href")) rescue nil
12
+ end
13
+
14
+ def modified_rel
15
+ "#{node.attr("rel")} nofollow noopener noreferrer".strip
16
+ end
17
+
18
+ def safe?
19
+ return true if uri.relative?
20
+
21
+ safelist_domains.any? { |matcher| match?(matcher: matcher, host: uri.host) }
22
+ end
23
+
24
+ private def match?(matcher:, host:)
25
+ case matcher
26
+ when String
27
+ host.end_with?(matcher)
28
+ when Regexp
29
+ host =~ matcher
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ module Rack
2
+ module Relations
3
+ class Middleware
4
+ attr_accessor :app, :processor
5
+
6
+ def initialize(app, safelist_domains: [])
7
+ @app = app
8
+ @processor = Rack::Relations::Processor.new(safelist_domains: safelist_domains)
9
+ end
10
+
11
+ def call(env)
12
+ status, headers, response = @app.call(env)
13
+
14
+ response = @processor.perform(response)
15
+
16
+ [status, headers, response]
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+ require "nokogiri"
2
+
3
+ module Rack
4
+ module Relations
5
+ class Processor
6
+ # TODO rename safelist_domains, might have other safelists?
7
+ attr_accessor :safelist_domains
8
+
9
+ def initialize(safelist_domains:)
10
+ @safelist_domains = safelist_domains
11
+ end
12
+
13
+ def perform(body)
14
+ doc = parsed_body(body.join("\n"))
15
+
16
+ doc.css("a[href]").each do |node|
17
+ anchor = Anchor.new(node, safelist_domains: safelist_domains)
18
+ next if anchor.safe?
19
+
20
+ node.set_attribute("rel", anchor.modified_rel)
21
+ end
22
+
23
+ [doc.to_html]
24
+ end
25
+
26
+ private def parsed_body(text)
27
+ if text =~ /<!DOCTYPE/i
28
+ Nokogiri::HTML(text)
29
+ else
30
+ Nokogiri::HTML.fragment(text)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module Relations
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ require_relative 'lib/rack/relations/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rack-relations"
5
+ spec.version = Rack::Relations::VERSION
6
+ spec.authors = ["Patrick Byrne"]
7
+ spec.email = ["code@patrickbyrne.net"]
8
+
9
+ spec.summary = %q{Dynamically rewrite markup to add `rel="nofollow noopener noreferrer"` to non-safelisted links.}
10
+ spec.homepage = "https://github.com/pbyrne/rack-relations.git"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/pbyrne/rack-relations.git"
18
+ spec.metadata["changelog_uri"] = "https://github.com/pbyrne/rack-relations.git/blob/master/CHANGELOG.markdown"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_runtime_dependency "nokogiri", "~> 1.10"
28
+ spec.add_runtime_dependency "rack", ">= 2.0", "< 3"
29
+
30
+ spec.add_development_dependency "mocha"
31
+ spec.add_development_dependency "pry"
32
+ spec.add_development_dependency "rack-test"
33
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-relations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Byrne
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '3'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '2.0'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '3'
47
+ - !ruby/object:Gem::Dependency
48
+ name: mocha
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: pry
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rack-test
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description:
90
+ email:
91
+ - code@patrickbyrne.net
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".semaphore/semaphore.yml"
98
+ - ".travis.yml"
99
+ - CHANGELOG.markdown
100
+ - CODE_OF_CONDUCT.markdown
101
+ - Gemfile
102
+ - Gemfile.lock
103
+ - LICENSE.txt
104
+ - README.markdown
105
+ - Rakefile
106
+ - bin/console
107
+ - bin/setup
108
+ - lib/rack/relations.rb
109
+ - lib/rack/relations/anchor.rb
110
+ - lib/rack/relations/middleware.rb
111
+ - lib/rack/relations/processor.rb
112
+ - lib/rack/relations/version.rb
113
+ - rack-relations.gemspec
114
+ homepage: https://github.com/pbyrne/rack-relations.git
115
+ licenses:
116
+ - MIT
117
+ metadata:
118
+ allowed_push_host: https://rubygems.org
119
+ homepage_uri: https://github.com/pbyrne/rack-relations.git
120
+ source_code_uri: https://github.com/pbyrne/rack-relations.git
121
+ changelog_uri: https://github.com/pbyrne/rack-relations.git/blob/master/CHANGELOG.markdown
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 2.3.0
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubygems_version: 3.0.3
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Dynamically rewrite markup to add `rel="nofollow noopener noreferrer"` to
141
+ non-safelisted links.
142
+ test_files: []