Skip to content

Bug: SQS Envelope assumes message body is always a JSON #3510

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

Closed
dreamorosi opened this issue Jan 22, 2025 · 2 comments · Fixed by #3513
Closed

Bug: SQS Envelope assumes message body is always a JSON #3510

dreamorosi opened this issue Jan 22, 2025 · 2 comments · Fixed by #3513
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped parser This item relates to the Parser Utility

Comments

@dreamorosi
Copy link
Contributor

Expected Behavior

When working with the SqsEnvelope I should be able to extract the message body regardless of whether it's a plain text string or a JSON-stringified one.

Current Behavior

When the message body is not a JSON-stringified object the envelope throws a runtime error as it's unable to parse it with JSON.parse().

Code snippet

import { z } from 'zod';
import { SqsEnvelope } from '@aws-lambda-powertools/parser/envelopes';

export const handler = async (event) => {
  try {
    SqsEnvelope.parse(event, z.string());
  } catch (error) {
    console.error(error);
  }
}

Steps to Reproduce

Send a message like this:

aws sqs send-message \           
  --queue-url https://sqs.eu-west-1.amazonaws.com/123456789012/sqs-parser-MyQueue \
  --message-body 'hello'

Observe the logs:

 ParseError: Failed to parse envelope. This error was caused by:
 Unexpected token 'h', "hello" is not valid JSON.

Possible Solution

We should avoid parsing by default and instead leave it to the caller to decide.

While working on this, we should also check the SQS Schema and the SNS + SQS Envelope, since they share some of the code.

Additionally, we should ensure the schema fails parsing when the Records array is empty, as mentioned in #3475.

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

22.x

Packaging format used

npm

Execution logs

@dreamorosi dreamorosi added bug Something isn't working confirmed The scope is clear, ready for implementation parser This item relates to the Parser Utility labels Jan 22, 2025
@dreamorosi dreamorosi self-assigned this Jan 22, 2025
@dreamorosi dreamorosi moved this from Triage to Working on it in Powertools for AWS Lambda (TypeScript) Jan 22, 2025
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (TypeScript) Jan 23, 2025
Copy link
Contributor

⚠️ COMMENT VISIBILITY 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 tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Jan 23, 2025
Copy link
Contributor

This is now released under v2.13.1 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Jan 28, 2025
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped parser This item relates to the Parser Utility
Projects
1 participant