check if links is an array in the JRD

This commit is contained in:
Benjamin Neff 2016-06-27 04:00:59 +02:00
parent 2a0e87957f
commit c0ab42fecc
2 changed files with 13 additions and 2 deletions

View file

@ -129,6 +129,8 @@ class ConnectionTester
nd_resp = http.get(find_nodeinfo_url(ni_resp.body))
find_software_version(nd_resp.body)
end
rescue NodeInfoFailure => e
raise e
rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError => e
raise NodeInfoFailure, "#{e.class}: #{e.message}"
rescue Faraday::ResourceNotFound, JSON::JSONError => e
@ -183,8 +185,10 @@ class ConnectionTester
# walk the JSON document, get the actual document location
def find_nodeinfo_url(body)
links = JSON.parse(body)
links.fetch("links").find { |entry|
jrd = JSON.parse(body)
links = jrd.fetch("links")
raise NodeInfoFailure, "invalid JRD: '#/links' is not an array!" unless links.is_a?(Array)
links.find { |entry|
entry.fetch("rel") == NODEINFO_SCHEMA
}.fetch("href")
end

View file

@ -145,6 +145,13 @@ describe ConnectionTester do
expect { tester.nodeinfo }.to raise_error(ConnectionTester::NodeInfoFailure)
end
it "handles a invalid jrd document gracefully" do
invalid_wellknown = {links: {rel: ConnectionTester::NODEINFO_SCHEMA, href: "/nodeinfo"}}
stub_request(:get, "#{url}#{ConnectionTester::NODEINFO_FRAGMENT}")
.to_return(status: 200, body: JSON.generate(invalid_wellknown))
expect { tester.nodeinfo }.to raise_error(ConnectionTester::NodeInfoFailure)
end
it "handles a invalid nodeinfo document gracefully" do
stub_request(:get, "#{url}#{ConnectionTester::NODEINFO_FRAGMENT}")
.to_return(status: 200, body: JSON.generate(ni_wellknown))