repost 0.5.1 → 0.5.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77a301cd78a8e6ce5a6b34b8acb0538701b59ca4bc351e40a8095500b0e0a735
4
- data.tar.gz: 9ee29217ffc133b3ccabe49090844757334e66904135c2aad686a7f17d1dddbe
3
+ metadata.gz: 41b967cb4d60089de6bdb34b27fa765201b3216186c7da3d0259b4ed18694ac4
4
+ data.tar.gz: a348d14926f1b1df688dd7d8bca93ad3f721f7f4259f366e6075447e48d63882
5
5
  SHA512:
6
- metadata.gz: 68eab1a118f6407a1432fbad24e8fd553a0460c3821cbb945e829fd3b7afa14da183e323d7d2b64695ae1b454600840fe862b16bb05f7702d5d213ad24b2ad9d
7
- data.tar.gz: 3e551dc5fa1045f106b7555894f13bb2cd02bc32a5ae6409df8e8096be848e8aea86b5db45d0ea9e7327ee8edf0f040b2958695c5280721d733f89d5828cc4e0
6
+ metadata.gz: fcf88d8239d00a462d962b4659f6cb29c1ed85027941a73d96b06d014e76d019745318fb2a5d535d1c52051b3ed69f86f7fa7430066d12b8eaed38a4ce11fa32
7
+ data.tar.gz: b1e9ac130d2b5e8d2ae213da35ce40a3a5b6ffe468a7b536a9390935792cbb00d15d3a414f530810578ff515139101e74976c5915b18f79f56c8a551c64fa969
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 YaroslavO
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,188 @@
1
+ <p align="right">
2
+ <a href="https://github.com/vergilet/repost"><img align="" src="https://user-images.githubusercontent.com/2478436/51829223-cb05d600-22f5-11e9-9245-bc6e82dcf028.png" width="56" height="56" /></a>
3
+ <a href="https://rubygems.org/gems/repost"><img align="right" src="https://user-images.githubusercontent.com/2478436/51829691-c55cc000-22f6-11e9-99a5-42f88a8f2a55.png" width="56" height="56" /></a>
4
+ </p>
5
+
6
+
7
+ <p align="center">
8
+ <a href="https://rubygems.org/gems/repost">
9
+ <img width="460" src="https://user-images.githubusercontent.com/2478436/55672583-44491880-58a5-11e9-945c-939f90470df8.png"></a>
10
+ </p>
11
+
12
+ Gem **Repost** implements Redirect using POST method.
13
+
14
+ Implementation story and some details in the following article [Redirect using POST in Rails](https://medium.com/@momlookhowican/redirect-using-post-in-rails-5748da354343).
15
+
16
+ [![Gem Version](https://badge.fury.io/rb/repost.svg)](https://badge.fury.io/rb/repost)
17
+ [![CI](https://github.com/vergilet/repost/actions/workflows/ci.yml/badge.svg)](https://github.com/vergilet/repost/actions/workflows/ci.yml)
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'repost'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install repost
34
+
35
+
36
+
37
+ ## What problem does it solve?
38
+
39
+ When you need to send some parameters to an endpoint which should redirect you after execution. There wouldn't be a problem if an endpoint receives [GET], because you can just use:
40
+ ```ruby
41
+ redirect_to entity_url(id: @model.id, token: model.token...)
42
+ ```
43
+
44
+
45
+ But when an endpoint receives [POST], you have to generate html form and submit it. So `repost` gem helps to avoid creation of additional view with html form, just use `redirect_post` method instead.
46
+ I faced with this problem when was dealing with bank transactions. You can see the approximate scheme:
47
+
48
+ <p align="center">
49
+ <a href="https://user-images.githubusercontent.com/2478436/55143646-d0da3500-5147-11e9-91a3-1bac9d560fb2.png">
50
+ <img src="https://user-images.githubusercontent.com/2478436/55143646-d0da3500-5147-11e9-91a3-1bac9d560fb2.png"></a>
51
+ </p>
52
+
53
+ > **P.S. The `repost` gem was initially created in response to the redirection process required by [Adyen 3D Secure 1](https://docs.adyen.com/online-payments/classic-integrations/api-integration-ecommerce/3d-secure/3d-secure-1/#step-2-redirect-to-the-card-issuer), which often involved creating an HTML form and submitting it via POST. <br>
54
+ However, with the advent of 3D Secure 2, which aims for a more integrated authentication experience, the use of such forms for POST submissions has not been encountered. 3D Secure 2 typically manages authentication data exchanges in the background, potentially eliminating the need for manual form submission.**
55
+
56
+
57
+ ## Usage
58
+
59
+ If you use Rails, gem automatically includes helper methods to your controllers:
60
+
61
+ ```ruby
62
+ repost(...)
63
+ ```
64
+ and, as an alias
65
+
66
+ ```ruby
67
+ redirect_post(...)
68
+ ```
69
+
70
+ *Under the hood it calls `render` method of current controller with `html:`.*
71
+
72
+ ### Example in Rails app:
73
+
74
+ ```ruby
75
+ class MyController < ApplicationController
76
+ ...
77
+ def index
78
+ repost(...)
79
+ end
80
+ ...
81
+ # or
82
+ def show
83
+ redirect_post(...)
84
+ end
85
+ end
86
+ ```
87
+ ______________
88
+
89
+ If you use Sinatra, Roda or etc., you need to require it first somewhere in you project:
90
+
91
+ ```ruby
92
+ require 'repost'
93
+ ```
94
+
95
+ Then ask senpai to generate a string with html:
96
+
97
+
98
+ ```ruby
99
+ Repost::Senpai.perform(...)
100
+ ```
101
+
102
+ ### Example in Sinatra, Roda, etc. app:
103
+
104
+ ```ruby
105
+ class MyController < Sinatra::Base
106
+ get '/' do
107
+ Repost::Senpai.perform(...)
108
+ end
109
+ end
110
+ ```
111
+
112
+
113
+
114
+ #### *Reminder:*
115
+
116
+ - *In Rails app use `repost` or `redirect_post` method in your controller which performs 'redirect' when it is called.*
117
+
118
+ - *In Sinatra, Roda, etc. app or if you need html output - call Senpai*
119
+
120
+
121
+ #### Full example:
122
+
123
+ *UPD: authenticity token is **turned off** by default. Use `:auto` or `'auto'` to turn on default authenticity token from Rails. Any other string value would be treated as custom auth token value.*
124
+
125
+ ```ruby
126
+ # plain ruby
127
+ # Repost::Senpai.perform('http://......)
128
+
129
+
130
+ # Rails
131
+ redirect_post('http://examp.io/endpoint', # URL, looks understandable
132
+ params: {
133
+ a: 1,
134
+ 'b': { "c": 2 },
135
+ d: [ 3, 4, 5 ],
136
+ e: { f: 'string', g: [ 6, 7, 8 ] }
137
+ }, # Your request body, also nested params and arrays
138
+ options: {
139
+ method: :post, # OPTIONAL - DEFAULT is :post. :put/:patch/:delete are sent as POST with a hidden "_method" field (Rails-style override)
140
+ status: :ok, # OPTIONAL - DEFAULT is :ok. This is the http status that the form will be returned with.
141
+ authenticity_token: 'auto', # OPTIONAL - :auto or 'auto' for Rails form_authenticity_token, string - custom token
142
+ charset: 'Windows-1251', # OPTIONAL - DEFAULT is "UTF-8", corresponds for accept-charset
143
+ form_id: 'CustomFormID', # OPTIONAL - DEFAULT is autogenerated
144
+ autosubmit: false, # OPTIONAL - DEFAULT is true, if you want to get a confirmation for redirect
145
+ autosubmit_nonce: '1d3n7i4ier', # RAILS - DEFAULT is content_security_policy_nonce, for pure Ruby - string identifier, more info - https://edgeguides.rubyonrails.org/security.html#content-security-policy
146
+ decor: { # If autosubmit is turned off or Javascript is disabled on client
147
+ section: { # ... you can decorate confirmation section and button
148
+ classes: 'red-bg red-text', # OPTIONAL - <DIV> section, set classNames, separate with space
149
+ html: '<h1>Press this button, dude!</h1>' # OPTIONAL - Any html, which will appear before submit button. Rendered as-is (NOT escaped) - never pass user input here
150
+ },
151
+ submit: {
152
+ classes: 'button-decorated round-border', # OPTIONAL - <Input> with type submit, set classNames, separate with space
153
+ text: 'c0n71nue ...' # OPTIONAL - DEFAULT is 'Continue'
154
+ }
155
+ }
156
+ }
157
+ )
158
+
159
+ ```
160
+
161
+ ### Authenticity Token (Rails)
162
+
163
+ Currently you can pass the **authenticity token** in two ways:
164
+
165
+ * Recommended:
166
+
167
+ *Use `options` and `:auto` to pass the auth token. That should protect you from any implementation changes in future Rails versions*
168
+
169
+ ```ruby
170
+ redirect_post('https://exmaple.io/endpoint', options: {authenticity_token: :auto})
171
+ ```
172
+ * Or, it is still valid to:
173
+
174
+ *use `params` and `form_authenticity_token` method directly from ActionController*
175
+ ```ruby
176
+ redirect_post('https://exmaple.io/endpoint', params: {authenticity_token: form_authenticity_token})
177
+ ```
178
+
179
+
180
+
181
+ ## License
182
+ The gem is available as open source under the terms of the MIT License.
183
+
184
+ Copyright © 2019 Yaro.
185
+
186
+ [![GitHub license](https://img.shields.io/badge/license-MIT-brightgreen)](https://raw.githubusercontent.com/vergilet/repost/master/LICENSE.txt)
187
+
188
+ **That's all folks.**
@@ -2,19 +2,23 @@ if defined?(Rails) && defined?(ActiveSupport)
2
2
  ActiveSupport.on_load(:action_controller) do
3
3
  class ::ActionController::Base
4
4
  def repost(url, params: {}, options: {})
5
+ options = options.dup
5
6
  status = options.delete(:status) || :ok
6
7
  authenticity_token = if ['auto', :auto].include?(options[:authenticity_token])
7
8
  form_authenticity_token
8
9
  else
9
10
  options[:authenticity_token]
10
11
  end
12
+ # Rails' per-request CSP nonce is the default; an explicitly passed
13
+ # :autosubmit_nonce (even nil, to opt out) takes precedence.
14
+ autosubmit_nonce = options.fetch(:autosubmit_nonce) { content_security_policy_nonce }
11
15
 
12
16
  html_payload = Repost::Senpai.perform(
13
17
  url,
14
18
  params: params,
15
19
  options: options.merge({
16
20
  authenticity_token: authenticity_token,
17
- autosubmit_nonce: content_security_policy_nonce
21
+ autosubmit_nonce: autosubmit_nonce
18
22
  }.compact)
19
23
  )
20
24
 
data/lib/repost/senpai.rb CHANGED
@@ -1,15 +1,20 @@
1
1
  require 'cgi'
2
+ require 'json'
3
+ require 'securerandom'
2
4
 
3
5
  module Repost
4
6
  class Senpai < Action
5
7
  DEFAULT_SUBMIT_BUTTON_TEXT = 'Continue'
6
8
  DEFAULT_CHARSET = 'UTF-8'
9
+ # Browsers only submit forms via GET or POST; other verbs are sent as POST
10
+ # with a Rails-style "_method" override field.
11
+ OVERRIDE_METHODS = %w[put patch delete].freeze
7
12
 
8
13
  def initialize(url, params: {}, options: {})
9
14
  @url = url
10
15
  @params = params
11
16
  @options = options
12
- @method = options.fetch(:method, :post)
17
+ @method = options.fetch(:method, :post).to_s.downcase
13
18
  @authenticity_token = options.fetch(:authenticity_token, nil)
14
19
  @charset = options.fetch(:charset, DEFAULT_CHARSET)
15
20
  @form_id = options.fetch(:form_id, generated_form_id)
@@ -41,7 +46,15 @@ module Repost
41
46
  end
42
47
 
43
48
  def form_head
44
- %Q(<form id="#{escape(form_id)}" action="#{escape(url)}" method="#{escape(method)}" accept-charset="#{escape(charset)}">)
49
+ %Q(<form id="#{escape(form_id)}" action="#{escape(url)}" method="#{escape(form_method)}" accept-charset="#{escape(charset)}">)
50
+ end
51
+
52
+ def form_method
53
+ method_override? ? 'post' : method
54
+ end
55
+
56
+ def method_override?
57
+ OVERRIDE_METHODS.include?(method)
45
58
  end
46
59
 
47
60
  def form_body
@@ -49,9 +62,14 @@ module Repost
49
62
  form_input(key, value)
50
63
  end
51
64
  inputs.unshift(csrf_token) if authenticity_token
65
+ inputs.unshift(method_override_input) if method_override?
52
66
  inputs.join
53
67
  end
54
68
 
69
+ def method_override_input
70
+ %Q(<input type="hidden" name="_method" value="#{escape(method)}">)
71
+ end
72
+
55
73
  def form_input(key, value)
56
74
  case value
57
75
  when Hash
@@ -80,21 +98,30 @@ module Repost
80
98
  end
81
99
 
82
100
  def submit_section
83
- %Q(<div class="#{escape(section_classes)}">
84
- #{section_html}
85
- <input class="#{escape(submit_classes)}" type="submit" value="#{escape(submit_text)}">
86
- </div>)
101
+ %Q(<div#{class_attr(section_classes)}>#{section_html}<input#{class_attr(submit_classes)} type="submit" value="#{escape(submit_text)}"></div>)
102
+ end
103
+
104
+ def class_attr(classes)
105
+ %Q( class="#{escape(classes)}") if classes
87
106
  end
88
107
 
89
108
  def generated_form_id
90
- "repost-#{Time.now.to_i}"
109
+ "repost-#{SecureRandom.hex(8)}"
91
110
  end
92
111
 
112
+ # Calls the prototype's submit() so a param named "submit" (which the
113
+ # browser exposes as form.submit, shadowing the method) can't break it.
93
114
  def auto_submit_script
94
115
  nonce_attr = %Q( nonce="#{escape(autosubmit_nonce)}") if autosubmit_nonce
95
116
  %Q(<script#{nonce_attr}>
96
- document.getElementById("#{escape(form_id)}").submit();
117
+ HTMLFormElement.prototype.submit.call(document.getElementById(#{js_string(form_id)}));
97
118
  </script>)
98
119
  end
120
+
121
+ # A JS string literal that is also safe inside an HTML <script> element.
122
+ # HTML escaping is wrong here: entities are not decoded inside <script>.
123
+ def js_string(value)
124
+ JSON.generate(value.to_s).gsub(/[<>&

]/) { |char| format('\u%04x', char.ord) }
125
+ end
99
126
  end
100
127
  end
@@ -1,3 +1,3 @@
1
1
  module Repost
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repost
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - YaroslavO
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-01-28 00:00:00.000000000 Z
11
+ date: 2026-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -46,6 +46,8 @@ executables: []
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
+ - LICENSE.txt
50
+ - README.md
49
51
  - Rakefile
50
52
  - lib/repost.rb
51
53
  - lib/repost/action.rb
@@ -57,6 +59,7 @@ licenses:
57
59
  - MIT
58
60
  metadata:
59
61
  homepage_uri: https://github.com/vergilet/repost
62
+ source_code_uri: https://github.com/vergilet/repost
60
63
  post_install_message:
61
64
  rdoc_options: []
62
65
  require_paths:
@@ -65,14 +68,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
68
  requirements:
66
69
  - - ">="
67
70
  - !ruby/object:Gem::Version
68
- version: '0'
71
+ version: 2.7.0
69
72
  required_rubygems_version: !ruby/object:Gem::Requirement
70
73
  requirements:
71
74
  - - ">="
72
75
  - !ruby/object:Gem::Version
73
76
  version: '0'
74
77
  requirements: []
75
- rubygems_version: 3.4.6
78
+ rubygems_version: 3.5.22
76
79
  signing_key:
77
80
  specification_version: 4
78
81
  summary: Gem implements Redirect using POST method