go_to_param 0.2.0 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 05ddd7d3e4b2514e5f5136af63f0263f0bde08c9
4
- data.tar.gz: 47acd514926425787274fa1452252147007ed803
2
+ SHA256:
3
+ metadata.gz: 9a103b9617231ac26d4b5e82ca46dea4bcfd961cbda378476113d3dcd5ab7303
4
+ data.tar.gz: 0d805e34c66359b35775edc1e77ac9246b8f5d3b9838755d748f4c3acd1ae44b
5
5
  SHA512:
6
- metadata.gz: 2c7e6949c134e0d72dce393533870c28860e8ea44bb51198e42869eba80c36bfee6dc09cb4dd5f288daec909b0f11f31087cfbde871dbbb56d5bcb24241421cb
7
- data.tar.gz: ef121087d69984d484e37a647396076cd528eec877889185abf22f5cdff43bc287611bd4543e5707ce6ad635535d90f0044593dc872a2e8df5c89e95c4802de3
6
+ metadata.gz: 04434b980320f4f2ed8522e9e059973b31b1eb2f5bacb9ad3be482a8e72ddafa859f3974a4385702f973111e25b005377c3865c2696f57f85f0399c1ddf4e1d0
7
+ data.tar.gz: c7d4929d8f99b889b6680fe3c3383c5bad5d092ddb6d64e0dd704882062360c78f15331a8d4e5764e8731b89f73e3fd32229b5ff90dd9a82bdc5d1e012fe000a
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ ## 1.1.2
4
+
5
+ - Fix keyword parameter warning. Thanks to @olleolleolle!
6
+
7
+ ## 1.1.1
8
+
9
+ - Don't raise exceptions if given hash params from hack attempts, such as: `go_to[foo]=bar`.
10
+
11
+ ## Earlier
12
+
13
+ Sorry, no changelog available for earlier versions.
data/README.md CHANGED
@@ -55,6 +55,8 @@ You can pass additional query parameters to include, which could be suitable if
55
55
 
56
56
  Note that these parameters always become transformed into a query string: if you're using Ruby on Rails, they won't be interpreted through your route definitions.
57
57
 
58
+ You can, however, pass a Rails-style `anchor: "foo"` parameter to set the URL fragment (`/example#foo`).
59
+
58
60
 
59
61
  ### hidden_go_to_tag
60
62
 
@@ -114,7 +116,13 @@ class SessionsController < ActionController::Base
114
116
  end
115
117
  ```
116
118
 
117
- Returns nil if the parameter value is not a relative path, to counter phishing attempts like `/login?go_to=http://evil.com/success_now_give_me_your_cc_details`.
119
+ Returns `nil` if the parameter value is not a relative path, to counter phishing attempts like `/login?go_to=http://evil.com/success_now_give_me_your_cc_details`.
120
+
121
+ If you need to allow some external URLs, that can be configured. You could do this in e.g. a `config/initializers/go_to.rb` file:
122
+
123
+ ``` ruby
124
+ GoToParam.allow_redirect_prefix("myapp://")
125
+ ```
118
126
 
119
127
  ### go_to_path_or
120
128
 
@@ -139,6 +147,7 @@ Or install it yourself as:
139
147
 
140
148
  $ gem install go_to_param
141
149
 
150
+
142
151
  ## License
143
152
 
144
153
  Copyright (c) 2013 Henrik Nyh
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
17
  spec.require_paths = ["lib"]
18
18
 
19
- spec.add_development_dependency "bundler", "~> 1.3"
19
+ spec.add_development_dependency "bundler", ">= 1.3"
20
20
  spec.add_development_dependency "rake"
21
21
  spec.add_development_dependency "rspec"
22
22
  end
@@ -2,6 +2,20 @@ require "cgi"
2
2
  require "go_to_param/version"
3
3
 
4
4
  module GoToParam
5
+ def self.allow_redirect_prefix(prefix)
6
+ allowed_redirect_prefixes << prefix
7
+ end
8
+
9
+ def self.allowed_redirect_prefixes
10
+ reset_allowed_redirect_prefixes unless @allowed_redirect_prefixes
11
+ @allowed_redirect_prefixes
12
+ end
13
+
14
+ # Mostly for tests…
15
+ def self.reset_allowed_redirect_prefixes
16
+ @allowed_redirect_prefixes = [ "/" ]
17
+ end
18
+
5
19
  def self.included(klass)
6
20
  klass.helper_method :hidden_go_to_tag, :hidden_go_to_here_tag,
7
21
  :go_to_params, :go_to_here_params,
@@ -21,7 +35,7 @@ module GoToParam
21
35
  end
22
36
 
23
37
  def go_to_here_params(additional_query_params = {})
24
- path = go_to_here_path(additional_query_params)
38
+ path = go_to_here_path(**additional_query_params)
25
39
 
26
40
  if path
27
41
  { go_to: path }
@@ -31,8 +45,10 @@ module GoToParam
31
45
  end
32
46
 
33
47
  def go_to_path
48
+ return nil if go_to_param_value.nil?
49
+
34
50
  # Avoid phishing redirects.
35
- if go_to_param_value.to_s.start_with?("/")
51
+ if matches_allowed_redirect_prefixes?
36
52
  go_to_param_value
37
53
  else
38
54
  nil
@@ -45,16 +61,23 @@ module GoToParam
45
61
 
46
62
  private
47
63
 
48
- def go_to_here_path(additional_query_params = {})
64
+ def matches_allowed_redirect_prefixes?
65
+ GoToParam.allowed_redirect_prefixes.any? { |prefix| go_to_param_value.start_with?(prefix) }
66
+ end
67
+
68
+ def go_to_here_path(anchor: nil, **additional_query_params)
49
69
  if request.get?
50
- _go_to_add_query_string_from_hash(request.fullpath, additional_query_params)
70
+ path_without_anchor = _go_to_add_query_string_from_hash(_go_to_fullpath, additional_query_params)
71
+ anchor ? path_without_anchor + "#" + anchor : path_without_anchor
51
72
  else
52
73
  nil
53
74
  end
54
75
  end
55
76
 
56
77
  def go_to_param_value
57
- params[:go_to]
78
+ # We use `to_s` to avoid "not a string" type errors from hack attempts where a hash is passed, e.g. "go_to[foo]=bar".
79
+ value = params[:go_to].to_s
80
+ value == "" ? nil : value
58
81
  end
59
82
 
60
83
  # Named this way to avoid conflicts. TODO: http://thepugautomatic.com/2014/02/private-api/
@@ -67,4 +90,11 @@ module GoToParam
67
90
  [ path, separator, query_string ].join
68
91
  end
69
92
  end
93
+
94
+ # Prevent encoding errors ("incompatible character encodings: UTF-8 and ASCII-8BIT") for certain malformed requests.
95
+ # Inspired by https://github.com/discourse/discourse/commit/090dc80f8a23dbb3ad703efbac990aa917c06505
96
+ def _go_to_fullpath
97
+ path = request.fullpath
98
+ path.dup.force_encoding("UTF-8").scrub
99
+ end
70
100
  end
@@ -1,3 +1,3 @@
1
1
  module GoToParam
2
- VERSION = "0.2.0"
2
+ VERSION = "1.1.2"
3
3
  end
@@ -1,25 +1,29 @@
1
1
  require_relative "../lib/go_to_param"
2
2
 
3
- class FakeController
4
- attr_accessor :params, :view_context, :request
3
+ describe GoToParam do
4
+ let(:controller_klass) do
5
+ Class.new do
6
+ attr_accessor :params, :view_context, :request
5
7
 
6
- def self.helper_method(*methods)
7
- @helper_methods = methods
8
- end
8
+ def self.helper_method(*methods)
9
+ @helper_methods = methods
10
+ end
11
+
12
+ def self.helper_methods
13
+ @helper_methods
14
+ end
9
15
 
10
- def self.helper_methods
11
- @helper_methods
16
+ include GoToParam
17
+ end
12
18
  end
13
19
 
14
- include GoToParam
15
- end
20
+ after { GoToParam.reset_allowed_redirect_prefixes }
16
21
 
17
- describe GoToParam do
18
- let(:controller) { FakeController.new }
22
+ let(:controller) { controller_klass.new }
19
23
 
20
24
  describe "#hidden_go_to_tag" do
21
25
  it "becomes a helper method" do
22
- expect(FakeController.helper_methods).to include :hidden_go_to_tag
26
+ expect(controller_klass.helper_methods).to include :hidden_go_to_tag
23
27
  end
24
28
 
25
29
  it "adds a hidden field tag" do
@@ -34,7 +38,7 @@ describe GoToParam do
34
38
 
35
39
  describe "#hidden_go_to_here_tag" do
36
40
  it "becomes a helper method" do
37
- expect(FakeController.helper_methods).to include :hidden_go_to_here_tag
41
+ expect(controller_klass.helper_methods).to include :hidden_go_to_here_tag
38
42
  end
39
43
 
40
44
  it "adds a hidden field tag" do
@@ -59,7 +63,7 @@ describe GoToParam do
59
63
 
60
64
  describe "#go_to_params" do
61
65
  it "becomes a helper method" do
62
- expect(FakeController.helper_methods).to include :go_to_params
66
+ expect(controller_klass.helper_methods).to include :go_to_params
63
67
  end
64
68
 
65
69
  it "includes the go_to parameter" do
@@ -77,7 +81,7 @@ describe GoToParam do
77
81
 
78
82
  describe "#go_to_here_params" do
79
83
  it "becomes a helper method" do
80
- expect(FakeController.helper_methods).to include :go_to_here_params
84
+ expect(controller_klass.helper_methods).to include :go_to_here_params
81
85
  end
82
86
 
83
87
  it "gets the request path as the go_to parameter" do
@@ -99,11 +103,27 @@ describe GoToParam do
99
103
  expect(controller.go_to_here_params(bar: 3)).to eq({ go_to: "/example?foo&bar=3" })
100
104
  end
101
105
 
106
+ it "accepts an anchor parameter" do
107
+ controller.request = double(get?: true, fullpath: "/example")
108
+ expect(controller.go_to_here_params(foo: "foo", anchor: "bar")).to eq({ go_to: "/example?foo=foo#bar" })
109
+ end
110
+
111
+ it "makes sure the go_to path is valid UTF-8" do
112
+ weird_path = "\xE0\x80\x80weird\330stuff".force_encoding("ASCII-8BIT")
113
+ replacement = "\uFFFD" # The Unicode "Replacement Character".
114
+
115
+ controller.request = double(get?: true, fullpath: weird_path)
116
+
117
+ go_to_value = controller.go_to_here_params[:go_to]
118
+
119
+ expect(go_to_value.encoding).to eq(Encoding::UTF_8)
120
+ expect(go_to_value).to eq("#{replacement}#{replacement}#{replacement}weird#{replacement}stuff")
121
+ end
102
122
  end
103
123
 
104
124
  describe "#go_to_path" do
105
125
  it "becomes a helper method" do
106
- expect(FakeController.helper_methods).to include :go_to_path
126
+ expect(controller_klass.helper_methods).to include :go_to_path
107
127
  end
108
128
 
109
129
  it "is the go_to parameter value" do
@@ -115,11 +135,23 @@ describe GoToParam do
115
135
  controller.params = { go_to: "http://evil.com", id: "1" }
116
136
  expect(controller.go_to_path).to be_nil
117
137
  end
138
+
139
+ it "is nil when given a hash" do
140
+ controller.params = { go_to: { evil: "true" }, id: "1" }
141
+ expect(controller.go_to_path).to be_nil
142
+ end
143
+
144
+ it "respects custom allowed redirect prefixes" do
145
+ GoToParam.allow_redirect_prefix("myapp://")
146
+
147
+ controller.params = { go_to: "myapp://", id: "1" }
148
+ expect(controller.go_to_path).to eq("myapp://")
149
+ end
118
150
  end
119
151
 
120
152
  describe "#go_to_path_or" do
121
153
  it "becomes a helper method" do
122
- expect(FakeController.helper_methods).to include :go_to_path_or
154
+ expect(controller_klass.helper_methods).to include :go_to_path_or
123
155
  end
124
156
 
125
157
  it "is the go_to parameter value" do
@@ -127,7 +159,12 @@ describe GoToParam do
127
159
  expect(controller.go_to_path_or("/default")).to eq("/example")
128
160
  end
129
161
 
130
- it "is the passed-in value if the parameter value is not a relative path" do
162
+ it "falls back if the go_to param is blank" do
163
+ controller.params = { go_to: "", id: "1" }
164
+ expect(controller.go_to_path_or("/default")).to eq("/default")
165
+ end
166
+
167
+ it "falls back if the go_to param is not allowed" do
131
168
  controller.params = { go_to: "http://evil.com", id: "1" }
132
169
  expect(controller.go_to_path_or("/default")).to eq("/default")
133
170
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: go_to_param
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik N
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-20 00:00:00.000000000 Z
11
+ date: 2020-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description:
55
+ description:
56
56
  email:
57
57
  - henrik@nyh.se
58
58
  executables: []
@@ -60,6 +60,7 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
+ - CHANGELOG.md
63
64
  - Gemfile
64
65
  - README.md
65
66
  - Rakefile
@@ -71,7 +72,7 @@ homepage: ''
71
72
  licenses:
72
73
  - MIT
73
74
  metadata: {}
74
- post_install_message:
75
+ post_install_message:
75
76
  rdoc_options: []
76
77
  require_paths:
77
78
  - lib
@@ -86,9 +87,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
87
  - !ruby/object:Gem::Version
87
88
  version: '0'
88
89
  requirements: []
89
- rubyforge_project:
90
- rubygems_version: 2.2.2
91
- signing_key:
90
+ rubygems_version: 3.1.2
91
+ signing_key:
92
92
  specification_version: 4
93
93
  summary: Rails "go_to" redirection param utilities.
94
94
  test_files: