sweet_params 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +82 -0
- data/Rakefile +7 -0
- data/lib/sweet_params.rb +9 -0
- data/lib/sweet_params/extensions.rb +32 -0
- data/lib/sweet_params/version.rb +3 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/sweet_params_spec.rb +69 -0
- data/sweet_params.gemspec +27 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 24b150701c6e6072aa8bb62ba842844a68d7cd70
|
4
|
+
data.tar.gz: e68ffaff84ee2cedc227cb46a6e7cc356023731d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cac1f99f54efbbeb04b871be84b771ce47152ad26be920920c63edadcbbb48b0de0ec4086fe7e0c0e8dfa589eff5351e5fb87496b2335b67b26c918de08c1125
|
7
|
+
data.tar.gz: ee68481da6fde49e27410d2013734b877209f5fe7f87668b54668ad65e20cb15cb3b434e1a8742ce08b114bbffe90d51f5d8b88cefa42e9e721a685d431d66e7
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Evgeny Likholetov <bsboris@gmail.com>
|
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,82 @@
|
|
1
|
+
# Sweet Params
|
2
|
+
|
3
|
+
Syntax sugar for Rails Strong Parameters, making them sweet and tasty to work with.
|
4
|
+
|
5
|
+
This plugin protects you from bad practice of using #to_sym on user provided params when comparing with known value (e.g. when using params for filters or scopes):
|
6
|
+
|
7
|
+
if params[:scope].to_sym == :recent
|
8
|
+
...
|
9
|
+
Symbols are not garbage collectable, so the code above has potential DoS vulnerability.
|
10
|
+
Attacker can send zillion of long random `[:scope]` params and your server will soon run out of memory.
|
11
|
+
|
12
|
+
Of course, you can use strings instead of symbols for known values, but this just doesn't feel right. In Ruby, we used to symbols when naming things.
|
13
|
+
|
14
|
+
So here goes Sweet Params, providing convinient (and safe!) methods for working with params using symbols:
|
15
|
+
|
16
|
+
def index
|
17
|
+
@posts = if params.has?(:scope, in: :recent)
|
18
|
+
Post.recent
|
19
|
+
elsif params.has?(:scope, in: %i(archived old))
|
20
|
+
Post.old
|
21
|
+
else
|
22
|
+
Post.all
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
or using `#validate_to_sym` and `case` statement:
|
27
|
+
|
28
|
+
def index
|
29
|
+
@posts = case params.validate_to_sym(:scope, in: %i(recent archived old))
|
30
|
+
when :recent then Post.recent
|
31
|
+
when :archived, :old then Post.old
|
32
|
+
else Post.all
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
## Installation
|
37
|
+
|
38
|
+
Add this line to your application's Gemfile:
|
39
|
+
|
40
|
+
gem 'sweet_params', '~> 0.0.1'
|
41
|
+
|
42
|
+
And then execute:
|
43
|
+
|
44
|
+
$ bundle
|
45
|
+
|
46
|
+
Or install it yourself as:
|
47
|
+
|
48
|
+
$ gem install sweet_params
|
49
|
+
|
50
|
+
## Usage
|
51
|
+
|
52
|
+
Testing whether param is present:
|
53
|
+
|
54
|
+
params.has?(:scope) # => params[:scope].present?
|
55
|
+
|
56
|
+
Multidimensional hashes are supported:
|
57
|
+
|
58
|
+
params.has?([:filter, :scope]) # => params[:filter][:scope].present?
|
59
|
+
|
60
|
+
Validating params with single:
|
61
|
+
|
62
|
+
params.has?(:scope, in: :recent) #=> params[:scope].to_s == :recent.to_s
|
63
|
+
|
64
|
+
... or multiple values:
|
65
|
+
|
66
|
+
params.has?(:scope, in: %i(recent new)) #=> params[:scope].to_s == :recent.to_s or params[:scope].to_s == :new.to_s
|
67
|
+
|
68
|
+
Or you can just get the param, ensure that it is allowed and work with it your way:
|
69
|
+
|
70
|
+
params.validate(:scope, in: %i(hot recent)) # => params[:scope] or nil if params is not in whitelist
|
71
|
+
|
72
|
+
You can convert param to symbol (but only if it whitelisted)
|
73
|
+
|
74
|
+
params.validate_to_sym(:scope, in: %i(hot recent)) # => params[:scope].to_sym or nil if params is not in whitelist
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
1. Fork it
|
79
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
80
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
81
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
82
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/sweet_params.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module SweetParams
|
2
|
+
module Extensions
|
3
|
+
def has?(path, options = nil)
|
4
|
+
options ? !!validate(path, options) : get_param_by_path(path).present?
|
5
|
+
end
|
6
|
+
|
7
|
+
def validate(path, options)
|
8
|
+
param = get_param_by_path(path)
|
9
|
+
param.present? && allowed?(param, options) ? param : nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def validate_to_sym(path, options)
|
13
|
+
validate(path, options).try(:to_sym)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def get_param_by_path(*path)
|
19
|
+
path.flatten.reduce(self) { |hash, key| hash && hash[key] }
|
20
|
+
end
|
21
|
+
|
22
|
+
def allowed?(param, options)
|
23
|
+
if (whitelist = *options[:in]).any?
|
24
|
+
whitelist.flatten.map(&:to_s).include?(param)
|
25
|
+
else
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
ActionController::Parameters.send :include, SweetParams::Extensions
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'minitest/autorun'
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sweet_params'
|
3
|
+
|
4
|
+
describe SweetParams do
|
5
|
+
let(:params) { ActionController::Parameters.new(scope: 'recent', filter: { scope: 'recent' }, empty: '') }
|
6
|
+
|
7
|
+
describe '#has?' do
|
8
|
+
it 'should respond to method' do
|
9
|
+
params.must_respond_to :has?
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should be true if parameter is present' do
|
13
|
+
params.has?(:scope, in: [:recent, :new]).must_equal true
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should be false if parameter is missing' do
|
17
|
+
params.has?(:empty).must_equal false
|
18
|
+
params.has?(:not_here).must_equal false
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should handle multi-dimensional params hash' do
|
22
|
+
params.has?([:filter, :scope]).must_equal true
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should handle multi-dimensional params hash' do
|
26
|
+
params.has?([:filter, :not_here]).must_equal false
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should use single value as whitelist' do
|
30
|
+
params.has?(:scope, in: :recent).must_equal true
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should use array as whitelist' do
|
34
|
+
params.has?(:scope, in: [:recent, :new]).must_equal true
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should not allow not whitelisted params' do
|
38
|
+
params.has?(:scope, in: [:hot, :new]).must_equal false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#validate' do
|
43
|
+
it 'should respond to method' do
|
44
|
+
params.must_respond_to :validate
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should allow whitelisted param' do
|
48
|
+
params.validate(:scope, in: [:hot, :recent]).must_equal 'recent'
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should return nil for not whitelisted param' do
|
52
|
+
params.validate(:scope, in: [:hot, :new]).must_equal nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#validate_to_sym' do
|
57
|
+
it 'should respond to method' do
|
58
|
+
params.must_respond_to :validate_to_sym
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should symbolize whitelisted param' do
|
62
|
+
params.validate_to_sym(:scope, in: [:hot, :recent]).must_equal :recent
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should return nil for not whitelisted param' do
|
66
|
+
params.validate_to_sym(:scope, in: [:hot, :new]).must_equal nil
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sweet_params/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'sweet_params'
|
8
|
+
spec.version = SweetParams::VERSION
|
9
|
+
spec.platform = Gem::Platform::RUBY
|
10
|
+
spec.authors = ['Evgeny Likholetov']
|
11
|
+
spec.email = ['bsboris@gmail.com']
|
12
|
+
spec.description = 'Syntax sugar for Rails Strong Parameters.'
|
13
|
+
spec.summary = 'Syntax sugar for Rails Strong Parameters.'
|
14
|
+
spec.homepage = 'https://github.com/bsboris/sweet_params'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'activesupport', '>= 4.0'
|
22
|
+
spec.add_dependency 'actionpack', '>= 4.0'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
25
|
+
spec.add_development_dependency 'minitest', '~> 4.2'
|
26
|
+
spec.add_development_dependency 'rake'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sweet_params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Evgeny Likholetov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionpack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Syntax sugar for Rails Strong Parameters.
|
84
|
+
email:
|
85
|
+
- bsboris@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/sweet_params.rb
|
96
|
+
- lib/sweet_params/extensions.rb
|
97
|
+
- lib/sweet_params/version.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
- spec/sweet_params_spec.rb
|
100
|
+
- sweet_params.gemspec
|
101
|
+
homepage: https://github.com/bsboris/sweet_params
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.2.0
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Syntax sugar for Rails Strong Parameters.
|
125
|
+
test_files:
|
126
|
+
- spec/spec_helper.rb
|
127
|
+
- spec/sweet_params_spec.rb
|