Commit 2a0e8795 authored by Benjamin Neff's avatar Benjamin Neff

validate NodeInfo json against schema in ConnectionTester

parent da1a0289
......@@ -129,6 +129,8 @@ class ConnectionTester
nd_resp = http.get(find_nodeinfo_url(ni_resp.body))
find_software_version(nd_resp.body)
end
rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError => e
raise NodeInfoFailure, "#{e.class}: #{e.message}"
rescue Faraday::ResourceNotFound, JSON::JSONError => e
raise NodeInfoFailure, e.message[0..255].encode(Encoding.default_external, undef: :replace)
rescue StandardError => e
......@@ -190,6 +192,7 @@ class ConnectionTester
# walk the JSON document, find the version string
def find_software_version(body)
info = JSON.parse(body)
JSON::Validator.validate!(NodeInfo.schema("1.0"), info)
sw = info.fetch("software")
@result.software_version = "#{sw.fetch('name')} #{sw.fetch('version')}"
end
......
......@@ -113,13 +113,21 @@ describe ConnectionTester do
end
describe "#nodeinfo" do
let(:ni_wellknown) { {links: [{rel: ConnectionTester::NODEINFO_SCHEMA, href: "/nodeinfo"}]} }
it "reads the version from the nodeinfo document" do
ni_wellknown = {links: [{rel: ConnectionTester::NODEINFO_SCHEMA, href: "/nodeinfo"}]}
ni_document = {software: {name: "diaspora", version: "a.b.c.d"}}
ni_document = NodeInfo.build do |doc|
doc.version = "1.0"
doc.open_registrations = true
doc.protocols.inbound << "diaspora"
doc.protocols.outbound << "diaspora"
doc.software.name = "diaspora"
doc.software.version = "a.b.c.d"
end
stub_request(:get, "#{url}#{ConnectionTester::NODEINFO_FRAGMENT}")
.to_return(status: 200, body: JSON.generate(ni_wellknown))
stub_request(:get, "#{url}/nodeinfo").to_return(status: 200, body: JSON.generate(ni_document))
stub_request(:get, "#{url}/nodeinfo").to_return(status: 200, body: JSON.generate(ni_document.as_json))
tester.nodeinfo
expect(result.software_version).to eq("diaspora a.b.c.d")
......@@ -136,5 +144,12 @@ describe ConnectionTester do
.to_return(status: 200, body: '{"json"::::"malformed"}')
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))
stub_request(:get, "#{url}/nodeinfo").to_return(status: 200, body: '{"software": "invalid nodeinfo"}')
expect { tester.nodeinfo }.to raise_error(ConnectionTester::NodeInfoFailure)
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment