JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Polymorphic has_many relationships linkage data skips authorization checks added to records_for

Open
#685 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Type: Bug
Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

I'm using a simple method of authorization which is being broken by link objects for polymorphic has many relationships.

I have a ResourceBase class derived from JSONAPI::Resource, which overrides records and records_for to only return authorized records. This was working fine until I added a polymorphic has many relationship to one of my resources. At which point a number of unauthorized link objects were appearing for the resource's polymorphic relationship.

The reason for this being that JSONAPI::ResourceSerializer#foreign_key_types_and_values makes a call directly to the resources underlying model for polymorphic relationships. This call skips the records_for call in which I have been performing my authorization.

foreign_key_types_and_values is the only place in which ResourceSerializer accesses the resource's underlying model leading me to believe that there should be a method on the JSONAPI::Resource which ResourceSerializer should be calling in this case.

A possible solution would be to:

  1. Change foreign_key_types_and_values to call a method on Resource to retrieve the foreign keys and types, for both polymorphic and non-polymorphic relationships.
  2. Change Resource::_add_relationship to create a new method on the resource for each polymorphic has many relationship. This method would return both the type and id of the related models making sure to do so by calling records_for.

I'll see if I can put together a PR for this, but thought I'd mention the bug and my suggested fix first.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with JSONAPI::ResourceSerializer#foreign_key_types_and_values and Resource::_add_relationship, then trace how records_for is used for authorized relationship records. Verify that polymorphic has_many linkage data follows the resource-level authorization path and that unauthorized related records no longer produce link objects.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, authorization, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.