acts_as_html_sanitized 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1382947b56257ef40f8dacdaac8cba08d0f5e9bc
4
+ data.tar.gz: e2724e49f8ebc1821c7c3100887ae8b51aea8835
5
+ SHA512:
6
+ metadata.gz: b7ad77554243a7a944aa105ddb596df3e5b1163f8a22b8af3f61a32a8839880a9415482a905ec67447090b95ca46c73c70f29a51ee5ed6c1d53e8850101812ba
7
+ data.tar.gz: f1ffa1afcc86720fe213a18dec0d1510aa7937167b06ea41c50e66afbb27f80e8708b6f5717f73a140e71ce7a6b086fc7f432d1f232665fca231ab8cf4f28e0d
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Sasha Gerrand
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActsAsHtmlSanitizer'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'lib'
23
+ t.libs << 'test'
24
+ t.pattern = 'test/**/*_test.rb'
25
+ t.verbose = false
26
+ end
27
+
28
+ task default: :test
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+ require 'acts_as_html_sanitized/model_extensions'
3
+
4
+ module ActsAsHtmlSanitized
5
+ if defined?(ActiveRecord::Base)
6
+ ActiveRecord::Base.send(:include, ActsAsHtmlSanitized::ModelExtensions)
7
+ end
8
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_record'
2
+ require 'sanitize'
3
+
4
+ module ActsAsHtmlSanitized
5
+ module ModelExtensions
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def acts_as_html_sanitized
12
+ before_validation do |record|
13
+ for column in record.class.content_columns
14
+ if column.type == :string || column.type == :text
15
+ unless record[column.name].nil?
16
+ record[column.name] = Sanitize.clean(record[column.name])
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsHtmlSanitized
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,80 @@
1
+ require 'test_helper'
2
+
3
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
4
+ ActiveRecord::Schema.verbose = false
5
+
6
+ def rails_3?
7
+ defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 3 && ActiveRecord::VERSION::MAJOR < 4
8
+ end
9
+
10
+ def setup_db
11
+ # AR caches columns options like defaults etc. Clear them!
12
+ ActiveRecord::Base.connection.schema_cache.clear!
13
+ ActiveRecord::Schema.define(version: 1) do
14
+ create_table :tests do |t|
15
+ t.column :str, :string
16
+ t.column :txt, :text
17
+ t.column :created_at, :datetime
18
+ t.column :updated_at, :datetime
19
+ end
20
+ end
21
+ end
22
+
23
+ def teardown_db
24
+ ActiveRecord::Base.connection.tables.each do |table|
25
+ ActiveRecord::Base.connection.drop_table(table)
26
+ end
27
+ end
28
+
29
+ class Test < ActiveRecord::Base
30
+ self.table_name = 'tests'
31
+
32
+ acts_as_html_sanitized
33
+ end
34
+
35
+ class ActsAsHtmlSanitizedTest < MiniTest::Unit::TestCase
36
+ def setup
37
+ ActiveRecord::Base.connection.tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
38
+ setup_db
39
+ end
40
+
41
+ def teardown
42
+ teardown_db
43
+ end
44
+
45
+ def test_module_has_expected_name
46
+ assert_kind_of Module, ActsAsHtmlSanitized
47
+ end
48
+
49
+ def test_string_attrs_are_sanitized
50
+ input = '<em>I am emphasised</em>'
51
+ Test.create(str: input)
52
+ assert_match ' I am emphasised', Test.first.str
53
+ end
54
+
55
+ def test_string_attrs_do_not_contain_html
56
+ input = '<em>I am emphasised</em>'
57
+ Test.create(str: input)
58
+ refute_match input, Test.first.str
59
+ end
60
+
61
+ def test_string_attrs_are_sanitized
62
+ input =<<HTML
63
+ <script type="text/javascript">
64
+ alert('ACHIEVEMENT UNLOCKED: XSS attack'):
65
+ </script>
66
+ HTML
67
+ Test.create(txt: input)
68
+ assert_match "\nalert('ACHIEVEMENT UNLOCKED: XSS attack'):\n\n", Test.first.txt
69
+ end
70
+
71
+ def test_string_attrs_do_not_contain_html
72
+ input =<<HTML
73
+ <script type="text/javascript">
74
+ alert('ACHIEVEMENT UNLOCKED: XSS attack'):
75
+ </script>
76
+ HTML
77
+ Test.create(txt: input)
78
+ refute_match input, Test.first.txt
79
+ end
80
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_record'
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
4
+
5
+ require 'acts_as_html_sanitized'
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_html_sanitized
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sasha Gerrand
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sanitize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Sanitizes your ActiveRecord attributes.
56
+ email:
57
+ - github-acts_as_html_sanitized@sgerrand.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/acts_as_html_sanitized/model_extensions.rb
63
+ - lib/acts_as_html_sanitized/version.rb
64
+ - lib/acts_as_html_sanitized.rb
65
+ - MIT-LICENSE
66
+ - Rakefile
67
+ - test/acts_as_html_sanitized_test.rb
68
+ - test/test_helper.rb
69
+ homepage: https://github.com/sgerrand/acts_as_html_sanitized
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.0.14
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Sanitizes your ActiveRecord attributes.
93
+ test_files:
94
+ - test/acts_as_html_sanitized_test.rb
95
+ - test/test_helper.rb