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

Bug: SNS envelopes assumes data is always a JSON #3266

Open
dreamorosi opened this issue Oct 28, 2024 · 1 comment
Open

Bug: SNS envelopes assumes data is always a JSON #3266

dreamorosi opened this issue Oct 28, 2024 · 1 comment
Labels
bug Something isn't working confirmed The scope is clear, ready for implementation parser This item relates to the Parser Utility

Comments

@dreamorosi
Copy link
Contributor

Expected Behavior

When processing Amazon SNS events using an AWS Lambda function customers can use a built-in envelope to quickly access the contents of the message.

The envelope should be able to process both messages that are plain strings or JSON encoded strings.

Current Behavior

Currently if the Message field is a plain text string, the envelope will throw an error because it always attempts to JSON.parse() it.

For example, this fails:

{
  "Records": [
    {
      "EventVersion": "1.0",
      "EventSubscriptionArn": "arn:aws:sns:us-east-2:123456789012:sns-la ...",
      "EventSource": "aws:sns",
      "Sns": {
        "SignatureVersion": "1",
        "Timestamp": "2019-01-02T12:45:07.000Z",
        "Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==",
        "SigningCertUrl": "https://sns.us-east-2.amazonaws.com/SimpleNotification",
        "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
        "Message": "Hello from SNS!",
        "MessageAttributes": {
          "Test": {
            "Type": "String",
            "Value": "TestString"
          },
          "TestBinary": {
            "Type": "Binary",
            "Value": "TestBinary"
          }
        },
        "Type": "Notification",
        "UnsubscribeUrl": "https://sns.us-east-2.amazonaws.com/?Action=Unsubscribe",
        "TopicArn": "arn:aws:sns:us-east-2:123456789012:sns-lambda",
        "Subject": "TestInvoke"
      }
    }
  ]
}

Code snippet

const parsedBody = SnsEnvelope.parse(event, testSchema);

Steps to Reproduce

N/A

Possible Solution

No response

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

20.x

Packaging format used

npm

Execution logs

Failed to parse envelope. This error was caused by: Unexpected token 'H', "Hello from SNS!" is not valid JSON.

@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 Oct 28, 2024
@dreamorosi dreamorosi moved this from Triage to Backlog in Powertools for AWS Lambda (TypeScript) Oct 28, 2024
@dreamorosi
Copy link
Contributor Author

When fixing this, change the it.fails() test to it() in packages/parser/tests/unit/envelopes/sns.test.ts which was introduced in #3248.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working confirmed The scope is clear, ready for implementation parser This item relates to the Parser Utility
Projects
Development

No branches or pull requests

1 participant