Skip to content

Commit f938273

Browse files
authored
Support xmlsec1 1.3.13 and bundle it in all wheels (#435)
Fixes building and running python-xmlsec against xmlsec1 1.3.13 and bumps the bundled xmlsec1 to 1.3.13 on all platforms (Linux, macOS, Windows). Closes #434. ## Backward-incompatible changes xmlsec1 1.3.13 tightens several defaults. python-xmlsec follows the new library defaults and does not restore the old behavior. Wheels bundle 1.3.13, so these changes apply to everyone installing from wheels. Source builds behave according to the libxmlsec1 version they link against. ### 1. Only empty and same-document reference URIs are allowed by default `<dsig:Reference>` URIs are now limited to `URI=""` and `URI="#id"` by default. Any other URI, e.g. `http://...`, `cid:...`, `file:///...` or a relative path such as `other.xml`, makes `SignatureContext.sign()` / `verify()` raise `xmlsec.Error`. This mostly affects code that resolves external references through `xmlsec.register_callbacks`. To opt in, set the new `SignatureContext.enabled_reference_uris` property: ctx = xmlsec.SignatureContext() ctx.enabled_reference_uris = ( xmlsec.constants.TransformUriTypeEmpty | xmlsec.constants.TransformUriTypeSameDocument | xmlsec.constants.TransformUriTypeRemote ) # or xmlsec.constants.TransformUriTypeAny ### 2. External URIs are rejected in encryption and KeyInfo processing The same default (empty + same-document only) now applies to: - `<enc:CipherReference URI="...">` when decrypting with `EncryptionContext.decrypt()` - `<dsig:RetrievalMethod URI="...">` and `<dsig11:KeyInfoReference URI="...">` in `<dsig:KeyInfo>` python-xmlsec does not expose a setting to widen these, so documents that rely on external URIs here can no longer be processed. `EncryptionContext.encrypt_uri()` is not affected: xmlsec1 allows the URI passed by the caller. ### 3. Other behavior changes inherited from xmlsec1 1.3.13 These come from the library itself and have no python-xmlsec setting: - **Relationship transform** (OOXML / OPC signatures): several handling bugs were fixed, so digests can differ from those produced by older versions. - **PBKDF2**: iteration counts above 1,000,000 are rejected. - **CBC decryption**: a padding length of zero is now rejected as invalid padding. - **Nested key depth**: the `<enc:EncryptedKey>` nesting limit (default 1) now also counts `<enc11:DerivedKey>`, `<enc:AgreementMethod>` and `<as:EncapsulationMechanism>`. See the xmlsec1 1.3.13 release notes for the full list: https://www.aleksey.com/xmlsec/news.html ## New API - `SignatureContext.enabled_reference_uris` (read/write `int`): bitmask of allowed `<dsig:Reference>` URI types. Raises `TypeError` for non-int or `del`, `OverflowError` for negative values, `ValueError` for values above `TransformUriTypeAny`. - New constants: `xmlsec.constants.TransformUriTypeNone`, `TransformUriTypeEmpty`, `TransformUriTypeSameDocument`, `TransformUriTypeLocal`, `TransformUriTypeRemote`, `TransformUriTypeAny`. ## Compatibility fixes for xmlsec1 1.3.13 - Build: include `<xmlsec/dl.h>` explicitly. 1.3.13 no longer includes it transitively. - `xmlsec.init()` called after import no longer fails. 1.3.13 rejects a second `xmlSecInit()` without a shutdown in between. - Encrypting with an `<enc:EncryptedKey>` keeps lax key lookup for the key encryption key. 1.3.13 stopped copying the lax-search flag into the nested context that encrypts the session key. ## Build and packaging - Bundled xmlsec1 1.3.12 → 1.3.13 on Linux/macOS (`build_support`, `cache_libs` workflow) and Windows (win-binaries release `2026.10.28`). - Static xmlsec1 builds are configured with `--disable-apps` (1.3.13's fuzz targets fail to build against a private libxslt prefix) and `--with-pic` (1.3.13's hardening flags otherwise produce non-PIC objects that can't be linked into the extension module). ## Docs and tests - Documented the new constants and `enabled_reference_uris`. - Added tests for `enabled_reference_uris`. The `register_callbacks` test now opts into all URI types.
1 parent 99f8855 commit f938273

11 files changed

Lines changed: 123 additions & 4 deletions

File tree

‎.github/workflows/cache_libs.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ on:
2020
required: false
2121
type: string
2222
XMLSEC1_VERSION:
23-
default: "1.3.12"
23+
default: "1.3.13"
2424
required: false
2525
type: string
2626
ZLIB_VERSION:

‎build_support/lib_xmlsec_dependency_builder.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def triplet(self) -> str:
3636

3737

3838
class LibXmlsecDependencyBuilder:
39-
WINDOWS_LIBS_DOWNLOAD_RELEASE_URL = 'https://github.com/mxamin/python-xmlsec-win-binaries/releases/download/2026.10.23/'
39+
WINDOWS_LIBS_DOWNLOAD_RELEASE_URL = 'https://github.com/mxamin/python-xmlsec-win-binaries/releases/download/2026.10.28/'
4040
LIB_VERSION_ENV_VARS: ClassVar[dict[str, str]] = {
4141
'libiconv_version': 'PYXMLSEC_LIBICONV_VERSION',
4242
'libxml2_version': 'PYXMLSEC_LIBXML2_VERSION',
@@ -50,15 +50,15 @@ class LibXmlsecDependencyBuilder:
5050
'libxml2_version': '2.14.6', # Make sure it matches with lxml
5151
'libxslt_version': '1.1.43', # 1.1.44+ requires libxml2 >= 2.15.1
5252
'openssl_version': '3.6.5',
53-
'xmlsec1_version': '1.3.12',
53+
'xmlsec1_version': '1.3.13',
5454
'zlib_version': '1.3.2',
5555
}
5656
WINDOWS_DEFAULT_LIB_VERSIONS: ClassVar[dict[str, str]] = {
5757
'libiconv_version': '1.19-1',
5858
'libxml2_version': '2.11.9-8', # Make sure it matches with lxml
5959
'libxslt_version': '1.1.45',
6060
'openssl_version': '3.5.9',
61-
'xmlsec1_version': '1.3.12',
61+
'xmlsec1_version': '1.3.13',
6262
'zlib_version': '1.3.2',
6363
}
6464

@@ -390,6 +390,8 @@ def _build_xmlsec1(self, env, prefix_arg, host_arg):
390390
'./configure',
391391
prefix_arg,
392392
'--disable-shared',
393+
'--with-pic',
394+
'--disable-apps',
393395
'--disable-gost',
394396
'--enable-md5',
395397
'--enable-ripemd160',

‎doc/source/modules/constants.rst‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,31 @@ Transforms
292292

293293
Transform could be used for operation.
294294

295+
.. data:: xmlsec.constants.TransformUriTypeNone
296+
297+
No URI types are allowed.
298+
299+
.. data:: xmlsec.constants.TransformUriTypeEmpty
300+
301+
Empty URI (:xml:`URI=""`), i.e. the document containing the signature.
302+
303+
.. data:: xmlsec.constants.TransformUriTypeSameDocument
304+
305+
Same-document URI (:xml:`URI="#id"`).
306+
307+
.. data:: xmlsec.constants.TransformUriTypeLocal
308+
309+
Local file URI (:xml:`URI="file:///path"`).
310+
311+
.. data:: xmlsec.constants.TransformUriTypeRemote
312+
313+
Any other non-empty URI, e.g. :xml:`URI="http://..."`, :xml:`URI="cid:..."`
314+
or a relative path such as :xml:`URI="other.xml"`.
315+
316+
.. data:: xmlsec.constants.TransformUriTypeAny
317+
318+
Any URI type.
319+
295320
.. data:: xmlsec.constants.TransformInclC14N
296321

297322
The regular (inclusive) C14N without comments transform klass.

‎src/constants.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ int PyXmlSec_ConstantsModule_Init(PyObject* package) {
282282
if (PYXMLSEC_ADD_INT_CONSTANT(TransformUsageEncryptionMethod) < 0) goto ON_FAIL;
283283
if (PYXMLSEC_ADD_INT_CONSTANT(TransformUsageAny) < 0) goto ON_FAIL;
284284

285+
if (PYXMLSEC_ADD_INT_CONSTANT(TransformUriTypeNone) < 0) goto ON_FAIL;
286+
if (PYXMLSEC_ADD_INT_CONSTANT(TransformUriTypeEmpty) < 0) goto ON_FAIL;
287+
if (PYXMLSEC_ADD_INT_CONSTANT(TransformUriTypeSameDocument) < 0) goto ON_FAIL;
288+
if (PYXMLSEC_ADD_INT_CONSTANT(TransformUriTypeLocal) < 0) goto ON_FAIL;
289+
if (PYXMLSEC_ADD_INT_CONSTANT(TransformUriTypeRemote) < 0) goto ON_FAIL;
290+
if (PYXMLSEC_ADD_INT_CONSTANT(TransformUriTypeAny) < 0) goto ON_FAIL;
291+
285292
#undef PYXMLSEC_ADD_INT_CONSTANT
286293

287294
#define PYXMLSEC_DECLARE_NAMESPACE(var, name) \

‎src/ds.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,40 @@ static PyObject* PyXmlSec_SignatureContextSetEnabledKeyData(PyObject* self, PyOb
517517
return NULL;
518518
}
519519

520+
static const char PyXmlSec_SignatureContextEnabledReferenceUris__doc__[] = \
521+
"Bitmask of :samp:`TransformUriType{XXX}` constants allowed in :xml:`<dsig:Reference/>` URI.\n\n"
522+
".. note:: since xmlsec1 1.3.13 only empty and same-document URIs are enabled by default. "
523+
"Dereferencing local or remote URIs (e.g. through :func:`~xmlsec.register_callbacks`) requires "
524+
"an explicit opt-in.\n";
525+
static PyObject* PyXmlSec_SignatureContextEnabledReferenceUrisGet(PyObject* self, void* closure) {
526+
PyXmlSec_SignatureContext* ctx = (PyXmlSec_SignatureContext*)self;
527+
return PyLong_FromUnsignedLong(ctx->handle->enabledReferenceUris);
528+
}
529+
530+
static int PyXmlSec_SignatureContextEnabledReferenceUrisSet(PyObject* self, PyObject* value, void* closure) {
531+
PyXmlSec_SignatureContext* ctx = (PyXmlSec_SignatureContext*)self;
532+
unsigned long uris;
533+
534+
if (value == NULL) {
535+
PyErr_SetString(PyExc_TypeError, "cannot delete enabled_reference_uris.");
536+
return -1;
537+
}
538+
if (!PyLong_Check(value)) {
539+
PyErr_SetString(PyExc_TypeError, "instance of *int* expected.");
540+
return -1;
541+
}
542+
uris = PyLong_AsUnsignedLong(value);
543+
if (PyErr_Occurred()) {
544+
return -1;
545+
}
546+
if (uris > xmlSecTransformUriTypeAny) {
547+
PyErr_SetString(PyExc_ValueError, "invalid uri type mask.");
548+
return -1;
549+
}
550+
ctx->handle->enabledReferenceUris = (xmlSecTransformUriType)uris;
551+
return 0;
552+
}
553+
520554
static PyGetSetDef PyXmlSec_SignatureContextGetSet[] = {
521555
{
522556
"key",
@@ -525,6 +559,13 @@ static PyGetSetDef PyXmlSec_SignatureContextGetSet[] = {
525559
(char*)PyXmlSec_SignatureContextKey__doc__,
526560
NULL
527561
},
562+
{
563+
"enabled_reference_uris",
564+
(getter)PyXmlSec_SignatureContextEnabledReferenceUrisGet,
565+
(setter)PyXmlSec_SignatureContextEnabledReferenceUrisSet,
566+
(char*)PyXmlSec_SignatureContextEnabledReferenceUris__doc__,
567+
NULL
568+
},
528569
{NULL} /* Sentinel */
529570
};
530571

‎src/enc.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ static int PyXmlSec_EncryptionContext__init__(PyObject* self, PyObject* args, Py
6262
ctx->handle->keyInfoReadCtx.flags = XMLSEC_KEYINFO_FLAGS_LAX_KEY_SEARCH;
6363
ctx->handle->keyInfoWriteCtx.flags = XMLSEC_KEYINFO_FLAGS_LAX_KEY_SEARCH;
6464

65+
// xmlsec 1.3.13 no longer copies these flags to the read context of the nested
66+
// context used to encrypt <enc:EncryptedKey/>, so the session key encryption key
67+
// lookup becomes strict. Create the nested context upfront (it is reused by
68+
// xmlsec) and enable lax key search on it as well.
69+
if (xmlSecKeyInfoCtxCreateEncCtx(&(ctx->handle->keyInfoWriteCtx)) < 0) {
70+
// ctx->manager is owned by ctx now and released on dealloc
71+
PyXmlSec_SetLastError("failed to create the nested encryption context");
72+
return -1;
73+
}
74+
ctx->handle->keyInfoWriteCtx.encCtx->keyInfoReadCtx.flags = XMLSEC_KEYINFO_FLAGS_LAX_KEY_SEARCH;
75+
6576
return 0;
6677
ON_FAIL:
6778
PYXMLSEC_DEBUGF("%p: init enc context - failed", self);

‎src/main.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <xmlsec/xmlsec.h>
1616
#include <xmlsec/crypto.h>
17+
#include <xmlsec/dl.h>
1718
#include <xmlsec/errors.h>
1819
#include <xmlsec/base64.h>
1920
#include <xmlsec/io.h>
@@ -56,6 +57,12 @@ static void PyXmlSec_Free(int what) {
5657
}
5758

5859
static int PyXmlSec_Init(void) {
60+
// Already initialized (e.g. on module import). Since xmlsec1 1.3.13,
61+
// calling xmlSecInit() again without a shutdown fails.
62+
if (free_mode == _PYXMLSEC_FREE_ALL) {
63+
return 0;
64+
}
65+
5966
if (xmlSecInit() < 0) {
6067
PyXmlSec_SetLastError("cannot initialize xmlsec library.");
6168
PyXmlSec_Free(_PYXMLSEC_FREE_NONE);

‎src/xmlsec/__init__.pyi‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class KeysManager:
6868

6969
class SignatureContext:
7070
key: Key | None
71+
enabled_reference_uris: int
7172
def enable_reference_transform(self, transform: Transform) -> None: ...
7273
def enable_signature_transform(self, transform: Transform) -> None: ...
7374
def register_id(self, node: _Element, id_attr: str = ..., id_ns: str | None = ...) -> None: ...

‎src/xmlsec/constants.pyi‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ TransformSha224: Final[__Transform]
129129
TransformSha256: Final[__Transform]
130130
TransformSha384: Final[__Transform]
131131
TransformSha512: Final[__Transform]
132+
TransformUriTypeAny: Final[int]
133+
TransformUriTypeEmpty: Final[int]
134+
TransformUriTypeLocal: Final[int]
135+
TransformUriTypeNone: Final[int]
136+
TransformUriTypeRemote: Final[int]
137+
TransformUriTypeSameDocument: Final[int]
132138
TransformUsageAny: Final[int]
133139
TransformUsageC14NMethod: Final[int]
134140
TransformUsageDSigTransform: Final[int]

‎tests/test_ds.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ def test_set_key_bad_type(self):
3939
with self.assertRaisesRegex(TypeError, r'instance of \*xmlsec.Key\* expected.'):
4040
ctx.key = ''
4141

42+
def test_set_enabled_reference_uris(self):
43+
ctx = xmlsec.SignatureContext()
44+
uris = consts.TransformUriTypeEmpty | consts.TransformUriTypeSameDocument | consts.TransformUriTypeRemote
45+
ctx.enabled_reference_uris = uris
46+
self.assertEqual(ctx.enabled_reference_uris, uris)
47+
48+
def test_set_enabled_reference_uris_bad_args(self):
49+
ctx = xmlsec.SignatureContext()
50+
with self.assertRaisesRegex(TypeError, r'instance of \*int\* expected.'):
51+
ctx.enabled_reference_uris = ''
52+
with self.assertRaises(OverflowError):
53+
ctx.enabled_reference_uris = -1
54+
with self.assertRaisesRegex(ValueError, 'invalid uri type mask'):
55+
ctx.enabled_reference_uris = consts.TransformUriTypeAny + 1
56+
with self.assertRaisesRegex(TypeError, 'cannot delete'):
57+
del ctx.enabled_reference_uris
58+
4259
def test_set_invalid_key(self):
4360
ctx = xmlsec.SignatureContext(manager=xmlsec.KeysManager())
4461
with self.assertRaisesRegex(TypeError, 'empty key.'):

0 commit comments

Comments
 (0)