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 +4 -4
- data/README.md +16 -0
- data/lib/jsonrpc_rails/middleware/validator.rb +18 -1
- data/lib/jsonrpc_rails/railtie.rb +2 -1
- data/lib/jsonrpc_rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 145a4a20db9c6d49fa01ca8b9eb0c1a853288195a78c099b4ecc9eefc4f551d8
|
4
|
+
data.tar.gz: c5ac9e090225a393917f0cda10c6d8a4d0593b428659d596539c1999edc3edcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|