mail_auto_link_obfuscation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5f3bee94220bd89058f7d7d87d4f788a08d5e6d
4
+ data.tar.gz: 5752d9c9d1ac194e1f444fd5aeac4fd773b474dc
5
+ SHA512:
6
+ metadata.gz: 3a6bb4b166403f21145a2b55d8490d45be38fa5f686efc73cb92a542aab56700c4299d9ee4d1d8e4daf64b6f5806d574cff85089490cc9eb615c343c6bfc1c06
7
+ data.tar.gz: d7aeb2e4d48caf00b4072c58c94edd72f32ed275d9cd84aeb74a7811bf332692100944365da4e26dba68e8d6aa4806e5b6c184d4626c7c37854a44be6ff3eb24
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.3
6
+
7
+ Metrics/LineLength:
8
+ Max: 125
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+
4
+ # Specify your gem's dependencies in mail_auto_link_obfuscation.gemspec
5
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ guard :rspec, cmd: 'bundle exec rspec' do
3
+ require 'guard/rspec/dsl'
4
+ dsl = Guard::RSpec::Dsl.new(self)
5
+
6
+ # RSpec files
7
+ rspec = dsl.rspec
8
+ watch(rspec.spec_helper) { rspec.spec_dir }
9
+ watch(rspec.spec_support) { rspec.spec_dir }
10
+ watch(rspec.spec_files)
11
+
12
+ # Ruby files
13
+ ruby = dsl.ruby
14
+ dsl.watch_spec_files_for(ruby.lib_files)
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Oliver Jundt
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,51 @@
1
+ # MailAutoLinkObfuscation
2
+
3
+ This gem hooks up your Rails application and prevents email clients from automatically converting link-like text (e.g. `example.com`, `info@example.com`, `ftp://123.234.234.234`) to hyperlinks in your emails.
4
+
5
+ Automatic links can be an undesired feature, especially when user generated content is part of your emails, e.g. a user's name. If your user is called `your.enemy.com` and you insert his name directly in your mail, you will find that most email clients will make this name clickable. This effect can brake your email layout/design and even worse, it can be considered a security issue.
6
+
7
+ To prevent email clients from auto-linking any link-like text we have to outsmart their link parsers. Inserting `span` tags in HTML parts and spaces in text parts has shown to work for most email clients. For HTML parts this obfuscation should be invisible. For text parts, it's unfortunately not.
8
+
9
+ HTML example: `Hello your.enemy.com!` becomes `Hello your<span>.</span>enemy<span>.</span>com`
10
+ Plain text example: `Hello your.enemy.com!` becomes `Hello your .enemy .com`
11
+
12
+ Note that this module will not touch any explicit links mentioned in anchors in the `href` attribute. Those links are considered desired and trusted. If you provide HTML and text parts with your email (which you should) this gem is also smart enough not to change links in the text part if those have been explicitly hyperlinked in the HTML part.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'mail_auto_link_obfuscation'
20
+ ```
21
+
22
+ ## Usage
23
+ Simply include `MailAutoLinkObfuscation::Automatic` in the mailers where you want obfuscate links.
24
+
25
+ ```ruby
26
+ class MyMailer
27
+ include MailAutoLinkObfuscation::Automatic
28
+ end
29
+ ```
30
+
31
+ ## Configuration
32
+ The obfuscation process can be configured at two places:
33
+
34
+ 1. `Rails.application.config.mail_auto_link_obfuscation`
35
+ 2. `MyMailer#mail_auto_link_obfuscation_options`
36
+
37
+ Options are passed as a hash. At this moment you can only add a `style` attribute to the inserted `span` tags using `{ span_style: "font:inherit" }`
38
+
39
+ ## Development
40
+
41
+ Specs can be run with `rake spec`. Guard is also available.
42
+
43
+ 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).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/moneybird/mail_auto_link_obfuscation. 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.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ require 'mail_auto_link_obfuscation/version'
3
+ require 'mail_auto_link_obfuscation/automatic'
4
+ require 'mail_auto_link_obfuscation/obfuscate_auto_links_on_delivery'
5
+ require 'mail_auto_link_obfuscation/auto_link_obfuscator'
6
+ require 'mail_auto_link_obfuscation/railtie'
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+ require 'nokogiri'
3
+
4
+ module MailAutoLinkObfuscation
5
+ class AutoLinkObfuscator
6
+ AUTO_LINKED_EMAIL_PATTERN = /\S+@\w+(?:\.\w+)+/
7
+ AUTO_LINKED_URL_PATTERN = %r{(\w+://\w+(\.\w+)*|\w+(\.\w+)*\.\w{2,})\S*}
8
+ AUTO_LINKED_PATTERN = Regexp.new([AUTO_LINKED_EMAIL_PATTERN, AUTO_LINKED_URL_PATTERN].join('|'))
9
+ KEY_CHARS = /[@.:]/
10
+
11
+ def initialize(mail, options)
12
+ @mail = mail
13
+ @options = options || {}
14
+ end
15
+
16
+ def run
17
+ extract_link_whitelist_from_html
18
+ transform_html_body if @mail.content_type == 'text/html'
19
+ transform_text_body if @mail.content_type == 'text/plain'
20
+ transform_html_part if @mail.html_part
21
+ transform_text_part if @mail.text_part
22
+ @mail
23
+ end
24
+
25
+ def extract_link_whitelist_from_html
26
+ @link_whitelist =
27
+ extract_link_whitelist_from(html_body_doc) +
28
+ extract_link_whitelist_from(html_part_doc)
29
+ end
30
+
31
+ def extract_link_whitelist_from(doc)
32
+ return Set.new unless doc
33
+ doc.xpath('//@href').map(&:content).to_set
34
+ end
35
+
36
+ def html_body_doc
37
+ return unless @mail.content_type == 'text/html'
38
+ @html_body_doc ||= Nokogiri::HTML(@mail.body.decoded)
39
+ end
40
+
41
+ def html_part_doc
42
+ return unless @mail.html_part
43
+ @html_part_doc ||= Nokogiri::HTML(@mail.html_part.body.decoded)
44
+ end
45
+
46
+ def transform_html_body
47
+ @mail.body = transform_html(html_body_doc)
48
+ end
49
+
50
+ def transform_html_part
51
+ @mail.html_part.body = transform_html(html_part_doc)
52
+ end
53
+
54
+ def transform_html(doc)
55
+ doc.xpath('//body/descendant::text()').each do |node|
56
+ content = transform_auto_linked_pattern(node.content) do |match|
57
+ match.gsub(KEY_CHARS, span_template)
58
+ end
59
+
60
+ node.replace(content)
61
+ end
62
+
63
+ doc.to_s
64
+ end
65
+
66
+ def span_template
67
+ @span_template ||= '<span' + (@options[:span_style] ? " style=#{@options[:span_style]}" : '') + '>\0</span>'
68
+ end
69
+
70
+ def transform_text_body
71
+ @mail.body = transform_text(@mail.body.decoded)
72
+ end
73
+
74
+ def transform_text_part
75
+ @mail.text_part.body = transform_text(@mail.text_part.body.decoded)
76
+ end
77
+
78
+ def transform_text(text)
79
+ transform_auto_linked_pattern(text) do |match|
80
+ match.gsub(KEY_CHARS, ' \0')
81
+ end
82
+ end
83
+
84
+ def transform_auto_linked_pattern(text)
85
+ text.gsub(AUTO_LINKED_PATTERN) do |match|
86
+ @link_whitelist.include?(match) ? match : yield(match)
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ module MailAutoLinkObfuscation
3
+ module Automatic
4
+ def mail(*args, &block)
5
+ super.tap do |email|
6
+ email.extend ObfuscateAutoLinksOnDelivery
7
+ email.mail_auto_link_obfuscation_options = mail_auto_link_obfuscation_options.try(:dup)
8
+ end
9
+ end
10
+
11
+ def mail_auto_link_obfuscation_options
12
+ ::Rails.application.config.mail_auto_link_obfuscation
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ module MailAutoLinkObfuscation
3
+ module ObfuscateAutoLinksOnDelivery
4
+ attr_accessor :mail_auto_link_obfuscation_options
5
+
6
+ def deliver
7
+ obfuscate_links
8
+ super
9
+ end
10
+
11
+ def deliver!
12
+ obfuscate_links
13
+ super
14
+ end
15
+
16
+ private
17
+
18
+ def obfuscate_links
19
+ AutoLinkObfuscator.new(self, mail_auto_link_obfuscation_options).run
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ require 'rails'
3
+
4
+ module MailAutoLinkObfuscation
5
+ class Railtie < ::Rails::Railtie
6
+ config.mail_auto_link_obfuscation = {}
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module MailAutoLinkObfuscation
3
+ VERSION = '0.1.0'
4
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'mail_auto_link_obfuscation/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'mail_auto_link_obfuscation'
9
+ spec.version = MailAutoLinkObfuscation::VERSION
10
+ spec.authors = ['Oliver Jundt']
11
+ spec.email = ['info@moneybird.com']
12
+
13
+ spec.summary = 'Obfuscate link-like mail content on delivery to prevent auto hyperlinks in modern email clients.'
14
+ spec.description = 'Obfuscate link-like mail content on delivery to prevent auto hyperlinks in modern email clients.'
15
+ spec.homepage = ''
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'railties', '>= 3.0', '< 5.1'
22
+ spec.add_dependency 'mail', '~> 2.5'
23
+ spec.add_dependency 'nokogiri', '~> 1.6'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.12'
26
+ spec.add_development_dependency 'rails', '>= 3.0', '< 5.1'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'rubocop', '~> 0.42'
30
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.7'
31
+ spec.add_development_dependency 'guard', '~> 2.14'
32
+ spec.add_development_dependency 'guard-rspec', '~> 4.7'
33
+ end
metadata ADDED
@@ -0,0 +1,227 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail_auto_link_obfuscation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oliver Jundt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.1'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: mail
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.5'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.5'
47
+ - !ruby/object:Gem::Dependency
48
+ name: nokogiri
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.6'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.6'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.12'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.12'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rails
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ - - "<"
83
+ - !ruby/object:Gem::Version
84
+ version: '5.1'
85
+ type: :development
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '3.0'
92
+ - - "<"
93
+ - !ruby/object:Gem::Version
94
+ version: '5.1'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rake
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '10.0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '10.0'
109
+ - !ruby/object:Gem::Dependency
110
+ name: rspec
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '3.0'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: '3.0'
123
+ - !ruby/object:Gem::Dependency
124
+ name: rubocop
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '0.42'
130
+ type: :development
131
+ prerelease: false
132
+ version_requirements: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - "~>"
135
+ - !ruby/object:Gem::Version
136
+ version: '0.42'
137
+ - !ruby/object:Gem::Dependency
138
+ name: rubocop-rspec
139
+ requirement: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - "~>"
142
+ - !ruby/object:Gem::Version
143
+ version: '1.7'
144
+ type: :development
145
+ prerelease: false
146
+ version_requirements: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - "~>"
149
+ - !ruby/object:Gem::Version
150
+ version: '1.7'
151
+ - !ruby/object:Gem::Dependency
152
+ name: guard
153
+ requirement: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - "~>"
156
+ - !ruby/object:Gem::Version
157
+ version: '2.14'
158
+ type: :development
159
+ prerelease: false
160
+ version_requirements: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - "~>"
163
+ - !ruby/object:Gem::Version
164
+ version: '2.14'
165
+ - !ruby/object:Gem::Dependency
166
+ name: guard-rspec
167
+ requirement: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - "~>"
170
+ - !ruby/object:Gem::Version
171
+ version: '4.7'
172
+ type: :development
173
+ prerelease: false
174
+ version_requirements: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - "~>"
177
+ - !ruby/object:Gem::Version
178
+ version: '4.7'
179
+ description: Obfuscate link-like mail content on delivery to prevent auto hyperlinks
180
+ in modern email clients.
181
+ email:
182
+ - info@moneybird.com
183
+ executables: []
184
+ extensions: []
185
+ extra_rdoc_files: []
186
+ files:
187
+ - ".gitignore"
188
+ - ".rspec"
189
+ - ".rubocop.yml"
190
+ - Gemfile
191
+ - Guardfile
192
+ - LICENSE.txt
193
+ - README.md
194
+ - Rakefile
195
+ - lib/mail_auto_link_obfuscation.rb
196
+ - lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb
197
+ - lib/mail_auto_link_obfuscation/automatic.rb
198
+ - lib/mail_auto_link_obfuscation/obfuscate_auto_links_on_delivery.rb
199
+ - lib/mail_auto_link_obfuscation/railtie.rb
200
+ - lib/mail_auto_link_obfuscation/version.rb
201
+ - mail_auto_link_obfuscation.gemspec
202
+ homepage: ''
203
+ licenses:
204
+ - MIT
205
+ metadata: {}
206
+ post_install_message:
207
+ rdoc_options: []
208
+ require_paths:
209
+ - lib
210
+ required_ruby_version: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ version: '0'
215
+ required_rubygems_version: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - ">="
218
+ - !ruby/object:Gem::Version
219
+ version: '0'
220
+ requirements: []
221
+ rubyforge_project:
222
+ rubygems_version: 2.5.1
223
+ signing_key:
224
+ specification_version: 4
225
+ summary: Obfuscate link-like mail content on delivery to prevent auto hyperlinks in
226
+ modern email clients.
227
+ test_files: []