arangamani / jenkins_api_client

Ruby Client libraries for communicating with Jenkins Remote Access API
http://github.arangamani.net/jenkins_api_client/
MIT License
416 stars 239 forks source link

Unable to parse response body with bad characters #294

Open itskingori opened 4 years ago

itskingori commented 4 years ago

I'm experiencing a JSON::ParserError because of a bad character (in my case it's 0x1b):

Screenshot 2020-03-24 at 16 59 18

Here's the stack trace that showed me where the issue was:

Screenshot 2020-03-24 at 16 55 21

This is the JSON.parse we're hitting ...

https://github.com/arangamani/jenkins_api_client/blob/72f49f2e7ef12d58a0e33856ca91962b2e27709b/lib/jenkins_api_client/client.rb#L807-L816

I've been able to replicate using this file (jenkins-log-with-bad-character.txt):

[1] pry(main)> file = File.open("/Users/Kingori/Desktop/jenkins-log-with-bad-character.txt")
=> #<File:/Users/Kingori/Desktop/jenkins-log-with-bad-character.txt>
[2] pry(main)> file_data = file.read
=> "{\"comment\":\"Some gems seem to be missing from your /srv/bundle/cache directory.\\n\eCould not find rake-12.3.3 in any of the sources\"}\n"
[3] pry(main)> JSON.parse(file_data)
JSON::ParserError: 783: unexpected token at '{"comment":"Some gems seem to be missing from your /srv/bundle/cache directory.\nould not find rake-12.3.3 in any of the sources"}
'
from /Users/Kingori/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/json-2.3.0/lib/json/common.rb:156:in `parse'
[4] pry(main)> file_data.encoding
=> #<Encoding:UTF-8>
hadrianvalentine commented 4 years ago

Parsing with Oj doesn't seem to give this error. Making the switch might be a convenient fix for this.

JSON

irb(main):004:0> JSON.parse("{\"comment\":\"Some gems seem to be missing from your /srv/bundle/cache directory.\\n\eCould not find rake-12.3.3 in any of the sources\"}\n")
Traceback (most recent call last):
        1: from (irb):4
JSON::ParserError (783: unexpected token at '{"comment":"Some gems seem to be missing from your /srv/bundle/cache directory.\nould not find rake-12.3.3 in any of the sources"})
'

Oj

[6] pry(main)> Oj.load("{\"comment\":\"Some gems seem to be missing from your /srv/bundle/cache directory.\\n\eCould not find rake-12.3.3 in any of the sources\"}\n").first
=> ["comment",
 "Some gems seem to be missing from your /srv/bundle/cache directory.\n\eCould not find rake-12.3.3 in any of the sources"]
grosser commented 1 year ago

hmmm this seems to be a more general problem

JSON.parse("\"\n\"")

does not work either ... but also this seems to mean that the jenkins server is at fault and not the client, since the \e should have been escaped to \\e right ?

grosser commented 1 year ago

so prefer to open an issue with jenkins + make sure jenkins is up to date and then see what they say about this issue the \e afaik usually is from an escape sequence, so that smells like some cli printing gone wrong :/

... can you reliably reproduce this or is this randomly happening or at a random position in the reply ?