jpie 4.2.1.pre.202611250311 → 4.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38377aa6c2708d5f9921583458a686ed0eeead5d052d32309ed28e03b82a82b8
4
- data.tar.gz: c78fe5c29bbe9d030ea3d56c3abefbbe2dc474601e87fd60326b529131eebcd6
3
+ metadata.gz: 68b78d7b1a591c1ea4d42aa885e7ba37e1bf494e96a6c214bb8d8a99e3395769
4
+ data.tar.gz: 84b4a609454016f97cc9a9dac08de5d8ff674e7b7dd6799df51aa599dd6bfae0
5
5
  SHA512:
6
- metadata.gz: 0b1c1cdb8455a8445bd1f92491e9dd550376424d9d5252f0e8322b007b4a6a47fd12e46a3b300cba759753791157a534b552fdf3fa22ef3075dd0788173fcbb1
7
- data.tar.gz: '00869410a96c68e0a6ccee48927d23c1e60ba1181f5568d0cd474869c9ebc024e5a17c3ccf24a6ec3d2e42c2d552a35b714fb904bcac9d4ce7c6632a3e630bf3'
6
+ metadata.gz: 10decee75897b487367f1b0996d6f6bb7e179451ddc9a4724d1531a1c0364119fb5d223d0aef1614c6d446a71d78bbde92a4158086332290ca714b9c9fe2744a
7
+ data.tar.gz: 5ccbf2551f2b85d128b9b5e199f41b275eb610af785a18a4a433003241397ce13e8b9df875a4087ba5c0e6c1dc3bada24017419e5d2189e12ca5693748a69330
data/CHANGELOG.md CHANGED
@@ -7,6 +7,48 @@ at release time.
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [4.4.0] - 2026-11-25
11
+
12
+ ### Changed
13
+
14
+ - **Refuse a resource request whose `data` or `relationships` is no object.**
15
+ A POST, PATCH or DELETE of one resource now answers 400
16
+ (`parameter_not_allowed`) when `data` is a list or a scalar. It also answers
17
+ 400 when `relationships` is not an object. The detail is "data must be a JSON
18
+ object" or "relationships must be a JSON object". The check runs before the
19
+ other resource callbacks of jpie. A host callback that read such a body with
20
+ `raw_jsonapi_data`, or dug into `relationships`, raised, so the request
21
+ answered 500. A DELETE with such a body ran before; it now answers 400 and
22
+ deletes nothing. A DELETE with no `data`, or with an object body, behaves as
23
+ before. `/<type>/<id>/relationships/<name>` still takes a list `data`. A route
24
+ that `jsonapi_resources` did not draw is not checked.
25
+ **Upgrade:** a client that sends a list body on a resource DELETE gets 400.
26
+ Regression spec: `non_object_resource_data_spec.rb`.
27
+
28
+ ### Fixed
29
+
30
+ - **Read `relationship_name` from the route only.** A `?relationship_name=`
31
+ query parameter on a resource PATCH or POST skipped the type check. So a
32
+ PATCH of `/posts/1` with `type: "users"` saved the post. It now answers 409.
33
+
34
+ ## [4.3.0] - 2026-11-25
35
+
36
+ ### Fixed
37
+
38
+ - **Attach only a blob the caller may show.** A write body names a blob by its
39
+ signed id, under `relationships` or as a bare string under `attributes`. jpie
40
+ attached it with no authorization check, so a caller who held another
41
+ tenant's signed id attached that tenant's file and read it back. jpie now
42
+ resolves each named blob and calls `authorization_handler` with
43
+ `action: :show` on it before it writes the record. A refusal answers 403 and
44
+ writes nothing. An invalid signed id under `attributes` now answers 400
45
+ before an update writes the record, as it did under `relationships`. A value
46
+ that is neither a signed id nor a blob also answers 400 before the write.
47
+ Before, an update saved the record and purged the old file, then failed.
48
+ **Upgrade:** a handler that refuses `show` on an `ActiveStorage::Blob` now
49
+ also refuses the attach. Regression spec:
50
+ `attachment_blob_authorization_spec.rb`.
51
+
10
52
  ## [4.2.0] - 2026-11-24
11
53
 
12
54
  ### Changed
data/README.md CHANGED
@@ -1031,6 +1031,8 @@ The gem automatically renders `403 Forbidden` for `JSONAPI::AuthorizationError`
1031
1031
 
1032
1032
  Relationship endpoints authorize the parent resource with `action: :update` and `context: { relationship: :relationship_name }`.
1033
1033
 
1034
+ A create or update body that names an ActiveStorage blob, under `relationships` or as a signed id under `attributes`, authorizes each blob with `action: :show` before it writes the record. A caller attaches only a file it may read.
1035
+
1034
1036
  ## Instrumentation (Rails 8.1+)
1035
1037
 
1036
1038
  When running on Rails 8.1 or later, the gem automatically emits structured events via `Rails.event` for all CRUD and relationship operations. This enables seamless integration with monitoring and APM platforms like Datadog, AppSignal, New Relic, or Honeycomb.
@@ -1273,6 +1275,7 @@ The gem validates relationship data:
1273
1275
  - Invalid relationship type (for non-polymorphic associations) returns `400 Bad Request`
1274
1276
  - Invalid polymorphic type (class doesn't exist) returns `400 Bad Request`
1275
1277
  - Attempting to unset linkage that cannot be nullified (e.g., foreign key has NOT NULL constraint) returns `400 Bad Request`
1278
+ - A POST, PATCH or DELETE of one resource whose `data` is not an object, or whose `relationships` is not an object, returns `400 Bad Request` before any other callback reads the body. A relationship endpoint still takes a list `data`.
1276
1279
 
1277
1280
  ## Relationship Endpoint Details
1278
1281
 
@@ -19,7 +19,27 @@ module JSONAPI
19
19
  hash = deserialize_params(:update, definition: @resource_class)
20
20
  attachments = extract_active_storage_params_from_hash(hash, model_class)
21
21
  clean_params(hash, attachments)
22
- [hash, attachments]
22
+ [hash, authorize_attachment_blobs(attachments)]
23
+ end
24
+
25
+ # A client names a blob by its signed id, under `relationships` or as a bare string under
26
+ # `attributes`. The signature proves only that this application made the id, not that the
27
+ # caller may read the file. Resolve each named blob and ask the authorization handler to
28
+ # `show` it before the record is written, so a caller attaches only a file it may read.
29
+ # Any other value names no file to judge, so refuse it before the write too.
30
+ def authorize_attachment_blobs(attachments)
31
+ attachments.transform_values { authorize_attachment_value(it) }
32
+ end
33
+
34
+ def authorize_attachment_value(value)
35
+ return value.map { authorize_attachment_value(it) } if value.is_a?(Array)
36
+ return value if value.blank?
37
+
38
+ blob = value.is_a?(String) ? find_blob_by_signed_id(value) : value
39
+ raise ArgumentError, "Expected a signed blob ID, got #{value.class}" unless blob.is_a?(::ActiveStorage::Blob)
40
+
41
+ authorize_resource_action!(blob, action: :show)
42
+ blob
23
43
  end
24
44
 
25
45
  def clean_params(hash, attachments)
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONAPI
4
+ module ResourceActions
5
+ # A resource request carries one resource object in `data`, and the object carries its
6
+ # relationships as an object. The check runs before the other resource callbacks of jpie.
7
+ # So no later callback of jpie or of the host reads a list or a scalar as an object.
8
+ # A relationship endpoint takes a list `data` by design and keeps its own checks.
9
+ # A route that names no resource type is not a jpie resource route, so the check skips it.
10
+ # The check reads both route members from the path, because a query string can set them too.
11
+ module DocumentValidation
12
+ extend ActiveSupport::Concern
13
+
14
+ def validate_resource_document!
15
+ return if request.path_parameters[:resource_type].blank?
16
+ return if request.path_parameters[:relationship_name].present?
17
+
18
+ data = params[:data]
19
+ return if data.nil?
20
+
21
+ member = non_object_member(data)
22
+ return unless member
23
+
24
+ error = JSONAPI::Errors::ParameterNotAllowed.new(["#{member} must be a JSON object"])
25
+ render_parameter_not_allowed_error(error)
26
+ end
27
+
28
+ private
29
+
30
+ def non_object_member(data)
31
+ return "data" unless data.is_a?(ActionController::Parameters)
32
+
33
+ relationships = data[:relationships]
34
+ "relationships" unless relationships.nil? || relationships.is_a?(ActionController::Parameters)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -7,7 +7,8 @@ module JSONAPI
7
7
 
8
8
  def validate_resource_type!
9
9
  return unless @resource_class
10
- return if params[:relationship_name].present?
10
+ # Read the route, not params: a query string can set relationship_name too.
11
+ return if request.path_parameters[:relationship_name].present?
11
12
 
12
13
  requested = jsonapi_type
13
14
  expected = resource_type
@@ -8,6 +8,7 @@ require_relative "resource_actions/include_preloading"
8
8
  require_relative "resource_actions/serialization"
9
9
  require_relative "resource_actions/pagination"
10
10
  require_relative "resource_actions/type_validation"
11
+ require_relative "resource_actions/document_validation"
11
12
  require_relative "resource_actions/crud_helpers"
12
13
  require_relative "resource_actions/resource_loading"
13
14
 
@@ -21,10 +22,13 @@ module JSONAPI
21
22
  include Serialization
22
23
  include Pagination
23
24
  include TypeValidation
25
+ include DocumentValidation
24
26
  include CrudHelpers
25
27
  include ResourceLoading
26
28
 
27
29
  included do
30
+ # First, so no later callback reads a body that holds no resource object.
31
+ before_action :validate_resource_document!, only: %i[create update destroy]
28
32
  before_action :load_jsonapi_resource_class
29
33
  # A query parameter shapes the query the loader builds, so judge every one of them
30
34
  # before the loader reads the database. The body members stay after the load,
@@ -58,7 +62,8 @@ module JSONAPI
58
62
  def create
59
63
  @resource = build_resource_for_create
60
64
  authorize_resource_action!(@resource, action: :create)
61
- attach_active_storage_files(@resource, @create_attachments, resource_class: determine_sti_resource_class)
65
+ attachments = authorize_attachment_blobs(@create_attachments)
66
+ attach_active_storage_files(@resource, attachments, resource_class: determine_sti_resource_class)
62
67
  save_created(@resource)
63
68
  rescue ArgumentError => e
64
69
  render_create_error(e)
@@ -19,6 +19,7 @@ module JSONAPI
19
19
 
20
20
  skip_before_action :validate_resource_type!, only: %i[update destroy]
21
21
  skip_before_action :validate_resource_id!, only: %i[update destroy]
22
+ skip_before_action :validate_resource_document!, only: %i[update destroy]
22
23
 
23
24
  # A polymorphic or namespaced relationship can only learn at serialization
24
25
  # that the request namespace carries no resource for the record's class.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSONAPI
4
- VERSION = "4.2.1.pre.202611250311"
4
+ VERSION = "4.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jpie
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.1.pre.202611250311
4
+ version: 4.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Kampp
@@ -117,6 +117,7 @@ files:
117
117
  - lib/json_api/controllers/concerns/relationships/updating.rb
118
118
  - lib/json_api/controllers/concerns/resource_actions.rb
119
119
  - lib/json_api/controllers/concerns/resource_actions/crud_helpers.rb
120
+ - lib/json_api/controllers/concerns/resource_actions/document_validation.rb
120
121
  - lib/json_api/controllers/concerns/resource_actions/field_validation.rb
121
122
  - lib/json_api/controllers/concerns/resource_actions/filter_validation.rb
122
123
  - lib/json_api/controllers/concerns/resource_actions/include_preloading.rb