ljharb / qs

A querystring parser with nesting support
BSD 3-Clause "New" or "Revised" License
8.47k stars 731 forks source link

fix: parse url-encoded square brackets #506

Closed techouse closed 1 month ago

techouse commented 3 months ago

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.

coderabbitai[bot] commented 3 months ago

[!IMPORTANT]

Review skipped

Auto reviews are limited to specific labels.

Labels to auto review (1) * coderabbit

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

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.

[!TIP]

Early access features: enabled We are currently testing the following features in early access: - **OpenAI `gpt-4o` model for code reviews and chat**: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available. Note: - You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file. - Please join our [Discord Community](https://discord.com/invite/GsXnASn26c) to provide feedback and report issues. - OSS projects are always opted into early access features.

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 - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `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.` - `@coderabbitai help me debug CodeRabbit configuration file.` 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 an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@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](https://docs.coderabbit.ai/guides/configure-coderabbit) 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](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
techouse commented 3 months ago

@coderabbitai review

coderabbitai[bot] commented 3 months ago
Actions Performed Review triggered. > Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.
ljharb commented 3 months ago

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

techouse commented 3 months ago

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 https://github.com/ljharb/qs/blob/29dda211e8b02654f60975bdb703bcc73a8ee409/test/parse.js#L565

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 commented 3 months ago

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

techouse commented 3 months ago

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

The aforementioned test https://github.com/ljharb/qs/blob/29dda211e8b02654f60975bdb703bcc73a8ee409/test/parse.js#L565

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 https://github.com/ljharb/qs/commit/f884e2d6274c51ad8455e1339a0ad9b12bd63e06 to fix https://github.com/ljharb/qs/issues/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 commented 3 months ago

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

harjotgill commented 2 months ago

@coderabbitai configuration

coderabbitai[bot] commented 2 months ago
CodeRabbit Configuration ```yaml 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: [] ```
techouse commented 1 month ago

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

@ljharb ping 🤓