Skip to content

Commit 4c276d6

Browse files
tpazderkablaggacao
authored andcommitted
Omitt WSSE header elements from signature
1 parent c758ce2 commit 4c276d6

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed

src/zeep/wsse/signature.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
# SOAP envelope
2727
SOAP_NS = "http://schemas.xmlsoap.org/soap/envelope/"
28+
# Namespaces omitted from signing
29+
OMITTED_HEADERS = [ns.WSSE]
2830

2931

3032
def _read_file(f_name):
@@ -259,7 +261,9 @@ def _signature_prepare(envelope, key, signature_method, digest_method, signature
259261
header = get_or_create_header(envelope)
260262
if signatures["everything"]:
261263
for node in header.iterchildren():
262-
_sign_node(ctx, signature, node, digest_method)
264+
# Everything doesn't mean everything ...
265+
if node.nsmap.get(node.prefix) not in OMITTED_HEADERS:
266+
_sign_node(ctx, signature, node, digest_method)
263267
else:
264268
for node in signatures["header"]:
265269
_sign_node(

tests/test_wsse_signature.py

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,124 @@ def test_sign(
140140
assert sig.get("Algorithm") == expected_signature_href
141141

142142

143+
@skip_if_no_xmlsec
144+
@pytest.mark.parametrize("digest_method,expected_digest_href", DIGEST_METHODS_TESTDATA)
145+
@pytest.mark.parametrize(
146+
"signature_method,expected_signature_href", SIGNATURE_METHODS_TESTDATA
147+
)
148+
def test_sign_element(
149+
digest_method, signature_method, expected_digest_href, expected_signature_href
150+
):
151+
envelope = load_xml(
152+
"""
153+
<soapenv:Envelope
154+
xmlns:tns="http://tests.python-zeep.org/"
155+
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
156+
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
157+
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
158+
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
159+
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
160+
<soapenv:Header>
161+
<wsse:Security mustUnderstand="true">
162+
<wsu:Timestamp>
163+
<wsu:Created>2015-06-25T21:53:25.246276+00:00</wsu:Created>
164+
<wsu:Expires>2015-06-25T21:58:25.246276+00:00</wsu:Expires>
165+
</wsu:Timestamp>
166+
</wsse:Security>
167+
<tns:Some>OK</tns:Some>
168+
</soapenv:Header>
169+
<soapenv:Body>
170+
<tns:Function>
171+
<tns:Argument>OK</tns:Argument>
172+
</tns:Function>
173+
</soapenv:Body>
174+
</soapenv:Envelope>
175+
"""
176+
)
177+
178+
# Force header element
179+
signatures = {
180+
"everything": False,
181+
"body": False,
182+
"header": [{"Namespace": "http://tests.python-zeep.org/", "Name": "Some"}],
183+
}
184+
signature.sign_envelope(
185+
envelope,
186+
KEY_FILE,
187+
KEY_FILE,
188+
signature_method=getattr(xmlsec_installed.Transform, signature_method),
189+
digest_method=getattr(xmlsec_installed.Transform, digest_method),
190+
signatures=signatures,
191+
)
192+
signature.verify_envelope(envelope, KEY_FILE)
193+
194+
digests = envelope.xpath("//ds:DigestMethod", namespaces={"ds": ns.DS})
195+
assert len(digests)
196+
for digest in digests:
197+
assert digest.get("Algorithm") == expected_digest_href
198+
signatures = envelope.xpath("//ds:SignatureMethod", namespaces={"ds": ns.DS})
199+
assert len(signatures)
200+
for sig in signatures:
201+
assert sig.get("Algorithm") == expected_signature_href
202+
203+
204+
@skip_if_no_xmlsec
205+
@pytest.mark.parametrize("digest_method,expected_digest_href", DIGEST_METHODS_TESTDATA)
206+
@pytest.mark.parametrize(
207+
"signature_method,expected_signature_href", SIGNATURE_METHODS_TESTDATA
208+
)
209+
def test_sign_everything(
210+
digest_method, signature_method, expected_digest_href, expected_signature_href
211+
):
212+
envelope = load_xml(
213+
"""
214+
<soapenv:Envelope
215+
xmlns:tns="http://tests.python-zeep.org/"
216+
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
217+
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
218+
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
219+
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
220+
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
221+
<soapenv:Header>
222+
<wsse:Security mustUnderstand="true">
223+
<wsu:Timestamp>
224+
<wsu:Created>2015-06-25T21:53:25.246276+00:00</wsu:Created>
225+
<wsu:Expires>2015-06-25T21:58:25.246276+00:00</wsu:Expires>
226+
</wsu:Timestamp>
227+
</wsse:Security>
228+
<tns:Some>OK</tns:Some>
229+
</soapenv:Header>
230+
<soapenv:Body>
231+
<tns:Function>
232+
<tns:Argument>OK</tns:Argument>
233+
</tns:Function>
234+
</soapenv:Body>
235+
</soapenv:Envelope>
236+
"""
237+
)
238+
239+
# Force header element and body signature
240+
signatures = {"everything": True, "body": True, "header": []}
241+
signature.sign_envelope(
242+
envelope,
243+
KEY_FILE,
244+
KEY_FILE,
245+
signature_method=getattr(xmlsec_installed.Transform, signature_method),
246+
digest_method=getattr(xmlsec_installed.Transform, digest_method),
247+
signatures=signatures,
248+
)
249+
signature.verify_envelope(envelope, KEY_FILE)
250+
251+
digests = envelope.xpath("//ds:DigestMethod", namespaces={"ds": ns.DS})
252+
assert len(digests)
253+
for digest in digests:
254+
assert digest.get("Algorithm") == expected_digest_href
255+
signatures = envelope.xpath("//ds:SignatureMethod", namespaces={"ds": ns.DS})
256+
assert len(signatures)
257+
for sig in signatures:
258+
assert sig.get("Algorithm") == expected_signature_href
259+
260+
143261
@skip_if_no_xmlsec
144262
def test_sign_pw():
145263
envelope = load_xml(
@@ -161,7 +279,9 @@ def test_sign_pw():
161279

162280
# Force body signature
163281
signatures = {"everything": False, "body": True, "header": []}
164-
signature.sign_envelope(envelope, KEY_FILE_PW, KEY_FILE_PW, "geheim", signatures=signatures)
282+
signature.sign_envelope(
283+
envelope, KEY_FILE_PW, KEY_FILE_PW, "geheim", signatures=signatures
284+
)
165285
signature.verify_envelope(envelope, KEY_FILE_PW)
166286

167287

0 commit comments

Comments
 (0)