respondo 2.1.2 → 2.2.0
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/CHANGELOG.md +55 -0
- data/README.md +51 -19
- data/lib/generators/respondo/install/install_generator.rb +449 -351
- data/lib/respondo/configuration.rb +13 -0
- data/lib/respondo/controller_helpers.rb +104 -46
- data/lib/respondo/errors.rb +15 -0
- data/lib/respondo/response_builder.rb +33 -8
- data/lib/respondo/serializer.rb +47 -1
- data/lib/respondo/version.rb +1 -1
- data/lib/respondo.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d6ca76a634ae41adbeeb0a8a5fa4e7821497b38fde9eca353660ec309560871a
|
|
4
|
+
data.tar.gz: d9dd248b313227e38377e5cc378408ef5f0b8a6d9c96876accfebb3d259c9937
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5deece2436f11c11c864c9df1907cef64b2d2225c6fc3867c98fbda0fe3757fa5ee2250bfb0d55ca525e8251df752aa2b86ee070cc04b2fdeaf789b77e6ec59a
|
|
7
|
+
data.tar.gz: 6bf043bca44a2d60be789e3f7a53b7d9a142b01df51c7527e037fac657d50af3f51af383dcd77178a09eb961047715a5b9963f8ad896d02b88924b9e3ee52b3d
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,60 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [2.2.0] — Security hardening
|
|
4
|
+
|
|
5
|
+
Fixes from an independent security assessment of 2.1.3. Upgrade notes are at the bottom.
|
|
6
|
+
|
|
7
|
+
### Security
|
|
8
|
+
- **Install generator code injection (CWE-94).** Keys entered for `default_meta` were written
|
|
9
|
+
unescaped into `config/initializers/respondo.rb`, which Rails executes on boot. Keys are now
|
|
10
|
+
validated (`/\A[a-z_][a-z0-9_]*\z/i`) and emitted through a safe literal. Project name is
|
|
11
|
+
single-lined. An existing initializer is backed up to `respondo.rb.bak` instead of silently overwritten.
|
|
12
|
+
- **Excessive data exposure.** New `config.strict_serialization` raises
|
|
13
|
+
`Respondo::UnsafeSerializationError` when a raw ActiveRecord model/relation is passed without a
|
|
14
|
+
serializer; otherwise a one-time warning is logged per model class. README now leads with
|
|
15
|
+
allow-listed output.
|
|
16
|
+
- **`meta` can no longer spoof `request_id` / `timestamp`.** They are applied after all
|
|
17
|
+
caller/default meta, string-keyed meta is normalised so no duplicate keys appear.
|
|
18
|
+
- **Redirect helpers** accept `location:` and send a real `Location` header. Targets are limited
|
|
19
|
+
to relative paths / same host (`allow_other_host: true` to opt out); CR/LF, protocol-relative and
|
|
20
|
+
non-http(s) targets are rejected.
|
|
21
|
+
- **Unbounded collections.** New `config.max_collection_size` (default `nil`, i.e. unchanged) raises
|
|
22
|
+
`Respondo::CollectionTooLargeError`; relations are capped in SQL with `LIMIT max+1`.
|
|
23
|
+
- README `rescue_from` example no longer returns `e.message` to clients.
|
|
24
|
+
|
|
25
|
+
### Fixed
|
|
26
|
+
- `camelize_keys` no longer silently drops a value when two keys collapse to the same camelCase
|
|
27
|
+
name (`user_id` + `userId`); colliding keys are left unchanged.
|
|
28
|
+
- `meta: nil` no longer raises `NoMethodError` in every code-setting helper.
|
|
29
|
+
- Helpers no longer depend on ActiveSupport's `#try`, so manual inclusion outside Rails works.
|
|
30
|
+
- `render_im_a_teapot` crashed on Rack 2.x ("Unrecognized status code :im_a_teapot", found by running a real Rails 6.1 app against every helper); it now falls back to the number 418 when Rack has no symbol for it.
|
|
31
|
+
- Rack 3.1+ deprecation warnings: `render_payload_too_large` now uses `:content_too_large` when Rack knows it, and `render_not_extended` sends the number 510 (Rack deprecated the `:not_extended` symbol). Found by running the demo app on Rails 8.1.
|
|
32
|
+
- `render_unprocessable` picks `:unprocessable_content` or `:unprocessable_entity` depending on the
|
|
33
|
+
installed Rack (older Rack rejects the new name).
|
|
34
|
+
- Generator no longer uses Ruby 3-only endless method definitions.
|
|
35
|
+
|
|
36
|
+
### Removed (breaking)
|
|
37
|
+
- `render_continue`, `render_switching_protocols`, `render_processing`, `render_early_hints`.
|
|
38
|
+
1xx statuses are interim protocol responses; sending a JSON body with them can desync clients and
|
|
39
|
+
proxies. Use a normal 2xx helper, or Rails' `head`/`send_early_hints`.
|
|
40
|
+
|
|
41
|
+
### Changed (breaking)
|
|
42
|
+
- Caller-supplied `meta: { timestamp: ... }` is now ignored (the server timestamp always wins).
|
|
43
|
+
|
|
44
|
+
### Upgrading from 2.x
|
|
45
|
+
1. Replace any use of the four 1xx helpers.
|
|
46
|
+
2. If you relied on overriding `meta[:timestamp]`/`meta[:request_id]`, put your value under a different key.
|
|
47
|
+
3. Recommended: set `config.strict_serialization = true` and a `config.max_collection_size`.
|
|
48
|
+
4. Redirect helpers: pass `location:` instead of `meta: { redirect_url: ... }`.
|
|
49
|
+
|
|
50
|
+
## [2.1.3] — Docs & Dependency Updates
|
|
51
|
+
|
|
52
|
+
### Changed
|
|
53
|
+
- README updated (add a one-line summary of what you changed)
|
|
54
|
+
- Development dependencies updated to patched versions, fixing CVEs in
|
|
55
|
+
`concurrent-ruby`, `erb` and `rails-html-sanitizer`. These are test-only
|
|
56
|
+
dependencies, so there is no change to the gem's runtime behavior.
|
|
57
|
+
|
|
3
58
|
## [2.1.2] — Code Quality & Test Coverage
|
|
4
59
|
|
|
5
60
|
### Fixed
|
data/README.md
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
# Respondo 🎯
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Consistent JSON API responses for Rails, in one line.
|
|
6
6
|
|
|
7
|
+
[](https://github.com/spatelpatidar/respondo/actions/workflows/ci.yml)
|
|
7
8
|
[](https://rubygems.org/gems/respondo)
|
|
8
9
|
[](https://rubygems.org/gems/respondo)
|
|
9
10
|
[](LICENSE)
|
|
@@ -28,9 +29,9 @@ You're building an API consumed by a React frontend and a Flutter app. Three dev
|
|
|
28
29
|
{ "users": [...] }
|
|
29
30
|
```
|
|
30
31
|
|
|
31
|
-
Your frontend devs
|
|
32
|
+
Your frontend devs end up writing `if (res.data || res.result || res.users)` just to survive. The Front End team files bugs about the mismatch. Code review turns into an argument about whose format is "right."
|
|
32
33
|
|
|
33
|
-
|
|
34
|
+
Respondo fixes this: one response shape, enforced automatically across every controller. Every developer. Every time.
|
|
34
35
|
|
|
35
36
|
```json
|
|
36
37
|
{
|
|
@@ -58,7 +59,7 @@ bundle install
|
|
|
58
59
|
rails generate respondo:install
|
|
59
60
|
```
|
|
60
61
|
|
|
61
|
-
That's it. No `include` in `ApplicationController
|
|
62
|
+
That's it. No `include` in `ApplicationController`, no boilerplate — Respondo hooks in automatically via a Railtie.
|
|
62
63
|
|
|
63
64
|
---
|
|
64
65
|
|
|
@@ -87,11 +88,11 @@ class UsersController < ApplicationController
|
|
|
87
88
|
end
|
|
88
89
|
```
|
|
89
90
|
|
|
90
|
-
|
|
91
|
+
Now your frontend can count on getting the same shape back every time.
|
|
91
92
|
|
|
92
93
|
---
|
|
93
94
|
|
|
94
|
-
##
|
|
95
|
+
## What changes
|
|
95
96
|
|
|
96
97
|
| Without Respondo | With Respondo |
|
|
97
98
|
|---|---|
|
|
@@ -100,13 +101,13 @@ Your frontend now gets a **guaranteed** structure — forever.
|
|
|
100
101
|
| Pagination shape differs per endpoint | Pagination always in `meta.pagination` |
|
|
101
102
|
| Validation errors in different keys | Always in `errors`, always a hash |
|
|
102
103
|
| `render json:` boilerplate in every action | One expressive method call |
|
|
103
|
-
| camelCase conversion scattered across code | `config.camelize_keys = true
|
|
104
|
+
| camelCase conversion scattered across code | `config.camelize_keys = true`, done |
|
|
104
105
|
|
|
105
106
|
---
|
|
106
107
|
|
|
107
108
|
## What every response looks like
|
|
108
109
|
|
|
109
|
-
Every response
|
|
110
|
+
Every response, success or error, has the same four keys:
|
|
110
111
|
|
|
111
112
|
| Key | Type | Description |
|
|
112
113
|
|---|---|---|
|
|
@@ -189,12 +190,12 @@ class PostsController < ApplicationController
|
|
|
189
190
|
end
|
|
190
191
|
```
|
|
191
192
|
|
|
192
|
-
## Auto-
|
|
193
|
+
## Auto-serialization
|
|
193
194
|
|
|
194
195
|
Respondo automatically handles:
|
|
195
196
|
|
|
196
197
|
| Input type | Output |
|
|
197
|
-
|
|
198
|
+
|----------------------------------|--------------------------------------|
|
|
198
199
|
| `ActiveRecord::Base` instance | `record.as_json` |
|
|
199
200
|
| `ActiveRecord::Relation` | Array of `as_json` records |
|
|
200
201
|
| `ActiveModel::Errors` | `{ field: ["message", ...] }` |
|
|
@@ -205,6 +206,22 @@ Respondo automatically handles:
|
|
|
205
206
|
| Anything with `#to_h` | `.to_h` |
|
|
206
207
|
| Primitives (String, Integer...) | As-is |
|
|
207
208
|
|
|
209
|
+
> ⚠️ **Don't expose raw models.** `record.as_json` includes **every column** — including
|
|
210
|
+
> `password_digest`, API tokens and internal flags. Always return an explicit, allow-listed
|
|
211
|
+
> shape in production:
|
|
212
|
+
>
|
|
213
|
+
> ```ruby
|
|
214
|
+
> render_ok(data: user.as_json(only: %i[id name email]))
|
|
215
|
+
> # or configure a serializer once for the whole app:
|
|
216
|
+
> config.serializer = ->(obj) { UserBlueprint.render_as_hash(obj) }
|
|
217
|
+
> ```
|
|
218
|
+
>
|
|
219
|
+
> Set `config.strict_serialization = true` to make Respondo **raise**
|
|
220
|
+
> (`Respondo::UnsafeSerializationError`) when a raw ActiveRecord model or relation is passed
|
|
221
|
+
> without a serializer. Without it, Respondo logs a one-time warning per model class.
|
|
222
|
+
> Respondo also camelizes keys inside your `data` hashes — if two keys collapse to the same
|
|
223
|
+
> camelCase name (`user_id` / `userId`) both are left untouched rather than one overwriting the other.
|
|
224
|
+
|
|
208
225
|
---
|
|
209
226
|
|
|
210
227
|
## Configuration
|
|
@@ -225,9 +242,14 @@ Respondo.configure do |config|
|
|
|
225
242
|
config.include_request_id = true # adds request_id to every meta
|
|
226
243
|
config.camelize_keys = true # snake_case → camelCase (Flutter/JS friendly)
|
|
227
244
|
config.default_meta = { api_version: "v1" }
|
|
245
|
+
config.strict_serialization = true # raise on raw ActiveRecord data: (recommended)
|
|
246
|
+
config.max_collection_size = 1_000 # raise instead of loading huge collections (default: nil = no limit)
|
|
228
247
|
end
|
|
229
248
|
```
|
|
230
249
|
|
|
250
|
+
`request_id` and `timestamp` in `meta` are always generated by Respondo; values passed via `meta:` or
|
|
251
|
+
`default_meta` can't override them.
|
|
252
|
+
|
|
231
253
|
### camelCase output (great for Flutter / React Native)
|
|
232
254
|
|
|
233
255
|
```ruby
|
|
@@ -252,16 +274,20 @@ config.serializer = ->(obj) { MySerializer.new(obj).as_json }
|
|
|
252
274
|
|
|
253
275
|
## Global error handling (recommended pattern)
|
|
254
276
|
|
|
255
|
-
Add this to `ApplicationController` to handle exceptions app-wide without try/rescue in every action:
|
|
277
|
+
Add this to `ApplicationController` to handle exceptions app-wide without a try/rescue in every action:
|
|
256
278
|
|
|
257
279
|
```ruby
|
|
258
280
|
class ApplicationController < ActionController::API
|
|
281
|
+
# Use fixed messages: e.message contains model names and lookup keys
|
|
282
|
+
# (e.g. "Couldn't find User with 'id'=42"), which helps attackers enumerate your schema.
|
|
259
283
|
rescue_from ActiveRecord::RecordNotFound do |e|
|
|
260
|
-
|
|
284
|
+
Rails.logger.info(e.message)
|
|
285
|
+
render_not_found(message: "Resource not found")
|
|
261
286
|
end
|
|
262
287
|
|
|
263
288
|
rescue_from ActionController::ParameterMissing do |e|
|
|
264
|
-
|
|
289
|
+
Rails.logger.info(e.message)
|
|
290
|
+
render_bad_request(message: "Required parameter missing")
|
|
265
291
|
end
|
|
266
292
|
|
|
267
293
|
rescue_from StandardError do |e|
|
|
@@ -275,7 +301,7 @@ end
|
|
|
275
301
|
|
|
276
302
|
## Complete HTTP helper reference
|
|
277
303
|
|
|
278
|
-
|
|
304
|
+
There's a helper for basically every HTTP status code. Here are the ones you'll actually reach for:
|
|
279
305
|
|
|
280
306
|
### 2xx — Success
|
|
281
307
|
|
|
@@ -286,7 +312,7 @@ Respondo covers every HTTP status code. Here are the helpers you'll use every da
|
|
|
286
312
|
| `render_accepted` | 202 | Async jobs — request queued |
|
|
287
313
|
| `render_no_content` | 200* | After DELETE — no body |
|
|
288
314
|
|
|
289
|
-
> *Rails renders 200 with a JSON body for `render_no_content` to
|
|
315
|
+
> *Rails renders 200 with a JSON body for `render_no_content` to keep the response shape consistent.
|
|
290
316
|
|
|
291
317
|
```ruby
|
|
292
318
|
render_ok(data: @user, message: "Profile fetched")
|
|
@@ -327,13 +353,16 @@ render_bad_gateway(message: "Payment processor is unreachable — you have not b
|
|
|
327
353
|
<details>
|
|
328
354
|
<summary><strong>Full list of all helpers (click to expand)</strong></summary>
|
|
329
355
|
|
|
330
|
-
#### 1xx — Informational
|
|
331
|
-
`render_continue` · `render_switching_protocols` · `render_processing` · `render_early_hints`
|
|
332
|
-
|
|
333
356
|
#### 2xx — Success
|
|
334
357
|
`render_ok` · `render_created` · `render_accepted` · `render_non_authoritative` · `render_no_content` · `render_reset_content` · `render_partial_content` · `render_multi_status` · `render_already_reported` · `render_im_used`
|
|
335
358
|
|
|
336
359
|
#### 3xx — Redirect
|
|
360
|
+
Pass the target with `location:` — it is sent as a real `Location` header (and mirrored to
|
|
361
|
+
`meta.redirect_url`). Only relative paths and same-host URLs are accepted; anything else raises
|
|
362
|
+
`Respondo::UnsafeRedirectError` unless you pass `allow_other_host: true` (never do that with user input).
|
|
363
|
+
```ruby
|
|
364
|
+
render_found(location: "/login")
|
|
365
|
+
```
|
|
337
366
|
`render_multiple_choices` · `render_moved_permanently` · `render_found` · `render_see_other` · `render_not_modified` · `render_temporary_redirect` · `render_permanent_redirect`
|
|
338
367
|
|
|
339
368
|
#### 4xx — Client Error
|
|
@@ -422,5 +451,8 @@ Released under the [MIT License](LICENSE).
|
|
|
422
451
|
---
|
|
423
452
|
|
|
424
453
|
<div align="center">
|
|
425
|
-
<sub>If Respondo saved
|
|
454
|
+
<sub>If Respondo saved your an hour, give it a Star⭐.</sub>
|
|
455
|
+
</div>
|
|
456
|
+
<div align="center">
|
|
457
|
+
<sub>it helps other developers find it.</sub>
|
|
426
458
|
</div>
|