jsonrpc-rails 0.2.0 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c7eaabe38b79dd41bac2752a456d5278e6cc780c3d2ebbe0493c349ff24ee10
4
- data.tar.gz: 51797ce9c50cd5e51b248f8a4376880f7ffcf19245daa8ae2ef1a4b9d5d17cdf
3
+ metadata.gz: 145a4a20db9c6d49fa01ca8b9eb0c1a853288195a78c099b4ecc9eefc4f551d8
4
+ data.tar.gz: c5ac9e090225a393917f0cda10c6d8a4d0593b428659d596539c1999edc3edcb
5
5
  SHA512:
6
- metadata.gz: 3a55b1b369ff4d277b3351c6632a858bd00244cd0799108fbeaf4e64ba2e5b39c4687be0f1855e96df90343541e8ac0ab137684cad4f7af7394a75612b367395
7
- data.tar.gz: 9883f5c131ab3437cde5410f82a21f072824a045e1680ad27d81ef1f4d10d471149a7db639c18d8e2d194586979919224c4e921bae263236023d71934d2a6cc5
6
+ metadata.gz: a5d77db449217fddce82825a2daac7019d9c2133759dfd23cf01c1eb10fb4ec9d91b24ceca98fadb244b011a6becb15fe40256ab5d318cc4e65a5d23b2842247
7
+ data.tar.gz: fdcf40b192d2f132d577c47195b007e67172e574f99936c3d74f2c30d31e7cc956a6dabba74ef8362777deaec2fb3f4a974736aa14991fd9e33ccba2bd28faf3
data/README.md CHANGED
@@ -38,6 +38,22 @@ gem install jsonrpc-rails
38
38
  ### Rendering Responses
39
39
 
40
40
  Once installed, **jsonrpc-rails** registers a custom renderer with Rails.
41
+
42
+ Enable validation where you need it
43
+
44
+ Add this to config/application.rb (or an environment file):
45
+ ```ruby
46
+ # Validate only the JSON‑RPC endpoints you expose
47
+ config.jsonrpc_rails.validated_paths = [
48
+ "/rpc", # exact string
49
+ %r{\A/api/v\d+/rpc\z}, # regexp
50
+ ->(p) { p.start_with? "/rpc/private" } # lambda / proc
51
+ ]
52
+ ```
53
+
54
+ Leave the array empty (default) and the middleware is effectively off.
55
+ Use [/.*\z/] if you really want it on everywhere.
56
+
41
57
  In your controllers, you can render JSON-RPC responses like so:
42
58
 
43
59
  ```ruby
@@ -20,11 +20,15 @@ module JSONRPC_Rails
20
20
  CONTENT_TYPE = "application/json"
21
21
  ENV_PAYLOAD_KEY = :"jsonrpc.payload"
22
22
 
23
- def initialize(app)
23
+ def initialize(app, paths = nil)
24
24
  @app = app
25
+
26
+ @paths = Array(paths || Rails.configuration.jsonrpc_rails.validated_paths)
25
27
  end
26
28
 
27
29
  def call(env)
30
+ return @app.call(env) unless validate_path?(env["PATH_INFO"])
31
+
28
32
  # Only process POST requests with the correct Content-Type
29
33
  return @app.call(env) unless env["REQUEST_METHOD"] == "POST" &&
30
34
  env["CONTENT_TYPE"]&.start_with?(CONTENT_TYPE)
@@ -87,6 +91,19 @@ module JSONRPC_Rails
87
91
  nil
88
92
  end
89
93
 
94
+ def validate_path?(path)
95
+ return false if @paths.empty?
96
+
97
+ @paths.any? do |m|
98
+ case m
99
+ when String then path == m
100
+ when Regexp then m.match?(path)
101
+ when Proc then m.call(path)
102
+ else false
103
+ end
104
+ end
105
+ end
106
+
90
107
  # Performs strict validation on a single object to ensure it conforms
91
108
  # to the JSON-RPC 2.0 structure (jsonrpc, method, params, id) and
92
109
  # has no extraneous keys.
@@ -3,8 +3,9 @@ require_relative "middleware/validator"
3
3
  module JSONRPC_Rails
4
4
  # Use Rails::Railtie to integrate with the Rails application
5
5
  class Railtie < Rails::Railtie
6
+ config.jsonrpc_rails = ActiveSupport::OrderedOptions.new
7
+ config.jsonrpc_rails.validated_paths = [] # By default, we inject it into the void.
6
8
  # Insert the JSON-RPC Validator middleware early in the stack.
7
- # Inserting before Rack::Sendfile, which is typically present early in the stack.
8
9
  initializer "jsonrpc-rails.add_validator_middleware" do |app|
9
10
  app.middleware.use JSONRPC_Rails::Middleware::Validator
10
11
  end
@@ -1,3 +1,3 @@
1
1
  module JSONRPC_Rails
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonrpc-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih