Skip to content

Conversation

@MarcosSpessatto
Copy link
Contributor

@MarcosSpessatto MarcosSpessatto commented Apr 16, 2025

This pull request addresses a regression issue related to the parsing of application/x-www-form-urlencoded request bodies. The changes focus on refining the handling of incoming webhook requests and improving error management in the parseBodyParams method.

Key changes include:

  • In apps/meteor/app/api/server/router.ts, the error handling within the parseBodyParams method has been updated. Now, when an error occurs during parsing, the method returns an empty object instead of the overrideBodyParams.
  • In apps/meteor/app/integrations/server/api/api.js, the construction of request URLs for incoming webhooks has been adjusted. Additionally, the logic for parsing application/x-www-form-urlencoded request bodies has been refined, especially for those containing a JSON payload within a form field.

These updates aim to enhance the robustness of the URL-encoded body parsing process, ensuring that errors are handled gracefully and that the request data is accurately interpreted.

@dionisio-bot
Copy link
Contributor

dionisio-bot bot commented Apr 16, 2025

Looks like this PR is ready to merge! 🎉
If you have any trouble, please check the PR guidelines

@MarcosSpessatto MarcosSpessatto added this to the 7.6.0 milestone Apr 16, 2025
@kody-ai
Copy link

kody-ai bot commented Apr 16, 2025

Code Review Completed! 🔥

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization
Breaking Changes

Access your configuration settings here.

@changeset-bot
Copy link

changeset-bot bot commented Apr 16, 2025

⚠️ No Changeset found

Latest commit: b4740fd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@MarcosSpessatto MarcosSpessatto changed the title regression: parse urlencoded body as string when there is no specific… regression: parse urlencoded body as string when there is no specific key(payload) Apr 16, 2025
msg: 'Body received as application/x-www-form-urlencoded without the "payload" key, parsed as string',
content,
});
c.set('bodyParams-override', JSON.parse(content));
Copy link

Choose a reason for hiding this comment

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

kody code-review Potential Issues high

try {
    const singleKey = Object.keys(body)[0];
    const valueToParse = body[singleKey];
    c.set('bodyParams-override', JSON.parse(valueToParse));
} catch (parseError) {
    incomingLogger.error({ msg: 'Failed to parse form field value as JSON', key: Object.keys(body)[0], error: parseError });
    c.body(JSON.stringify({ success: false, error: 'Invalid JSON in form field value' }), 400);
    return; // Stop processing here
}

Parsing the raw form content (content) as JSON is likely incorrect, as it should be the value of the single form field that is parsed instead.

This issue appears in multiple locations:

  • apps/meteor/app/integrations/server/api/api.js: Lines 343-343
  • apps/meteor/app/integrations/server/api/api.js: Lines 343-343
    Please modify the code to parse the value of the single form field as JSON instead of the raw form content to avoid potential parsing errors.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

const content_raw = Buffer.concat(buffers).toString('utf8');
const protocol = `${this.request.headers.get('x-forwarded-proto')}:` || 'http:';
const url = new URL(this.request.url, `${protocol}//${this.request.headers.host}`);
const url = new URL(this.request.url, `${protocol}//${this.request.headers.get('host')}`);
Copy link

Choose a reason for hiding this comment

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

kody code-review Error Handling high

const host = this.request.headers.get('host');
if (!host) {
    // Option 1: Throw an error if host is required
    throw new Error('Host header is missing');
    // Option 2: Handle appropriately if a fallback isn't possible
    // return API.v1.failure('Missing Host header'); 
}
const url = new URL(this.request.url, `${protocol}//${host}`);

The this.request.headers.get('host') call might return null, leading to potential errors when constructing a URL with an invalid host part.

This issue appears in multiple locations:

  • apps/meteor/app/integrations/server/api/api.js: Lines 97-97
  • apps/meteor/app/integrations/server/api/api.js: Lines 97-97
    Please add validation for the 'host' header to ensure it is present and handle cases where it is missing to prevent errors in URL construction.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

Comment on lines +336 to +343
c.set('bodyParams-override', JSON.parse(body.payload));
return next();
}
incomingLogger.debug({
msg: 'Body received as application/x-www-form-urlencoded without the "payload" key, parsed as string',
content,
});
c.set('bodyParams-override', JSON.parse(content));
Copy link

Choose a reason for hiding this comment

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

kody code-review Error Handling medium

try {
    const parsed = JSON.parse(body.payload);
    c.set('bodyParams-override', parsed);
    return next();
} catch (e) {
    incomingLogger.error({ msg: 'Failed to parse payload as JSON', error: e });
    c.body(JSON.stringify({ success: false, error: 'Invalid JSON payload' }), 400);
    return;
}

The JSON.parse calls are within an outer try...catch block, which may lead to generic error handling and less granular error logging.

This issue appears in multiple locations:

  • apps/meteor/app/integrations/server/api/api.js: Lines 336-343
  • apps/meteor/app/integrations/server/api/api.js: Lines 336-343
    Please add specific try...catch blocks around each JSON.parse call to enable more granular error handling and logging.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

@codecov
Copy link

codecov bot commented Apr 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.16%. Comparing base (fe5f8a2) to head (b4740fd).
Report is 1 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop   #35823   +/-   ##
========================================
  Coverage    61.16%   61.16%           
========================================
  Files         2971     2971           
  Lines        70839    70839           
  Branches     16185    16185           
========================================
+ Hits         43328    43330    +2     
+ Misses       24562    24560    -2     
  Partials      2949     2949           
Flag Coverage Δ
e2e 57.68% <ø> (+<0.01%) ⬆️
unit 75.61% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kody-ai
Copy link

kody-ai bot commented Apr 16, 2025

Code Review Completed! 🔥

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization
Breaking Changes

Access your configuration settings here.

@kody-ai
Copy link

kody-ai bot commented Apr 17, 2025

Code Review Completed! 🔥

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization
Breaking Changes

Access your configuration settings here.

@github-actions
Copy link
Contributor

PR Preview Action v1.6.1

🚀 View preview at
https://RocketChat.github.io/Rocket.Chat/pr-preview/pr-35823/

Built to branch gh-pages at 2025-04-17 11:27 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@MarcosSpessatto MarcosSpessatto marked this pull request as ready for review April 17, 2025 12:13
@MarcosSpessatto MarcosSpessatto requested a review from a team as a code owner April 17, 2025 12:13
@ggazzo ggazzo added the stat: QA assured Means it has been tested and approved by a company insider label Apr 17, 2025
@dionisio-bot dionisio-bot bot added the stat: ready to merge PR tested and approved waiting for merge label Apr 17, 2025
@ggazzo ggazzo merged commit 72fbdac into develop Apr 17, 2025
48 of 50 checks passed
@ggazzo ggazzo deleted the regression/hono-parse-urlencoded branch April 17, 2025 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stat: QA assured Means it has been tested and approved by a company insider stat: ready to merge PR tested and approved waiting for merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants