Skip to content

escapeJavaScript doesn't escape quotes #8

@jlongman

Description

@jlongman

It looks to me like the table used to do the conversion has the wrong value for the " symbol. see https://github.com/ToQoz/api-gateway-mapping-template/blob/master/index.js#L164

var escapeJavaScriptTable = {
  '"': '\"',    // 2.a
  '\\': '\\\\',

but should be

var escapeJavaScriptTable = {
  '"': '\\"',    // 2.a
  '\\': '\\\\',

note the extra backslash. I tested this in a debugger and overrode the value in the table to the doubly-escaped value and my tests passed. Additionally I sent it to apigw->dynamodb and the value was correct.


Given, in the template:
"categories": {"S": "$util.escapeJavaScript($input.json('$.categories'))"}
I expect my output to look like:
"categories":{\"pwa\":{\"id\":\"pwa\",\"title\":\"Progressive Web App\"[...]
but it looks like
"categories":{"pwa":{"id":"pwa","title":"Progressive Web App"[...]

My test looks like:

test('complete APRT VLT', () => {
  const context = {
    requestId: 'externally-specified-value',
  };
  const categories = {
    pwa: {
      id: 'pwa',
      title: 'Progressive Web App',
      score: '0.33',
    },
//.. cut
  };
  const body = {
    auditId: 'body-specified-value',
    categories,
//.. cut
  };
  const jsonBody = JSON.stringify(body);
  const result = mappingTemplate({ template: vtl, payload: jsonBody, context });
  expect(result).toBeDefined();
  expect(result).not.toEqual('');
  const json = JSON.parse(result);
  expect(json).toBeDefined();
  expect(json.categories).toBe(categories); // TODO richer test suite

Using the same template with APIGW to dynamodDB, when I push the object then do a GetItem on it I get:

{
    "Item": {
        "auditId": {
            "S": "09fe9c66-a508-447c-8878-a7de1503e88c"
        },
        "categories": {
            "S": "{\"pwa\":{\"id\":\"pwa\",\"title\":\"Progressive Web App\",\"score\":0.33}, //etc
        },

Ie the object is correctly escaped.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions