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(custom-resource): custom resources fail with data containing multi-byte utf8 chars #24501

Merged
merged 8 commits into from Mar 10, 2023

Conversation

mrgrain
Copy link
Contributor

@mrgrain mrgrain commented Mar 7, 2023

Custom Resources need to write their response into a S3 object.
This is implemented as a PUT request to a pre-signed URL and has to specify the content-length of the response object.
Previously the CustomResource code would use responseBody.length.
However this returns the number of graphemes, not bytes.
If any utf8 characters with graphemes != bytes are part of the response, CloudFormation would fail the deployment with a Response is not valid JSON error.

Also updates the log-retention-provider code, although the data should only contain 1-byte characters. Due to this limitation it can't be tested.

Closes #24491


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team March 7, 2023 15:40
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p1 labels Mar 7, 2023
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Mar 7, 2023
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@@ -1,13 +1,4 @@
const baseConfig = require('@aws-cdk/cdk-build-tools/config/eslintrc');
baseConfig.parserOptions.project = __dirname + '/tsconfig.json';

baseConfig.overrides = [{
// @TODO Fixing the import order will cause custom resources to updated due to a hash change
// We should apply the fix the next time `framework.ts` is meaningfully changed to avoid unnecessary resource updates
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice! Good opportunity to fix this todo 🥳

@aws-cdk-automation aws-cdk-automation dismissed their stale review March 10, 2023 11:58

✅ Updated pull request passes all PRLinter validations. Dissmissing previous PRLinter review.

@mrgrain mrgrain marked this pull request as ready for review March 10, 2023 13:29
@mrgrain mrgrain changed the title fix(custom-resource): custom resource data cannot contain utf8 characters fix(custom-resource): custom resources fail if data contains utf8 characters Mar 10, 2023
// THEN
const emptyLength = emptyDataRequest.headers?.['content-length'] as number;
const utf8Length = utf8DataRequest.headers?.['content-length'] as number;
expect(utf8Length - emptyLength).toEqual(6);
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems unnecessarily elaborate, but ok 😀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I thought about this. But if I manually counted the bytes it is just a magic number.

@mrgrain mrgrain added the pr/do-not-merge This PR should not be merged at this time. label Mar 10, 2023
@mrgrain mrgrain force-pushed the mrgrain/fix/custom-resource-utf8-response branch from f064bcc to df03857 Compare March 10, 2023 14:05
@mrgrain mrgrain changed the title fix(custom-resource): custom resources fail if data contains utf8 characters fix(custom-resource): custom resources fail with data containing utf8 characters Mar 10, 2023
@mrgrain mrgrain changed the title fix(custom-resource): custom resources fail with data containing utf8 characters fix(custom-resource): custom resources fail with data containing multi-byte utf8 chars Mar 10, 2023
@@ -54,7 +54,6 @@
"devDependencies": {
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/integ-tests": "0.0.0",
Copy link
Contributor Author

@mrgrain mrgrain Mar 10, 2023

Choose a reason for hiding this comment

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

This is to avoid a circular dependency with integ-tests. It's okay because the only usage is in the language tests which are currently run manually.

Once we have the new repo structure, this should be possible to implement in the separate integ test package.

@mrgrain mrgrain removed the pr/do-not-merge This PR should not be merged at this time. label Mar 10, 2023
@mergify
Copy link
Contributor

mergify bot commented Mar 10, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: ae2a41a
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit 9bd5078 into aws:main Mar 10, 2023
5 checks passed
@mergify
Copy link
Contributor

mergify bot commented Mar 10, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

homakk pushed a commit to homakk/aws-cdk that referenced this pull request Mar 28, 2023
…i-byte utf8 chars (aws#24501)

Custom Resources need to write their response into a S3 object.
This is implemented as a PUT request to a pre-signed URL and has to specify the `content-length` of the response object.
Previously the CustomResource code would use `responseBody.length`.
However this returns the number of graphemes, not bytes.
If any utf8 characters with `graphemes != bytes` are part of the response, CloudFormation would fail the deployment with a `Response is not valid JSON` error.

Also updates the `log-retention-provider` code, although the data should only contain 1-byte characters. Due to this limitation it can't be tested.

Closes aws#24491

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort p1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

integ-tests: Response is not valid JSON
3 participants