Skip to content

Event Bridge Rule and Single-Quote Escaping

0

I'm baffled, and out of ideas. I have an internal API Gateway that has a direct integration with StepFunctions. It has been working great -- until I started sending requests to it via EventBridge where the Rule is using Input Path and an Input Transformer to pair-down the event to match the (very simple) expected payload for the API.

Via the Rule, when any property value contains a single tick (') the Integration Request transform produces bad JSON (escapes the tick with ', which isn't allowed in JSON). When calling the API directly or via the "Test" UX it works even with single ticks. I've carefully compared the logged "Method request body before transform" and it appears identical in both cases (I even used a diff tool to compare, not trusting my eyes).

So what's going on here? Could it be extra HTTP headers sent by EventBridge? Byte encoding?

2 Answers
1

The issue you're experiencing with EventBridge rules and single-quote escaping in API Gateway integration requests is likely related to how EventBridge handles input transformation and JSON encoding.

When EventBridge processes events and applies input transformers, it attempts to ensure valid JSON output. This process can sometimes lead to unexpected behavior, especially when dealing with special characters like single quotes.

Here are a few things to consider:

  1. Input Transformation: EventBridge replaces input transformers at runtime to ensure valid JSON output. This process may be affecting how single quotes are handled in your event data.

  2. JSON Encoding: EventBridge automatically decodes and parses JSON-escaped or base64-encoded fields. This automatic parsing could be interfering with your single quotes.

  3. Quoting in Input Templates: When using input transformers, it's recommended to put quotes around variables that refer to JSON path parameters, but not around variables that refer to JSON objects or arrays. This might be affecting how your single quotes are processed.

To address this issue, you could try the following:

  1. Review your input transformer configuration and ensure that quotes are used correctly in your input template.

  2. If possible, try to avoid using single quotes in your data. If that's not feasible, you might need to implement additional escaping or encoding in your event data before it reaches EventBridge.

  3. Consider using a more robust JSON parsing method in your API Gateway integration request, which can handle potential escaping issues.

  4. If the issue persists, you might need to implement a custom solution, such as a Lambda function between EventBridge and API Gateway, to properly handle the JSON transformation and ensure correct escaping of special characters.

Remember that EventBridge's behavior in handling JSON and special characters can sometimes lead to unexpected results, especially when integrating with other services that have their own JSON parsing rules.
Sources
Amazon EventBridge Pipes input transformation - Amazon EventBridge
Amazon EventBridge input transformation - Amazon EventBridge

answered a year ago

EXPERT

reviewed a year ago

0
Accepted Answer

Okay, I found a solution that works but I think the problem is a legitimate one (a bug) with EventBridge. First, this is the solution I found by guessing:

$util.escapeJavaScript($input.json('$')).replaceAll("\n", "").replaceAll("\\'", "'")

Why does this solve the problem? The only answer seems to be that the payload delivered to the API Gateway endpoint includes a raw (not escaped) newline before the tick mark. That puts it right inside a string value, which is incorrect for JSON. Whoops. The first replaceAll removes the newline so that the second replaceAll can then find and replace the escaped tick mark (\') with a unescaped one ('). You note that none of the AWS documentation and examples for EventBridge include tick marks in payloads. I'd hate to think that's intentional.

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.