Bug: sam local generate-event does not escape special characters in input
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 1.2k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 52
Description
### Description:
`sam local generate-event` does not escape special characters when interpolating values into the output.
Note that this was previously raised as #2848 but I think it was closed due to a misunderstanding. Regardless of what the body string *is* the output from `sam local generate-event` should still be valid JSON. The [comment indicating this behaviour was by design](https://github.com/aws/aws-sam-cli/issues/2848#issuecomment-933509224) seemed to confuse the issue as being with the body text being JSON.
In my reproduction below I'm not using a JSON message body.
I suspect that in general anything being inserted into an event template using `chevron` will have to be escaped.
### Steps to reproduce:
I want to generate a test SQS receive message event with the following body:
```
This message is "plain text".
It is most certainly not JSON-formatted!
```
And so I use `sam local generate-event`:
```console
$ cat body.txt
This message is "plain text".
It is most certainly not JSON-formatted!
$ sam local generate-event sqs receive-message --body "$(cat body.txt)"
{
"Records": [
{
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
"receiptHandle": "MessageReceiptHandle",
"body": "This message is "plain text".
It is most certainly not JSON-formatted!",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1523232000000",
"SenderId": "123456789012",
"ApproximateFirstReceiveTimestamp": "1523232000001"
},
"messageAttributes": {},
"md5OfBody": "786457a8a924bfb2f34d6b7fc4e2e7da",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
"awsRegion": "us-east-1"
}
]
}
```
### Observed result:
The generated JSON is invalid:
```console
$ sam local generate-event sqs receive-message --body "$(cat body.txt)" | python3 -m json.tool
Expecting ',' delimiter: line 6 column 33 (char 161)
```
To make it clear given the comments on #2848, this is nothing to do with the *body* being JSON. Irrespective of what body is used, the generated event should still be a valid JSON document.
### Expected result:
I'd expect the following JSON event:
```js
{
"Records": [
{
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
"receiptHandle": "MessageReceiptHandle",
"body": "This message is \"plain text\".\nIt is most certainly not JSON-formatted!",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1523232000000",
"SenderId": "123456789012",
"ApproximateFirstReceiveTimestamp": "1523232000001"
},
"messageAttributes": {},
"md5OfBody": "786457a8a924bfb2f34d6b7fc4e2e7da",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
"awsRegion": "us-east-1"
}
]
}
```
### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
1. OS: OS X
2. `sam --version`: SAM CLI, version 1.53.0
3. AWS region: N/A
Contributor guide
Assessment
This issue has not been assessed yet.