0

I have a server written in Rust, this server gets a request in JSON, the JSON the server is getting is a string and sometimes users write quotes inside the value. For example when making a new forum thread.

The only thing I really need to do is to escape the quotes inside the value.

So this:

"{"name":""test"", "username":"tomdrc1", "date_created":"07/12/2019", "category":"Developer", "content":"awdawdasdwd"}"

Needs to be turned into this:

"{"name":"\"test\"", "username":"tomdrc1", "date_created":"07/12/2019", "category":"Developer", "content":"awdawdasdwd"}"

I tried to replace:

let data = let data = "{"name":""test"", "username":"tomdrc1", "date_created":"07/12/2019", "category":"Developer", "content":"awdawdasdwd"}".to_string().replace("\"", "\\\"");
let res: serde_json::Value = serde_json::from_str(&data).unwrap();

But it results in the following error: thread '' panicked at 'called Result::unwrap() on an Err value: Error("key must be a string", line: 1, column: 2)

I suspect because it transforms the string to the following:

let data = "{\"name\":\"\"test\"\", \"username\":\"tomdrc1\", \"date_created\":\"07/12/2019\", \"category\":\"Developer\", \"content\":\"awdawdasdwd\"}"
SirDarius
  • 41,440
  • 8
  • 86
  • 100
Tomdrc1
  • 65
  • 1
  • 8

1 Answers1

1

If I understand your question right, the issue is that you are receiving strings which should be JSON but are in fact malformed (perhaps generated by concatenating strings).

If you are unable to fix the source of those non-JSON strings the only solution I can think of involves a lot of heavy lifting with caveat:

  1. Writing a custom "malformed-JSON" parser
  2. Careful inspection/testing/analysis of how the broken client is broken
  3. Using the brokenness information to fix the "malformed-JSON"
  4. Using the fixed JSON to do normal request processing

I would recommend not to do that except for maybe a training excercise. Fixing the client will be done in minutes but implementing this perfectly on the server will take days or weeks. The next time this one problematic client has been changed you'll have to redo all the hard work.

The real answer:

  • Return "400 Bad Request" with some additional "malformed json" hint
  • Fix the client if you have access to it

Additional notes:

  • Avoid unwrapping in a server
  • Look for ways to propagate the Result::Err to caller and use it to trigger a "400 Bad Request" response
  • Check out error handling chapter in the Rust book for more
joonas
  • 136
  • 1
  • 4
  • Thanks! I'll just have to fix the client, I'm just worried in case someone will mess with the JS or something along those lines. I will stop using unwrap. – Tomdrc1 Jul 12 '19 at 20:56