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

Added support for retrieving uri from referenced_document when uri is external file #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/xmldsig/reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,9 @@ def referenced_node
if reference_uri && reference_uri != ""
if @id_attr.nil? && reference_uri.start_with?("cid:")
content_id = reference_uri[4..-1]
if @referenced_documents.has_key?(content_id)
@referenced_documents[content_id].dup
else
raise(
ReferencedNodeNotFound,
"Could not find referenced document with ContentId #{content_id}"
)
end
get_node_by_referenced_documents!(@referenced_documents, content_id)
elsif !File.extname(reference_uri).gsub('.', '').empty?
get_node_by_referenced_documents!(@referenced_documents, reference_uri)
else
id = reference_uri[1..-1]
referenced_node_xpath = @id_attr ? "//*[@#{@id_attr}=$uri]" : "//*[@ID=$uri or @wsu:Id=$uri]"
Expand Down Expand Up @@ -96,5 +91,18 @@ def validate_digest_value
@errors << :digest_value
end
end

private

def get_node_by_referenced_documents!(referenced_documents, content_id)
if referenced_documents.has_key?(content_id)
referenced_documents[content_id].dup
else
raise(
ReferencedNodeNotFound,
"Could not find referenced document with ContentId #{content_id}"
)
end
end
end
end
15 changes: 15 additions & 0 deletions spec/fixtures/unsigned_with_xml_reference.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<foo:Foo xmlns:foo="http://example.com/foo#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" ID="foo">
<foo:Bar>bar</foo:Bar>
<ds:Signature>
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<ds:Reference URI="test.xml">
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue></ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue></ds:SignatureValue>
</ds:Signature>
</foo:Foo>
24 changes: 24 additions & 0 deletions spec/lib/xmldsig/reference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@
end
end
end

context "when the referenced node is file name" do
let(:document) { Nokogiri::XML::Document.parse File.read("spec/fixtures/unsigned_with_xml_reference.xml") }
let(:xml_document) { "<test><ing>present</ing></test>" }
let(:referenced_documents) { { "test.xml" => xml_document } }
let(:reference) { Xmldsig::Reference.new(document.at_xpath('//ds:Reference', Xmldsig::NAMESPACES), nil, referenced_documents) }

it "has the correct reference_uri" do
expect(reference.reference_uri).to eq "test.xml"
end

it "returns the document referenced by file name" do
expect(reference.referenced_node).to eq xml_document
end

context "when the document has no referenced_documents matching the referenced name" do
let(:referenced_documents) { Hash.new }

it "raises ReferencedNodeNotFound" do
expect { reference.referenced_node }.
to raise_error(Xmldsig::Reference::ReferencedNodeNotFound)
end
end
end
end

describe "#reference_uri" do
Expand Down