Skip to content

Bug: Payload validation broken for DynamoDBPersistenceLayer #2058

Closed
@kevin-secrist

Description

@kevin-secrist

Expected Behaviour

The IdempotencyHandler should throw a IdempotencyValidationError when the validation hash in DynamoDB does not match the incoming event's validation hash (specified by payloadValidationJmesPath).

Current Behaviour

It does not do that.

Code snippet

I would not expect the example code to work: https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/idempotency/#payload-validation

Steps to Reproduce

  1. Specify payloadValidationJmesPath in the IdempotencyConfig
  2. Run the same payload twice (except with a difference at the field specified at payloadValidationJmesPath

No IdempotencyValidationError will be thrown.

Possible Solution

Reverting to 1.17 fixes the issue.

The changes done in this PR #1779 does not do payload hash checking on the returned document, because that logic is short circuited when returning the existingRecord on the error.

const idempotencyRecord: IdempotencyRecord =
e.existingRecord ||
(await this.#persistenceStore.getRecord(
this.#functionPayloadToBeHashed
));

The following logic within BasePersistenceLayer.getRecord() is skipped, so the IdempotencyValidationError error is not thrown.

public async getRecord(data: JSONValue): Promise<IdempotencyRecord> {
const idempotencyKey = this.getHashedIdempotencyKey(data);
const cachedRecord = this.getFromCache(idempotencyKey);
if (cachedRecord) {
this.validatePayload(data, cachedRecord);
return cachedRecord;
}
const record = await this._getRecord(idempotencyKey);
this.saveToCache(record);
this.validatePayload(data, record);
return record;
}

Not sure how stable the interface for BaseIdempotencyLayer is but exposing validatePayload (and also saveToCache, because local caching is also being skipped) as protected and calling that on the returned record would solve the immediate problem but it doesn't seem like the most elegant solution.

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

18.x

Packaging format used

npm

Execution logs

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcompletedThis item is complete and has been merged/shippedidempotencyThis item relates to the Idempotency Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions