JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

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

Aperta
#954 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Ruby
Stelle
2.3k
Fork
546
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in lib/jsonapi/request_parser.rb dal percorso parse_params descritto nell’issue, quindi segui l’entry point update_relationship. Usa lo scenario di test del controller indicato per verificare che una relazione non consentita non cancelli l’autore esistente e che la richiesta restituisca bad_request.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
ruby
Ambito
api
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.