Skip to content

Bug: DataMasking.decrypt returns non-string values without decrypting them #5989

Closed

Description

@dreamorosi

Expected Behavior

DataMasking.decrypt() should decrypt each selected value through the configured provider, or throw if the value is not an encrypted string. Without fields, a non-string payload should throw DataMaskingUnsupportedTypeError.

Current Behavior

In packages/data-masking/src/DataMasking.ts:196-201, a non-string value at a selected field produces a warning and returns unchanged. Objects, numbers, booleans and null do not reach provider.decrypt() at line 204. This also applies to fields selected through wildcards.

Without fields, only string inputs reach provider.decrypt() at packages/data-masking/src/DataMasking.ts:187-189. A non-string input is deep copied at line 192 and returned at line 208, without a provider call or warning.

The AWS Encryption SDK authentication and encryption-context checks run inside packages/data-masking/src/providers/kms/AWSEncryptionSDKProvider.ts:79-92. They therefore do not run for these non-string values.

Open PR #5974 makes the encryption-context check exact, but that check runs inside provider.decrypt(). Non-string values never reach it. Open PR #5975 does not change this behavior either.

Code snippet

import { DataMasking } from '@aws-lambda-powertools/data-masking';

let decryptCalls = 0;
const masker = new DataMasking({
  provider: {
    encrypt: async (value: string) => value,
    decrypt: async (_value: string) => {
      decryptCalls += 1;
      throw new Error('Dummy provider received a decrypt call');
    },
  },
});

const context = { tenantId: 'dummy-tenant' };

console.log(
  await masker.decrypt(
    { profile: { plan: 'dummy-plan' } },
    { fields: ['profile'], context }
  )
);
// { profile: { plan: 'dummy-plan' } }, with a warning

console.log(
  await masker.decrypt({ plan: 'dummy-plan' }, { context })
);
// { plan: 'dummy-plan' }, without a warning

console.log(decryptCalls);
// 0

Steps to Reproduce

  1. Install @aws-lambda-powertools/data-masking@2.36.0.
  2. Run the snippet with Node.js 24.x or 22.x.
  3. Observe that the selected object field returns unchanged with a warning.
  4. Observe that the whole object payload returns as a copy without a warning.
  5. Observe that the provider receives no decrypt calls, despite the supplied encryption context.

Possible Solution

Throw DataMaskingUnsupportedTypeError when no fields are given and the input is not a string. At each resolved field target, throw the same error for non-string values instead of warning and returning them unchanged. Keep the existing undefined passthrough, which mirrors encrypt() and cannot come from JSON input.

Update the two tests in packages/data-masking/tests/unit/encrypt-decrypt.test.ts that currently assert this behavior:

  • Line 296, returns a copy when decrypting object without fields.
  • Line 310, passes through non-string values during field-level decrypt and warns.

Both should assert rejection with DataMaskingUnsupportedTypeError. Add regression tests for objects, numbers, booleans and null at explicit fields and wildcard targets, and for non-string input without fields.

Powertools for AWS Lambda (TypeScript) version

latest (2.36.0)

AWS Lambda function runtime

24.x, 22.x

Packaging format used

npm, Lambda Layers

Execution logs

No response

Activity

  1. added theissue type on Nov 16, 2026
  2. self-assigned this
    on Nov 16, 2026
  3. added
    data-maskingThis item relates to the Data Masking Utility
    and removed
    bugSomething isn't working
    on Nov 16, 2026
  4. powertools-for-aws-oss-automation commented on Nov 16, 2026

    @powertools-for-aws-oss-automation

    Warning

    This issue is now closed. Please be mindful that future comments are hard for our team to see.
    If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
    If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

data-maskingThis item relates to the Data Masking Utilitypending-releaseThis item has been merged and will be released soon

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions