Skip to content

fix(custom-resources): ArrayBufferView decodes underlying buffer instead of the view#32336

Closed
s12v wants to merge 10 commits intoaws:mainfrom
s12v:fix-decode-uint8array
Closed

fix(custom-resources): ArrayBufferView decodes underlying buffer instead of the view#32336
s12v wants to merge 10 commits intoaws:mainfrom
s12v:fix-decode-uint8array

Conversation

@s12v
Copy link
Copy Markdown
Contributor

@s12v s12v commented Nov 29, 2024

Issue

Related to #19065
Previous PR: #30356

Reason for this change

Sample app to reproduce:

import {Stack, StackProps} from 'aws-cdk-lib';
import {Construct} from 'constructs';
import {KeySpec, KeyUsage} from "aws-cdk-lib/aws-kms";
import cr = require('aws-cdk-lib/custom-resources');
import kms = require('aws-cdk-lib/aws-kms');

export class SampleStack extends Stack {
    constructor(scope: Construct, id: string, props?: StackProps) {
        super(scope, id, props);

        // Create key
        const kmsKey = new kms.Key(this, 'SampleKey', {
            keySpec: KeySpec.ECC_NIST_P256,
            keyUsage: KeyUsage.SIGN_VERIFY,
        });

        // Export public key
        const publicKeyApiCall = new cr.AwsCustomResource(this, 'PublicKey', {
            onCreate: {
                service: 'KMS',
                action: 'GetPublicKey',
                physicalResourceId: cr.PhysicalResourceId.of('PublicKey'),
                parameters: {
                    KeyId: kmsKey.keyArn,
                },
            },
            policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
                resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
            }),
        });

        const publicKey = publicKeyApiCall.getResponseField('PublicKey');
        this.exportValue(publicKey, {name: 'PublicKey'})
    }
}

Expected result: value of the PublicKey property of the KMS GetPublicKey API response.

Actual result: the entire underlying buffer of the response:

 2024-05-27T22:03:20.837Z	d9886a79-a519-4e21-99a5-5dffc09a2fec	INFO	API response {
    CustomerMasterKeySpec: 'ECC_NIST_P256',
    KeyId: 'arn:aws:kms:us-west-2:...:key/21e7d06f-b638-400b-82f3-613cca94abe1',
    KeySpec: 'ECC_NIST_P256',
    KeyUsage: 'SIGN_VERIFY',
    PublicKey: "0Y0\x13\x06\x07*�H�=\x02\x01\x06\b*�H�=\x03\x01\x07\x03B\x00\x04\b�I�Do�BgnM>l�hl�UF��'��hj�v�l��ɖ���b�\tf�=TC���\x10\n" +
      '[\rCC_�(\x15X1b~8\x00Z\x00\x00\x00a\x00\x00\x00z\x00\x00\x00/\x00\x00\x00/\x00\x00\x00\x10|\bv�U\x00\x00سjw�U\x00\x00\x00\x00\x02~\x01t_s\x00\x00\x00\x00Erro`~\bv�U\x00\x00 �jw�U\x00\x00\x00\x00\x00\x00���\x7F\x00\x00\x00\x00���\x7F\x00\x00\x00\x00\x01ninx�jw�U\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00=\x00on bas8}\bv�U\x00\x00x�jw�U\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00`~\bv�U\x00\x00��jw�U\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00�z\bv�U\x00\x00\x03\x00\x00\x00�U\x00\x00�jw�U\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00ȳjw�U\x00\x00@�jw�U\x00\x00��jw�U\x00\x00��jw�U\x00\x00�y\bv�U\x00\x00дjw�U\x00\x00\x00\x00\x00\x00���\x7F�\x03\x04v�U\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00K���\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00�ϘV�\x7F\x00\x00\x00\x00\x00\x00�U\x00\x00X\x04\x04v�U\x00\x00X�jw�U\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00�\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00�ϘV�\x7F\x00\x00\x18�jw�U\x00\x00\x01\x00\x00\x00\x01e\x00\x00fromStri\x02\x00\x00\x00�U\x00\x00�\x05\x04v�U\x00\x00��jw�U\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00�\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00�ϘV�\x7F\x00\x00X�jw�U\x00\x00\x00\x00\x00\
...

Which causes CloudFormation to fail with error ❌ SampleStack failed: Error: The stack named SampleStack failed to deploy: UPDATE_ROLLBACK_COMPLETE: Response object is too long.

Root cause: Uint8Array references an ArrayBuffer with offset and length. When using value.buffer, the entire buffer (entire response body) is used, while Uint8Array only references a part of it.

Description of changes

Decode the value instead of the underlying buffer.

Description of how you validated changes

  • Updated a unit test
  • Added integration test integ.aws-custom-resource-kms.ts

Checklist


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

@github-actions github-actions bot added the p2 label Nov 29, 2024
@aws-cdk-automation aws-cdk-automation requested a review from a team November 29, 2024 20:58
@github-actions github-actions bot added the beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK label Nov 29, 2024
Copy link
Copy Markdown
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.

@s12v s12v changed the title Fix decode uint8array fix(custom-resources): ArrayBufferView decodes underlying buffer instead of the view Nov 29, 2024
@codecov
Copy link
Copy Markdown

codecov bot commented Nov 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.64%. Comparing base (fbaab1d) to head (30f94cc).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #32336   +/-   ##
=======================================
  Coverage   80.64%   80.64%           
=======================================
  Files         107      107           
  Lines        6994     6994           
  Branches     1290     1290           
=======================================
  Hits         5640     5640           
  Misses       1175     1175           
  Partials      179      179           
Flag Coverage Δ
suite.unit 80.64% <ø> (ø)

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

Components Coverage Δ
packages/aws-cdk 80.64% <ø> (ø)

@s12v s12v changed the title fix(custom-resources): ArrayBufferView decodes underlying buffer instead of the view fix(custom-resources): ArrayBufferView decodes underlying buffer instead of the view. Nov 29, 2024
@s12v s12v changed the title fix(custom-resources): ArrayBufferView decodes underlying buffer instead of the view. fix(custom-resources): ArrayBufferView decodes underlying buffer instead of the view Nov 29, 2024
@aws-cdk-automation
Copy link
Copy Markdown
Collaborator

This PR has been in the CHANGES REQUESTED state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week.

@s12v s12v changed the title fix(custom-resources): ArrayBufferView decodes underlying buffer instead of the view fix(custom-resources): ArrayBufferView decodes underlying buffer instead of the view Dec 27, 2024
@s12v s12v changed the title fix(custom-resources): ArrayBufferView decodes underlying buffer instead of the view fix(custom-resources): ArrayBufferView decodes underlying buffer instead of the view Dec 27, 2024
@aws-cdk-automation aws-cdk-automation dismissed their stale review December 27, 2024 10:13

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

@aws-cdk-automation
Copy link
Copy Markdown
Collaborator

AWS CodeBuild CI Report

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

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

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Dec 27, 2024
@aws-cdk-automation
Copy link
Copy Markdown
Collaborator

This PR has been in the MERGE CONFLICTS state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week.

@s12v s12v marked this pull request as draft February 9, 2025 09:03
@aws-cdk-automation
Copy link
Copy Markdown
Collaborator

This PR has been deemed to be abandoned, and will be automatically closed. Please create a new PR for these changes if you think this decision has been made in error.

@aws-cdk-automation aws-cdk-automation added the closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. label Feb 10, 2025
@github-actions
Copy link
Copy Markdown
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 10, 2025
@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Feb 10, 2025
@s12v s12v marked this pull request as ready for review February 10, 2025 08:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants