1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
https://gitlab.gnome.org/GNOME/libxslt/-/issues/116
https://gitlab.gnome.org/GNOME/libxslt/-/commit/081cd6e97a620d035ed7df35e998e2d288bc7862
From 081cd6e97a620d035ed7df35e998e2d288bc7862 Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer <wellnhofer@aevum.de>
Date: Mon, 1 Jul 2024 13:21:14 +0200
Subject: [PATCH] transform: Handle filesystem paths after libxml2 changes
After changes in libxml2 2.13, base URIs and resolved URIs can be
filesystem paths.
Check for invalid URIs and remove fragment before resolving URI in
xsltDocumentFunction.
Handle filesystem paths in xsltCheckRead.
Should fix #116.
---
libxslt/functions.c | 64 ++++++++++++++++++++++++++-------------------
libxslt/security.c | 13 +++++++++
2 files changed, 50 insertions(+), 27 deletions(-)
diff --git a/libxslt/functions.c b/libxslt/functions.c
index 0c9dec43..d54f8f48 100644
--- a/libxslt/functions.c
+++ b/libxslt/functions.c
@@ -96,11 +96,10 @@ xsltXPathFunctionLookup (void *vctxt,
************************************************************************/
static void
-xsltDocumentFunctionLoadDocument(xmlXPathParserContextPtr ctxt, xmlChar* URI)
+xsltDocumentFunctionLoadDocument(xmlXPathParserContextPtr ctxt,
+ const xmlChar* URI, const xmlChar *fragment)
{
xsltTransformContextPtr tctxt;
- xmlURIPtr uri;
- xmlChar *fragment = NULL;
xsltDocumentPtr idoc; /* document info */
xmlDocPtr doc;
xmlXPathContextPtr xptrctxt = NULL;
@@ -115,26 +114,7 @@ xsltDocumentFunctionLoadDocument(xmlXPathParserContextPtr ctxt, xmlChar* URI)
goto out_fragment;
}
- uri = xmlParseURI((const char *) URI);
- if (uri == NULL) {
- xsltTransformError(tctxt, NULL, NULL,
- "document() : failed to parse URI\n");
- goto out_fragment;
- }
-
- /*
- * check for and remove fragment identifier
- */
- fragment = (xmlChar *)uri->fragment;
- if (fragment != NULL) {
- xmlChar *newURI;
- uri->fragment = NULL;
- newURI = xmlSaveUri(uri);
- idoc = xsltLoadDocument(tctxt, newURI);
- xmlFree(newURI);
- } else
- idoc = xsltLoadDocument(tctxt, URI);
- xmlFreeURI(uri);
+ idoc = xsltLoadDocument(tctxt, URI);
if (idoc == NULL) {
if ((URI == NULL) ||
@@ -194,7 +174,6 @@ out_fragment:
if (resObj == NULL)
resObj = xmlXPathNewNodeSet(NULL);
valuePush(ctxt, resObj);
- xmlFree(fragment);
}
/**
@@ -210,7 +189,8 @@ xsltDocumentFunction(xmlXPathParserContextPtr ctxt, int nargs)
{
xmlXPathObjectPtr obj, obj2 = NULL;
xmlChar *base = NULL, *URI;
-
+ xmlChar *newURI = NULL;
+ xmlChar *fragment = NULL;
if ((nargs < 1) || (nargs > 2)) {
xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
@@ -292,7 +272,32 @@ xsltDocumentFunction(xmlXPathParserContextPtr ctxt, int nargs)
valuePush(ctxt, xmlXPathNewNodeSet(NULL));
} else {
xsltTransformContextPtr tctxt;
+ xmlURIPtr uri;
+ const xmlChar *url;
+
tctxt = xsltXPathGetTransformContext(ctxt);
+
+ url = obj->stringval;
+
+ uri = xmlParseURI((const char *) url);
+ if (uri == NULL) {
+ xsltTransformError(tctxt, NULL, NULL,
+ "document() : failed to parse URI '%s'\n", url);
+ valuePush(ctxt, xmlXPathNewNodeSet(NULL));
+ goto error;
+ }
+
+ /*
+ * check for and remove fragment identifier
+ */
+ fragment = (xmlChar *)uri->fragment;
+ if (fragment != NULL) {
+ uri->fragment = NULL;
+ newURI = xmlSaveUri(uri);
+ url = newURI;
+ }
+ xmlFreeURI(uri);
+
if ((obj2 != NULL) && (obj2->nodesetval != NULL) &&
(obj2->nodesetval->nodeNr > 0) &&
IS_XSLT_REAL_NODE(obj2->nodesetval->nodeTab[0])) {
@@ -313,7 +318,8 @@ xsltDocumentFunction(xmlXPathParserContextPtr ctxt, int nargs)
(xmlNodePtr) tctxt->style->doc);
}
}
- URI = xmlBuildURI(obj->stringval, base);
+
+ URI = xmlBuildURI(url, base);
if (base != NULL)
xmlFree(base);
if (URI == NULL) {
@@ -326,10 +332,14 @@ xsltDocumentFunction(xmlXPathParserContextPtr ctxt, int nargs)
valuePush(ctxt, xmlXPathNewNodeSet(NULL));
}
} else {
- xsltDocumentFunctionLoadDocument( ctxt, URI );
+ xsltDocumentFunctionLoadDocument(ctxt, URI, fragment);
xmlFree(URI);
}
}
+
+error:
+ xmlFree(newURI);
+ xmlFree(fragment);
xmlXPathFreeObject(obj);
if (obj2 != NULL)
xmlXPathFreeObject(obj2);
diff --git a/libxslt/security.c b/libxslt/security.c
index 001ee6e4..3a66594f 100644
--- a/libxslt/security.c
+++ b/libxslt/security.c
@@ -422,6 +422,19 @@ xsltCheckRead(xsltSecurityPrefsPtr sec,
xmlURIPtr uri;
xsltSecurityCheck check;
+ if (xmlStrstr(URL, BAD_CAST "://") == NULL) {
+ check = xsltGetSecurityPrefs(sec, XSLT_SECPREF_READ_FILE);
+ if (check != NULL) {
+ ret = check(sec, ctxt, (const char *) URL);
+ if (ret == 0) {
+ xsltTransformError(ctxt, NULL, NULL,
+ "Local file read for %s refused\n", URL);
+ return(0);
+ }
+ }
+ return(1);
+ }
+
uri = xmlParseURI((const char *)URL);
if (uri == NULL) {
xsltTransformError(ctxt, NULL, NULL,
--
GitLab
|