Skip to content
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: parse url-encoded square brackets #506

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

techouse
Copy link

@techouse techouse commented May 19, 2024

I noticed that url-encoded square brackets, i.e. %5B and %5D, do not get correctly parsed in this case

t.test(
    'parses encoded brackets holds array of arrays when having two parts of strings with comma as array divider',
    function (st) {
        st.deepEqual(
            // foo[]=1,2,3&foo[]=4,5,6
            qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=4,5,6', { comma: true }), 
            { foo: [['1', '2', '3'], ['4', '5', '6']] }
        );
        st.deepEqual(
            // foo[]=1,2,3&foo[]=
            qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=', { comma: true }), 
            { foo: [['1', '2', '3'], ''] }
        );
        st.deepEqual(
            // foo[]=1,2,3&foo[]=
            qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=,', { comma: true }), 
            { foo: [['1', '2', '3'], ['', '']] }
        );
        st.deepEqual(
            // foo[]=1,2,3&foo[]=a
            qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=a', { comma: true }),
            { foo: [['1', '2', '3'], 'a'] }
        );
        st.end();
    }
);

This PR fixes this by replacing %5B or %5b with [ and %5D or %5d with ] before splitting the string into delimited parts inside parseQueryStringValues.

I've added the test mentioned above into into the parse test suite.

Copy link

coderabbitai bot commented May 19, 2024

Walkthrough

Walkthrough

The changes enhance the parseValues function by adding logic to decode URL-encoded square brackets, improving the handling of arrays within query parameters. Corresponding test cases were added to ensure the correct parsing of these structures, including various combinations of strings and empty values in nested arrays.

Changes

Files Change Summary
lib/parse.js Added code to replace %5B with [ and %5D with ] in cleanStr for better handling of URL-encoded arrays.
test/parse.js Introduced new test cases for parsing URL-encoded brackets with arrays of arrays, covering different string and empty value combinations.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@techouse techouse changed the title fix: parse encoded square brackets fix: parse url-encoded square brackets May 19, 2024
@techouse
Copy link
Author

@coderabbitai review

This comment was marked as outdated.

coderabbitai[bot]

This comment was marked as outdated.

@ljharb
Copy link
Owner

ljharb commented May 19, 2024

Can you elaborate a bit? Which test case in your OP is incorrect on the default branch?

@techouse
Copy link
Author

techouse commented May 19, 2024

Which test case in your OP is incorrect on the default branch?

So the test that exists and passes in v6.12.1 branch is this

t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {

However, there is no test for an encoded query, i.e. something like the test I've added

t.test('parses url-encoded brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
    st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
    st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=', { comma: true }), { foo: [['1', '2', '3'], ''] });
    st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
    st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });

    st.end();
});

This means that something like https://test.local/example?foo[]=1,2,3&foo[]=4,5,6 would parse OK, however, https://test.local/example?foo%5B%5D=1,2,3&foo%5B%5D=4,5,6 would fail. To make it work with v6.12.1 one would have to run the query through decodeURIComponent first before feeding it to qs.decode.

@ljharb
Copy link
Owner

ljharb commented May 19, 2024

Why would you be using square brackets in keys while also using commas? Brackets and commas are mutually exclusive for array encoding.

@techouse
Copy link
Author

techouse commented May 19, 2024

Why would you be using square brackets in keys while also using commas?

The aforementioned test

t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {

t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
    st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
    st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });
    st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
    st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });

    st.end();
});

predates this PR by 5 years and was added in f884e2d to fix #327. This PR just fixes it for URL-encoded query strings.

Brackets and commas are mutually exclusive for array encoding.

Sure, but this deals with decoding them, and since there's been a test for it for 5+ years, I assume it's used somewhere. 🤷

@ljharb
Copy link
Owner

ljharb commented May 20, 2024

Welp, fair enough. Thanks, I’ll give this a thorough review in the next day or three.

@harjotgill
Copy link

@coderabbitai configuration

Copy link

coderabbitai bot commented May 28, 2024

CodeRabbit Configuration
language: en-US
tone_instructions: ''
early_access: false
enable_free_tier: true
reviews:
  profile: chill
  request_changes_workflow: false
  high_level_summary: false
  high_level_summary_placeholder: '@coderabbitai summary'
  poem: false
  review_status: true
  collapse_walkthrough: true
  path_filters:
    - '!dist/**'
    - '!**/*.app'
    - '!**/*.bin'
    - '!**/*.bz2'
    - '!**/*.class'
    - '!**/*.db'
    - '!**/*.csv'
    - '!**/*.tsv'
    - '!**/*.dat'
    - '!**/*.dll'
    - '!**/*.dylib'
    - '!**/*.egg'
    - '!**/*.glif'
    - '!**/*.gz'
    - '!**/*.xz'
    - '!**/*.zip'
    - '!**/*.7z'
    - '!**/*.rar'
    - '!**/*.zst'
    - '!**/*.ico'
    - '!**/*.jar'
    - '!**/*.tar'
    - '!**/*.war'
    - '!**/*.lo'
    - '!**/*.log'
    - '!**/*.mp3'
    - '!**/*.wav'
    - '!**/*.wma'
    - '!**/*.mp4'
    - '!**/*.avi'
    - '!**/*.mkv'
    - '!**/*.wmv'
    - '!**/*.m4a'
    - '!**/*.m4v'
    - '!**/*.3gp'
    - '!**/*.3g2'
    - '!**/*.rm'
    - '!**/*.mov'
    - '!**/*.flv'
    - '!**/*.iso'
    - '!**/*.swf'
    - '!**/*.flac'
    - '!**/*.nar'
    - '!**/*.o'
    - '!**/*.ogg'
    - '!**/*.otf'
    - '!**/*.p'
    - '!**/*.pdf'
    - '!**/*.doc'
    - '!**/*.docx'
    - '!**/*.xls'
    - '!**/*.xlsx'
    - '!**/*.map'
    - '!**/*.out'
    - '!**/*.ppt'
    - '!**/*.pptx'
    - '!**/*.pkl'
    - '!**/*.pickle'
    - '!**/*.pyc'
    - '!**/*.pyd'
    - '!**/*.pyo'
    - '!**/*.pub'
    - '!**/*.pem'
    - '!**/*.rkt'
    - '!**/*.so'
    - '!**/*.ss'
    - '!**/*.eot'
    - '!**/*.exe'
    - '!**/*.pb.go'
    - '!**/*.pb.gw.go'
    - '!**/*.lock'
    - '!**/*.ttf'
    - '!**/*.yaml'
    - '!**/*.yml'
    - '!**/*.xml'
    - '!**/*.cfg'
    - '!**/*.toml'
    - '!**/*.ini'
    - '!**/*.mod'
    - '!**/*.sum'
    - '!**/*.work'
    - '!**/*.json'
    - '!**/*.mmd'
    - '!**/*.svg'
    - '!**/*.jpeg'
    - '!**/*.jpg'
    - '!**/*.png'
    - '!**/*.gif'
    - '!**/*.bmp'
    - '!**/*.tiff'
    - '!**/*.webm'
    - '!**/*.woff'
    - '!**/*.woff2'
    - '!**/*.dot'
    - '!**/*.md5sum'
    - '!**/*.wasm'
    - '!**/*.snap'
    - '!**/*.parquet'
    - '!**/gen/**'
    - '!**/_gen/**'
    - '!**/generated/**'
    - '!**/@generated/**'
    - '!**/vendor/**'
    - '!**/*.min.js'
    - '!**/*.min.js.map'
    - '!**/*.min.js.css'
    - '!**/*.tfstate'
    - '!**/*.tfstate.backup'
    - ''
  path_instructions: []
  auto_review:
    enabled: true
    auto_incremental_review: true
    ignore_title_keywords: []
    labels:
      - coderabbit
    drafts: true
    base_branches: []
  tools:
    shellcheck:
      enabled: true
    ruff:
      enabled: true
    markdownlint:
      enabled: true
    github-checks:
      enabled: true
      timeout_ms: 90000
    languagetool:
      enabled: true
      disabled_rules:
        - EN_UNPAIRED_BRACKETS
      disabled_categories:
        - TYPOS
        - TYPOGRAPHY
        - CASING
      enabled_only: false
      level: default
    biome:
      enabled: true
    hadolint:
      enabled: true
chat:
  auto_reply: true
knowledge_base:
  learnings:
    scope: auto
  issues:
    scope: auto
  jira:
    project_keys: []
  linear:
    team_keys: []

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants