JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Disabling JSONAPI.configuration.raise_if_parameters_not_allowed allows you to remove forbidden relationships

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

Nobody has claimed this yet.

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

Description

If raise_if_parameters_not_allowed is set to false, then something strange happens.

In https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/request_parser.rb#L237 the params are first filtered using parse_params. Suppose I have a model with an author relationship (like in the tests) that is optional (so presence is not validated) but remove the author from updatable_fields:

class Post < ActiveRecord::Base
  belongs_to :author, optional: true
end

class PostResource < JSONAPI::Resource
  has_one :author

  def self.updatable_fields(context)
    super - [:author]
  end
end

If I send an update_relationship request to the PostResource, then any relationship data I send to the PostResource gets set to nil. The post's author is set to nil and, since that is a valid value, the update is saved to the database. In other words, the following controller test would fail:

  def test_update_relationship_to_one_forbidden_relationship_filtered_parameter
    JSONAPI.configuration.raise_if_parameters_not_allowed = false

    refute_nil Post.find(1).author

    set_content_type_header!
    put :update_relationship, params: {post_id: 1, relationship: 'author', data: {anything: "really"}}

    assert_response :bad_request
    refute_nil Post.find(1).author
  ensure
    JSONAPI.configuration.raise_if_parameters_not_allowed = true
  end

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 in lib/jsonapi/request_parser.rb at the parse_params path described in the issue, then trace the update_relationship entry point. Use the named controller test scenario to verify that a forbidden relationship does not clear the existing author and that the request returns bad_request.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.