juniper icon indicating copy to clipboard operation
juniper copied to clipboard

Literal Scalar Parsing should not panic

Open dreamcodez opened this issue 7 years ago • 14 comments

Describe the bug Literal Scalar Parsing should not panic. Sending badly shaped data is a thing that Denial-Of-Service attackers like to do -- and being able to panic the thread this easy is a smell in my opinion.

To Reproduce Steps to reproduce the behavior:

  1. Define a schema with a scalar String, Int, or Float type
  2. Query the schema with query literals which violate the contract in step (1)
  3. Notice that the thread panics
  4. Code reference here: https://github.com/graphql-rust/juniper/blob/master/juniper/src/parser/value.rs ... look for parse_scalar_literal_by_infered_type and then look for the panics.

Expected behavior The framework should handle this class of error gracefully without a panic. It should return in this case a proper 400 Bad Request HTTP Response with the standard graphql json error reporting array format: { "errors": [ "... must be an Int" ] }

Additional context Violating the api shape is one of the easiest things a would-be denial-of-service attacker can try to attempt to crash your service and/or waste cpu cycles/compute resources/network resources.

dreamcodez avatar Sep 21 '19 01:09 dreamcodez

We should probably add this case to ParseError and bubble it up.

LegNeato avatar Sep 30 '19 00:09 LegNeato

@dreamcodez I am trying to reproduce panic here but I cannot. Could you provide more details?

andy128k avatar Oct 06 '19 21:10 andy128k

sure. sorry i have been busy shlepping my stuff from place to place.

I am going to create a small repo and post it here. give me an hour or so

I haven't had time to understand junipers test suite, but i can isolate it for you...

dreamcodez avatar Oct 20 '19 21:10 dreamcodez

I added more context to 434, and I have also posted a bounty for this issue (427)

$50 out of my own pocket if someone can just get this one thing fixed, its the only thing which is causing me to not want to use juniper right now :)

https://www.bountysource.com/issues/80699749-literal-scalar-parsing-should-not-panic

Thanks !

dreamcodez avatar Oct 20 '19 22:10 dreamcodez

(and unit tests of course)

dreamcodez avatar Oct 20 '19 22:10 dreamcodez

@andy128k @dreamcodez I tried to reproduce this, but haven't been able to.

All seems to work as expected, without a panic.

Can you please provide a simple reproduction with a sample schema definition and query?

theduke avatar Oct 21 '19 15:10 theduke

@dreamcodez @theduke Looks like I've reproduced it. String was a bad example (any schema has it because it is used in introspection).

But passing Int or Float literal causes panic. So, this is not because of violation of contract but because of absence of scalar type in schema.

andy128k avatar Oct 21 '19 18:10 andy128k

Can you add a test reproducing the panic?

theduke avatar Oct 22 '19 10:10 theduke

@theduke It is in my PR. There are two commits. First one reproduces panic and the second one fixes it.

andy128k avatar Oct 22 '19 11:10 andy128k

@dreamcodez @theduke Looks like I've reproduced it. String was a bad example (any schema has it because it is used in introspection).

But passing Int or Float literal causes panic. So, this is not because of violation of contract but because of absence of scalar type in schema.

I don't think that's the only cause -- it panics if you just pass it something which is not what the schema expects -- this is what happened in my small prototypal codebase. I don't have the example code right now... but I may in a few days or so... full disclosure:

I installed xfs in the boot partition of my new linux laptop/thinkpad and i let the battery die. so yeah -- the boot partition got hosed and once i rescue the laptop i can revive said code -- or i can code up an example codebase tonight.. just give me some time but its not really a hard test case at all.

If you complete the code I posted in issue #434 except inside the juniper codebase i'm pretty sure that will also do it.

post more soon.

dreamcodez avatar Oct 23 '19 05:10 dreamcodez

OK... as far as I can tell this issue might be solved in 0.14 and on.... I need to dig up the original code. I was not able to reproduce it from memory :) kudos to you all for finding out how to remove said panics even though it may I have identified the wrong panics in the first place :)

dreamcodez avatar Oct 23 '19 05:10 dreamcodez

carry on, ignore my drivel unless i can find that original code soon. and if these panics disappear which are being targeted (original code referenced in issue) -- i just need to know who to disburse the bounty to. thanks all!

dreamcodez avatar Oct 23 '19 05:10 dreamcodez

OK -- so it turns out this is an issue in 0.13.1:

pov from graphql playground:

{
  "error": "Failed to fetch. Please check your connection"
}

process:

thread 'tokio-runtime-worker-0' panicked at 'There needs to be a Int type'

in 0.14.1 its gonezo:

pov from graphql playground:

{
  "error": {
    "errors": [
      {
        "message": "There needs to be an Int type",
        "locations": [
          {
            "line": 2,
            "column": 13
          }
        ]
      }
    ]
  }
}

... process doesn't crash!

that said, kudos for banishing more panics from this codebase! :)

:pray:

now perusing other issues i know you mentioned bounty payouts somewhere ;)

dreamcodez avatar Oct 27 '19 18:10 dreamcodez

@andy128k please click 'claim' on the bounty on this page so i can approve it -- my money is tied up in their escrow :)

:pray:

https://www.bountysource.com/issues/80699749-literal-scalar-parsing-should-not-panic

dreamcodez avatar Oct 27 '19 18:10 dreamcodez