-
-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: false positive on unpermitted parameters #3097
Changes from 14 commits
e204d32
c6bf6da
217fbe3
ba92b18
7af1351
da92cce
4a6510d
ee791b0
d95a164
dd8c5e3
460cf46
35cb4de
2a2cce6
6a24b77
734487a
a9697dc
77e892c
cac6c3a
09a6384
09424f0
fcb0065
bc40705
8298275
227a40a
060af59
949121d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,7 +132,7 @@ def set_reflection_field | |
end | ||
|
||
def attachment_id | ||
params[:related_id] || params.require(:fields).permit(:related_id)[:related_id] | ||
params[:related_id] || params.dig(:fields, :related_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end | ||
|
||
def reflection_class | ||
|
@@ -200,7 +200,7 @@ def through_reflection? | |
end | ||
|
||
def additional_params | ||
@additional_params ||= params[:fields].permit(@attach_fields&.map(&:id)) | ||
@additional_params ||= params[:fields].slice(*@attach_fields&.map(&:id)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applying
|
||
end | ||
|
||
def set_attach_fields | ||
|
@@ -214,15 +214,12 @@ def set_attach_fields | |
|
||
def new_join_record | ||
@resource.fill_record( | ||
@reflection.through_reflection.klass.new, | ||
additional_params.merge( | ||
{ | ||
source_foreign_key => @attachment_record.id, | ||
through_foreign_key => @record.id | ||
} | ||
@reflection.through_reflection.klass.new( | ||
source_foreign_key => @attachment_record.id, | ||
through_foreign_key => @record.id | ||
), | ||
additional_params, | ||
fields: @attach_fields, | ||
extra_params: [source_foreign_key, through_foreign_key] | ||
) | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -461,8 +461,12 @@ def fill_record(record, params, extra_params: [], fields: nil) | |
|
||
# Write the user configured extra params to the record | ||
if extra_params.present? | ||
# Pick only the extra params | ||
# params at this point are already permited, only need the keys to access them | ||
extra_attributes = params.slice(*flatten_keys(extra_params)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about we |
||
|
||
# Let Rails fill in the rest of the params | ||
record.assign_attributes params.permit(extra_params) | ||
record.assign_attributes extra_attributes | ||
end | ||
|
||
record | ||
|
@@ -613,6 +617,22 @@ def entity_loader(entity) | |
def record_param | ||
@record_param ||= @record.persisted? ? @record.to_param : nil | ||
end | ||
|
||
private | ||
|
||
def flatten_keys(array) | ||
# [:fish_type, information: [:name, :history], reviews_attributes: [:body, :user_id]] | ||
# becomes | ||
# [:fish_type, :information, :reviews_attributes] | ||
array.flat_map do |item| | ||
case item | ||
when Hash | ||
item.keys | ||
else | ||
item | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Memoizing and permitting
id
.The
id
is present when an action is clicked from a show page.