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

Double encoding of slashes when using replace.json with response-transformer #1186

Closed
chrisbaldauf opened this issue Apr 29, 2016 · 2 comments
Assignees

Comments

@chrisbaldauf
Copy link

Overview

I'm seeing an issue where values for config.replace.json in the response-transformer plugin are showing up with doubly encoded slashes. This is an issue for me because I'm using kong with path based routing to serve swagger documents that need to have their basePath transformed to match the paths from which the APIs are being served.

Kong version: 0.7.0
Platform: Docker container

Use Case

What is being served by the underlying / backing service:

{
  "swagger": 2.0,
  "basePath": "/",
  ...
}

What I'd like kong to return by configuring the response-transformer:

{
  "swagger": 2.0,
  "basePath": "\/some\/other\/path",
  ...
}

What I'm actually seeing:

{
  "swagger": 2.0,
  "basePath": "\\\/some\\\/other\\\/path",
  ...
}

Steps to Reproduce

$ curl localhost:8001/apis -X POST -d "request_path=/test" \
  -d "upstream_url=http://mockbin.com" \
  -d "strip_request_path=true" 
{"upstream_url":"http:\/\/mockbin.com","request_path":"\/test","id":"22bc1328-1a91-4f45-b3d2-d8df73a2e87d","name":"test","created_at":1461937139000,"strip_request_path":true}

$ curl localhost:8000/test/headers -i
{
  "headers": [
    {
      "name": "host",
      "value": "mockbin.com"
    },
    ...
  ],
  "headersSize": 469
}

$ curl localhost:8001/apis/test/plugins -X POST -d "name=response-transformer" \
  -d "config.replace.json=headers:/foo/bar" 
{"api_id":"22bc1328-1a91-4f45-b3d2-d8df73a2e87d","id":"37c75e16-4a2e-427a-92d6-4b840b169148","created_at":1461937139000,"enabled":true,"name":"response-transformer","config":{"replace":{"json":["headers:\/foo\/bar"],"headers":{}},"add":{"headers":{},"json":{}},"remove":{"headers":{},"json":{}},"append":{"headers":{},"json":{}}}}

$ curl localhost:8000/test/headers 
{"headersSize":469,"headers":"\\\/foo\\\/bar"}

How I patched this

Patched from https://github.com/Mashape/kong/blob/0.7.0/kong/plugins/response-transformer/body_transformer.lua#L59

  -- replace  key:value to body
  for _, name, value in iter(conf.replace.json) do
    local v = cjson.encode(value)
    if stringy.startswith(v, "\"") and stringy.endswith(v, "\"") then
      v = v:sub(2, v:len() - 1):gsub("\\\"", "\"") -- To prevent having double encoded quotes
    end
    -- BEGIN PATCH
    -- Correct double encoding of forward slashes
    v = v:gsub("\\/", "/")
    -- END PATCH
    if json_body[name] then
      json_body[name] = v
    end
  end

I'd be curious to know if anyone else has run into this and if so how they solved it. If you can repro the issue and think the approach taken in the patch is viable, I can work on a PR for all of the config.json.* methods to make sure that forward slashes are handled appropriately.

@subnetmarco
Copy link
Member

The fix will be available in the next version.

@chrisbaldauf
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants