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

Hotfix: REST API multiform data missing boundary on headers #10505

Merged
merged 6 commits into from
Jul 30, 2024

Conversation

akshaysasidrn
Copy link
Collaborator

@akshaysasidrn akshaysasidrn commented Jul 29, 2024

QA Checklist

  • Integration Testing.
  • Smoke Testing.
  • Bug Fixes Verification.
  • Cross browser UI check.
  • Subpath Testing. (Automation/Smoke)
  • Reverse Proxy/Reserve Proxy with Subpath. (Smoke)
  • Automation Testing.
  • Migration testing. (including templates and import)
  • Test System Upgrade (Sanity Test System Specific. Cases)

@akshaysasidrn akshaysasidrn added run-ci CI is run only when this label is added create-review-app run-cypress Cypress E2E action labels Jul 29, 2024
Copy link

@TooljetBot
Copy link
Collaborator

TooljetBot commented Jul 29, 2024

Code Review Agent Run #1b4eb0

  • AI Based Review: ✔️ Successful

High-level Feedback

Ensure that headers are in the expected key-value pair format before processing them to avoid runtime errors. Add validation to check for key conflicts between 'queryHeaders' and 'sourceHeaders' before merging to prevent unintended overwrites or data loss. Check that the content type string is defined before calling 'toLowerCase()' to avoid runtime errors. Consider using 'Object.fromEntries' for converting header arrays to objects for better performance.

Actionable Issues

📄 plugins/packages/common/lib/utils.helper.ts
Issues: Total - 2, High importance - 2
Line 86-86 🔴 High importance - 1   
Line 96-96 🔴 High importance - 1   
📄 plugins/packages/common/lib/oauth.ts
Issues: Total - 2, High importance - 2
Line 12-12 🔴 High importance - 1   
Line 16-16 🔴 High importance - 1   

AI Code Review powered by Bito Logo

Copy link

sonarcloud bot commented Jul 29, 2024

hasDataSource = true
): { [k: string]: string } => {
const cleanHeaders = (headers) =>
headers.filter(([_, v]) => !isEmpty(v)).map(([k, v]) => [k.trim().toLowerCase(), v]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Bito Code Review Agent Run #1b4eb0 - 07/29/2024, 11:46 am

🔴 High importance
Issue: The 'cleanHeaders' function uses 'headers.filter(([_, v]) => !isEmpty(v)).map(([k, v]) => [k.trim().toLowerCase(), v]);' which assumes that headers are in the form of key-value pairs. If headers are not in this format, it will cause runtime errors.
Fix: Ensure that the headers are in the expected key-value pair format before processing them. Add a check to validate the format of headers.
Code suggestion
 @@ -85,7  85,11 @@
   const cleanHeaders = (headers) =>
 -    headers.filter(([_, v]) => !isEmpty(v)).map(([k, v]) => [k.trim().toLowerCase(), v]);
      headers
        .filter(header => Array.isArray(header) && header.length === 2 && typeof header[0] === 'string')
        .filter(([_, v]) => !isEmpty(v))
        .map(([k, v]) => [k.trim().toLowerCase(), v]);

   const _queryHeaders = cleanHeaders(queryOptions.headers || []);
   const queryHeaders = Object.fromEntries(_queryHeaders);

Is this a valid issue, or was it incorrectly flagged by the Agent?

  • it was incorrectly flagged


return headers;
return { ...queryHeaders, ...sourceHeaders };
Copy link
Collaborator

Choose a reason for hiding this comment

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

Bito Code Review Agent Run #1b4eb0 - 07/29/2024, 11:46 am

🔴 High importance
Issue: The merging of 'queryHeaders' and 'sourceHeaders' without validation or conflict resolution can lead to unintended overwrites or data loss if both objects contain the same keys.
Fix: Add validation to check for key conflicts between 'queryHeaders' and 'sourceHeaders' before merging. If conflicts are found, handle them appropriately, either by logging a warning, throwing an error, or deciding which value to keep.
Code suggestion
 @@ -92,7  96,11 @@
 -  return headers;
    const mergedHeaders = { ...queryHeaders, ...sourceHeaders };
    const conflictingKeys = Object.keys(queryHeaders).filter(key => key in sourceHeaders);
    if (conflictingKeys.length > 0) {
      console.warn('Conflicting headers found: ${conflictingKeys.join(', ')}');
    }
    return mergedHeaders;

Is this a valid issue, or was it incorrectly flagged by the Agent?

  • it was incorrectly flagged

return contentType === 'application/x-www-form-urlencoded';
export function checkIfContentTypeIsURLenc(headers: [string, string][] = []): boolean {
const contentType = headers.find(([key, _]) => key.toLowerCase() === 'content-type')?.[1];
return contentType?.toLowerCase() === 'application/x-www-form-urlencoded';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Bito Code Review Agent Run #1b4eb0 - 07/29/2024, 11:46 am

🔴 High importance
Issue: The use of 'toLowerCase()' on the content type string without checking if the string is defined could lead to runtime errors if the 'content-type' header is not present.
Fix: Add a check to ensure that the content type string is defined before calling 'toLowerCase()'.
Code suggestion
 @@ -11,3  11,4 @@
    const contentType = headers.find(([key, _]) => key.toLowerCase() === 'content-type')?.[1];
    return contentType ? contentType.toLowerCase() === 'application/x-www-form-urlencoded' : false;
 -  return contentType?.toLowerCase() === 'application/x-www-form-urlencoded';

Is this a valid issue, or was it incorrectly flagged by the Agent?

  • it was incorrectly flagged

const contentType = objectHeaders['content-type'] ?? objectHeaders['Content-Type'];
return contentType === 'multipart/form-data';
export function checkIfContentTypeIsMultipartFormData(headers: [string, string][] = []): boolean {
const contentType = headers.find(([key, _]) => key.toLowerCase() === 'content-type')?.[1];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Bito Code Review Agent Run #1b4eb0 - 07/29/2024, 11:46 am

🔴 High importance
Issue: The change from using 'Object.fromEntries' to 'Array.find' can lead to performance degradation for large header arrays. Converting the array to an object allows for O(1) access time, whereas 'Array.find' has O(n) complexity. A similar issue was also found in plugins/packages/common/lib/oauth.ts (line 11).
Fix: Consider reverting to using 'Object.fromEntries' for converting the headers array to an object for faster access times.
Code suggestion
 @@ -15,3  15,4 @@
    const objectHeaders = Object.fromEntries(headers);
    const contentType = objectHeaders['content-type'] ?? objectHeaders['Content-Type'];
    return contentType?.toLowerCase().startsWith('multipart/form-data') ?? false;
 -  const contentType = headers.find(([key, _]) => key.toLowerCase() === 'content-type')?.[1];
 -  return contentType?.toLowerCase().startsWith('multipart/form-data') ?? false;

Is this a valid issue, or was it incorrectly flagged by the Agent?

  • it was incorrectly flagged

@akshaysasidrn akshaysasidrn merged commit ce3a22f into lts-2.50 Jul 30, 2024
69 of 79 checks passed
@akshaysasidrn akshaysasidrn deleted the hotfix/restapi-multiform-data branch July 30, 2024 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
run-ci CI is run only when this label is added run-cypress Cypress E2E action
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants