Giter Site home page Giter Site logo

Comments (5)

davydog187 avatar davydog187 commented on July 22, 2024

Possible solutions

  1. Handle the binary value in props_as_map/1, but I'm not sure what would be appropriate to return, if anything
  2. Catch the function clause error and send the bad request
  3. Handle up the stack somewhere?

from rabbitmq-management.

michaelklishin avatar michaelklishin commented on July 22, 2024

@davydog187 can we start with how to reproduce this instead of what the solution should be?

from rabbitmq-management.

davydog187 avatar davydog187 commented on July 22, 2024

Background

We accidentally released a regression in our application code that double encoded JSON before sending it to the rabbitmq management API. This manifested itself as a 500 in our rabbitmq deployment, rather than a client error, as I would expect.

How to reproduce

Let's start by double-encoding some JSON, then send it to the queue creation API.

iex(2)> %{auto_delete: false, durable: true} |> Jason.encode!() |> Jason.encode!()
"\"{\\\"auto_delete\\\":false,\\\"durable\\\":true}\""
PUT /api/queues/my-vhost/my-queue
"\"{\\\"auto_delete\\\":false,\\\"durable\\\":true}\""

This will decode as binary rather than a map since it is still valid JSON.

from rabbitmq-management.

michaelklishin avatar michaelklishin commented on July 22, 2024

@davydog187 the value you have used is successfully parsed by our JSON parser (JSX):

rabbit_json:decode(<<"\"{\\\"auto_delete\\\":false,\\\"durable\\\":true}\"">>).
% => <<"{\"auto_delete\":false,\"durable\":true}">>

Same result with https://jsonlint.com. I'm afraid this double encoded JSON is still valid JSON as far as the parser goes (a string literal).

if we consider this to be only possible when we get incorrectly encoded JSON, this should be handled when the value is decoded (parsed):

with_vhost_and_props(Fun, ReqData, Context) ->
    case vhost(ReqData) of
        not_found ->
            not_found(rabbit_data_coercion:to_binary("vhost_not_found"),
                      ReqData, Context);
        VHost ->
            {ok, Body, ReqData1} = read_complete_body(ReqData),
            case decode(Body) of
                {ok, Props} ->
                    try
                        Fun(VHost, props_as_map(Props), ReqData1)
                    catch {error, Error} ->
                            bad_request(Error, ReqData1, Context)
                    end;
                {error, Reason} ->
                    bad_request(rabbit_mgmt_format:escape_html_tags(Reason),
                                ReqData1, Context)
            end
    end.

from rabbitmq-management.

michaelklishin avatar michaelklishin commented on July 22, 2024

The decoding function has a clause that indicates that the input is not considered to be valid JSON:

decode(<<"">>) ->
    {ok, #{}};
decode(Body) ->
    try
        {ok, rabbit_json:decode(Body)}
    catch error:_ -> {error, not_json}
    end.

I am reluctant to base this on a type check but a new clause can be added there.

from rabbitmq-management.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.