sanitize_attr 2.0.0
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.
- checksums.yaml +15 -0
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +6 -0
- data/lib/sanitize_attr.rb +28 -0
- data/lib/sanitize_attr/railtie.rb +9 -0
- data/lib/sanitize_attr/version.rb +3 -0
- data/sanitize_attr.gemspec +25 -0
- data/spec/sanitize_attr_spec.rb +78 -0
- data/spec/spec_helper.rb +3 -0
- metadata +115 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
!binary "U0hBMQ==":
|
|
3
|
+
metadata.gz: !binary |-
|
|
4
|
+
N2FlNzlhOTQ2NWNjZTZmMTM3N2NlYzI0ZmY3MDI2MTA1OTBhM2Q4Zg==
|
|
5
|
+
data.tar.gz: !binary |-
|
|
6
|
+
MDgwMWI0YjJmOTMzYjZmMTE1MDdmMTMyMjA5MjNiZGNiNTdlYzA3Yw==
|
|
7
|
+
SHA512:
|
|
8
|
+
metadata.gz: !binary |-
|
|
9
|
+
MTk3MzcyYzI5ODMyMWZkYjNmZmNlMjZlYjVhYTA5MGM0YWE2YTlmYjVjYjY1
|
|
10
|
+
YjlmMTk5MGI3MThjNjgzYzU4N2NjNTgxNDk0ZDg3NzYxYThjMDE0NmJmZmI3
|
|
11
|
+
MzBiZmUyMTljYzE5MjViNmMwYTVlYWQ5MmExZDY3ZDgxMTUwM2I=
|
|
12
|
+
data.tar.gz: !binary |-
|
|
13
|
+
YjQxMDk5NTA5ZjRlM2IyOTE0ODkyMmJiOWQ5ZmZjMTJhNGYxNjcxZjJiZTIy
|
|
14
|
+
N2EyNzEzOTVmOWNlYTYxNjM4YmJhNTRhMzgxODE2MGNiZjY1ZDJlODNmODMy
|
|
15
|
+
YmIzNzQ1ODY4OTUyMTBjMDNiZmVjOGQzZjI0ODk3NTllNTI3NGI=
|
data/.gitignore
ADDED
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 Daniel Vandersluis
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# SanitizeAttr
|
|
2
|
+
|
|
3
|
+
[](https://travis-ci.org/dvandersluis/sanitize_attr)
|
|
4
|
+
|
|
5
|
+
Automatically run AR attributes through Sanitize.clean before validation.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
gem 'sanitize_attr'
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install sanitize_attr
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
TODO: Write usage instructions here
|
|
24
|
+
|
|
25
|
+
## Contributing
|
|
26
|
+
|
|
27
|
+
1. Fork it
|
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
31
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'active_support/core_ext/string'
|
|
2
|
+
require 'sanitize'
|
|
3
|
+
require 'sanitize_attr/railtie' if defined? Rails::Railtie
|
|
4
|
+
|
|
5
|
+
module SanitizeAttr
|
|
6
|
+
def self.included(klass)
|
|
7
|
+
klass.extend(ClassMethods)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
module ClassMethods
|
|
11
|
+
def sanitize_attr(*columns)
|
|
12
|
+
options = columns.extract_options!
|
|
13
|
+
config = options.fetch(:config, Sanitize::Config::BASIC) # Allow basic HTML elements by default
|
|
14
|
+
|
|
15
|
+
config = Sanitize::Config.const_get(config.to_s.upcase) if config.is_a?(Symbol)
|
|
16
|
+
config ||= Sanitize::Config::BASIC
|
|
17
|
+
|
|
18
|
+
before_validation do
|
|
19
|
+
columns.each do |column|
|
|
20
|
+
send(:"#{column}=", Sanitize.fragment(send(column), config)) if send(column).is_a?(String)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
alias_method :sanitize_column, :sanitize_attr
|
|
26
|
+
alias_method :sanitize_columns, :sanitize_column
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'sanitize_attr/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |gem|
|
|
7
|
+
gem.name = "sanitize_attr"
|
|
8
|
+
gem.version = SanitizeAttr::VERSION
|
|
9
|
+
gem.authors = ["Daniel Vandersluis"]
|
|
10
|
+
gem.email = ["dvandersluis@selfmgmt.com"]
|
|
11
|
+
gem.description = %q{Automatically pass attributes through Sanitize before validation}
|
|
12
|
+
gem.summary = %q{Automatically pass attributes through Sanitize before validation.}
|
|
13
|
+
gem.homepage = ""
|
|
14
|
+
|
|
15
|
+
gem.files = `git ls-files`.split($/)
|
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
18
|
+
gem.require_paths = ["lib"]
|
|
19
|
+
|
|
20
|
+
gem.add_dependency "activerecord", ">= 3.0.0"
|
|
21
|
+
gem.add_dependency "sanitize", '>= 3.0.0'
|
|
22
|
+
|
|
23
|
+
gem.add_development_dependency "rspec"
|
|
24
|
+
gem.add_development_dependency "rake"
|
|
25
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'active_model'
|
|
3
|
+
|
|
4
|
+
class Article < Struct.new(:name, :description, :summary)
|
|
5
|
+
include ActiveModel::Validations
|
|
6
|
+
include ActiveModel::Callbacks
|
|
7
|
+
|
|
8
|
+
define_model_callbacks :validation
|
|
9
|
+
|
|
10
|
+
include SanitizeAttr
|
|
11
|
+
|
|
12
|
+
def valid?
|
|
13
|
+
run_callbacks(:validation) { super }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe SanitizeAttr do
|
|
18
|
+
before { Article.reset_callbacks :validation }
|
|
19
|
+
|
|
20
|
+
let(:name) { '<b onclick="alert()">My Name</b> <script type="text/javascript">evil()</script>'}
|
|
21
|
+
let(:description) { '<i>Description</i><iframe></iframe>'}
|
|
22
|
+
let(:summary) { '<html></html>' }
|
|
23
|
+
|
|
24
|
+
subject { Article.new(name, description, summary) }
|
|
25
|
+
|
|
26
|
+
it 'should sanitize only specified attributes' do
|
|
27
|
+
Article.sanitize_attr :name, :description
|
|
28
|
+
subject.valid?
|
|
29
|
+
|
|
30
|
+
subject.name.should == '<b>My Name</b> evil()'
|
|
31
|
+
subject.description.should == '<i>Description</i>'
|
|
32
|
+
subject.summary.should == '<html></html>'
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it 'should sanitize using the provided config' do
|
|
36
|
+
Article.sanitize_attr :name, :description, config: Sanitize::Config::DEFAULT
|
|
37
|
+
subject.valid?
|
|
38
|
+
|
|
39
|
+
subject.name.should == 'My Name evil()'
|
|
40
|
+
subject.description.should == 'Description'
|
|
41
|
+
subject.summary.should == '<html></html>'
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it 'should allow config to be defined as a symbol' do
|
|
45
|
+
Article.sanitize_attr :name, :description, config: :default
|
|
46
|
+
subject.valid?
|
|
47
|
+
|
|
48
|
+
subject.name.should == 'My Name evil()'
|
|
49
|
+
subject.description.should == 'Description'
|
|
50
|
+
subject.summary.should == '<html></html>'
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it 'should allow default to BASIC config if config is nil' do
|
|
54
|
+
Article.sanitize_attr :name, :description, config: nil
|
|
55
|
+
subject.valid?
|
|
56
|
+
|
|
57
|
+
subject.name.should == '<b>My Name</b> evil()'
|
|
58
|
+
subject.description.should == '<i>Description</i>'
|
|
59
|
+
subject.summary.should == '<html></html>'
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it 'should sanitize different columns with different configs' do
|
|
63
|
+
Article.sanitize_attr :name, config: :default
|
|
64
|
+
Article.sanitize_attr :description, config: :basic
|
|
65
|
+
subject.valid?
|
|
66
|
+
|
|
67
|
+
subject.name.should == 'My Name evil()'
|
|
68
|
+
subject.description.should == '<i>Description</i>'
|
|
69
|
+
subject.summary.should == '<html></html>'
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it 'should not sanitize if validation is not triggered' do
|
|
73
|
+
Article.sanitize_attr :name, :description
|
|
74
|
+
|
|
75
|
+
Sanitize.should_not_receive(:fragment)
|
|
76
|
+
subject
|
|
77
|
+
end
|
|
78
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sanitize_attr
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 2.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Daniel Vandersluis
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-04-01 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
type: :runtime
|
|
15
|
+
prerelease: false
|
|
16
|
+
name: activerecord
|
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 3.0.0
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ! '>='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 3.0.0
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
type: :runtime
|
|
29
|
+
prerelease: false
|
|
30
|
+
name: sanitize
|
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
|
32
|
+
requirements:
|
|
33
|
+
- - ! '>='
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: 3.0.0
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ! '>='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 3.0.0
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
type: :development
|
|
43
|
+
prerelease: false
|
|
44
|
+
name: rspec
|
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - ! '>='
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ! '>='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
type: :development
|
|
57
|
+
prerelease: false
|
|
58
|
+
name: rake
|
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ! '>='
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: '0'
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ! '>='
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
description: Automatically pass attributes through Sanitize before validation
|
|
70
|
+
email:
|
|
71
|
+
- dvandersluis@selfmgmt.com
|
|
72
|
+
executables: []
|
|
73
|
+
extensions: []
|
|
74
|
+
extra_rdoc_files: []
|
|
75
|
+
files:
|
|
76
|
+
- .gitignore
|
|
77
|
+
- .rspec
|
|
78
|
+
- .travis.yml
|
|
79
|
+
- Gemfile
|
|
80
|
+
- LICENSE.txt
|
|
81
|
+
- README.md
|
|
82
|
+
- Rakefile
|
|
83
|
+
- lib/sanitize_attr.rb
|
|
84
|
+
- lib/sanitize_attr/railtie.rb
|
|
85
|
+
- lib/sanitize_attr/version.rb
|
|
86
|
+
- sanitize_attr.gemspec
|
|
87
|
+
- spec/sanitize_attr_spec.rb
|
|
88
|
+
- spec/spec_helper.rb
|
|
89
|
+
homepage: ''
|
|
90
|
+
licenses: []
|
|
91
|
+
metadata: {}
|
|
92
|
+
post_install_message:
|
|
93
|
+
rdoc_options: []
|
|
94
|
+
require_paths:
|
|
95
|
+
- lib
|
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
|
+
requirements:
|
|
98
|
+
- - ! '>='
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '0'
|
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
|
+
requirements:
|
|
103
|
+
- - ! '>='
|
|
104
|
+
- !ruby/object:Gem::Version
|
|
105
|
+
version: '0'
|
|
106
|
+
requirements: []
|
|
107
|
+
rubyforge_project:
|
|
108
|
+
rubygems_version: 2.1.5
|
|
109
|
+
signing_key:
|
|
110
|
+
specification_version: 4
|
|
111
|
+
summary: Automatically pass attributes through Sanitize before validation.
|
|
112
|
+
test_files:
|
|
113
|
+
- spec/sanitize_attr_spec.rb
|
|
114
|
+
- spec/spec_helper.rb
|
|
115
|
+
has_rdoc:
|