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

ESP8266WebServer does not handle special characters in JSON body #2198

Closed
liquidfalcon opened this issue Jun 27, 2016 · 9 comments
Closed

ESP8266WebServer does not handle special characters in JSON body #2198

liquidfalcon opened this issue Jun 27, 2016 · 9 comments

Comments

@liquidfalcon
Copy link

liquidfalcon commented Jun 27, 2016

Regardless of whether the body is set as plain or application/json, _parseArguments will always perform URL decoding on it, leading to unexpected results.

Example JSON sent:

{ 
  "ssid" : "My+Network",
  "psk" :"special+Password!"
}

After receiving it through ESP8266WebServer

{ 
  "ssid" : "My Network",
  "psk" :"special Password!"
}

Currently it makes it so no JSON can include a plus sign, or any other symbol the URL decoder will pick up.

EDIT: Incidentally, including something like this completely breaks input:

{ "ssid":"SSID+&(!@","psk":"@#$%^&+" }

Double EDIT:

A super simple if else block like this works around it for now:

   if (data.indexOf(F("plain=")) != -1) {
           _currentArgs = new RequestArgument[1];
           RequestArgument& arg = _currentArgs[0];
           arg.key = data.substring(data.indexOf(F("plain=")), data.indexOf(F("=")));
           arg.value = data.substring(data.indexOf(F("=")) + 1);
   } else {

I plan to modify it to include the option to retrieve the raw body payload if the content-type is equal to application/json, and not call _parseArguments. Would you be interested in this as a pull request once this is complete? I think this is the most sensible way to handle JSON data, otherwise it seems to be a bit of a hack to run it past something expecting url/form encoded data.

@liquidfalcon liquidfalcon changed the title ESP8266WebServer Performs URL Decoding on JSON body ESP8266WebServer does not handle special characters in JSON body Jun 27, 2016
@igrr
Copy link
Member

igrr commented Jun 30, 2016

Need to check this together with #1989.

@igrr igrr added this to the 2.4.0 milestone Jun 30, 2016
me-no-dev pushed a commit to me-no-dev/Arduino that referenced this issue Jun 30, 2016
me-no-dev added a commit that referenced this issue Jun 30, 2016
@me-no-dev
Copy link
Collaborator

@liquidfalcon issue should be fixed now. Can you please confirm?

me-no-dev added a commit that referenced this issue Jul 4, 2016
* fix urlDecode points

Fixes:

#1989
#2198

* Add missing separator between get and plain post arguments
@liquidfalcon
Copy link
Author

So, the commits fix the first issue, which was with the plus symbol, but, it still picks up on others, such as the Ampersand, and cuts the payload after that.

For example, here is what I send:
{ "ssid":"SSID+&(!@","psk":"@#$%^&+" }

And this is what I receive for plain
{ "ssid":"SSID+

@me-no-dev
Copy link
Collaborator

can you please paste the full request that you are sending so I can test it here?

@liquidfalcon
Copy link
Author

liquidfalcon commented Jul 6, 2016

Of course - Here's the raw request I tested with using cURL:

POST /config HTTP/1.1
Host: linez-43.local
User-Agent: curl/7.47.1
Accept: */*
Content-Type: application/json
Content-Length: 38

{ "ssid":"SSID+&(!@","psk":"@#$%^&+" }

Command: curl -X POST -d '{ "ssid":"SSID+&(!@","psk":"@#$%^&+" }' -H "Content-Type: application/json" linez-43.local/config -v

@me-no-dev
Copy link
Collaborator

great! will do

@me-no-dev
Copy link
Collaborator

btw totally different issues :) first was urldecode and here it's trying to parse parameters, which it should not

@me-no-dev
Copy link
Collaborator

Fixed! The whole text is copied on it's own without ever being parsed, decoded or anything else.

curl -X POST -d '{ "ssid":"SSID+&(!@","psk":"@#$%^&+" }' -H "Content-Type: application/json" esp.local/test -v
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 192.168.254.182...
* Connected to esp.local (192.168.254.182) port 80 (#0)
> POST /test HTTP/1.1
> Host: esp.local
> User-Agent: curl/7.47.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 38
> 
* upload completely sent off: 38 out of 38 bytes
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Content-Length: 44
< Connection: close
< 
* Closing connection 0
plain={ "ssid":"SSID+&(!@","psk":"@#$%^&+" }

@liquidfalcon
Copy link
Author

You're a legend mate, thank you!

igrr pushed a commit to esp8266/ESPWebServer that referenced this issue Jun 29, 2017
igrr pushed a commit to esp8266/ESPWebServer that referenced this issue Jun 29, 2017
* fix urlDecode points

Fixes:

esp8266/Arduino#1989
esp8266/Arduino#2198

* Add missing separator between get and plain post arguments
@igrr igrr closed this as completed Jan 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants