Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Stricter parameter handling avoiding unexpected conversion/types#63

Merged
jgraichen merged 1 commit into
mainfrom
f/stricter-params
Feb 12, 2025
Merged

Stricter parameter handling avoiding unexpected conversion/types#63
jgraichen merged 1 commit into
mainfrom
f/stricter-params

Conversation

@jgraichen
Copy link
Copy Markdown
Owner

Restify checked every parameter if it responds to #to_param, and calls that, before passing values to Addressable::Template. Since Rails patches #to_param into anything, that resulted in accepting virtually anything somehow as a parameter.

For example, any Array was encoded a slash-delimited string of the individual values ([1,2] -> "1/2"). That not only could result in confusing things accidentally passed as params, but also made it impossible to pass a parameter multiple times (a: [1, 2] -> "a=1&a=2").

This pull request takes the basic type detection from Addressable::Template and tries to only apply #to_param, which addressable does not support at all, for non-basic types. Therefore, arrays and hashes, should behave similar to when passed directly to Addressable::Template, but it will still be possible to e.g. pass an ActiveRecord model as a parameter, using #to_param.

This makes passing standard and Rails-style argument lists possible:

expand(p: [1, 2])       -> "/?p=1&p=2"
expand('p[]': [1, 2])   -> "/?p%5B%5D=1&p%5B%5D=2"

Fixes #44

@jgraichen jgraichen requested a review from franzliedke January 17, 2025 14:23
@jgraichen jgraichen self-assigned this Jan 17, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.15%. Comparing base (a33193a) to head (58aab5c).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #63      +/-   ##
==========================================
+ Coverage   95.11%   95.15%   +0.03%     
==========================================
  Files          20       20              
  Lines         635      640       +5     
==========================================
+ Hits          604      609       +5     
  Misses         31       31              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment thread lib/restify/relation.rb Outdated
Comment thread lib/restify/relation.rb Outdated
# concatenation any Array to "a/b/c". Instead, we want to check
# one level of basic types only.
if value.is_a?(Array)
return nesting ? value.map {|val| convert_param(val) } : value
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you meaning to pass nesting: false here? 🤔

What is supposed to happen on the second iteration?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it also raises. Fun.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The idea is not to reject stuff here, but only sprinkle enough #to_param to cover the most important Rails cases 😬.

Copy link
Copy Markdown
Owner Author

@jgraichen jgraichen Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be acceptable to resolve #44? 😁

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you meaning to pass nesting: false here? 🤔

Yes, fixed. 🙏

Restify checked every param if it responds to to_param, and calls it,
before passing values to Addressable::Template. Since Rails patches
to_param into anything, that resulted in accepting virtually anything
somehow into params.

For example, any Array was encoded a slash-delimited string of the
individual values ([1,2] -> "1/2"), which not only could result in
confusing things accidentially passed as params, but also made it
impossible to pass a parameter multiple times (a: [1, 2] -> "a=1&a=2").

This commit takes the basic type detection from Addressable::Template
and tries to only apply to_param, which addressable does not support at
all, for non-basic types. Therefore, arrays and hash, should behave
similar to when passed directly to Addressable::Template, but it will
still be possible to e.g. pass an ActiveRecord model as a parameter,
using #to_param.

This makes passing standard and Rails-style argument lists possible:

    expand(p: [1, 2])       -> "/?p=1&p=2"
    expand('p[]': [1, 2])   -> "/?p%5B%5D=1&p%5B%5D=2"

Fixes #44
@jgraichen jgraichen merged commit 9f583df into main Feb 12, 2025
@jgraichen jgraichen deleted the f/stricter-params branch February 12, 2025 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[2.0] Disallow non-primitive values for query parameters

2 participants