Skip to content

Commit

Permalink
parser: Allow to disable catalogs with parser options
Browse files Browse the repository at this point in the history
Implement XML_PARSE_NO_SYS_CATALOG and XML_PARSE_NO_CATALOG_PI.

Fixes #735.
  • Loading branch information
nwellnhof committed Jul 2, 2024
1 parent 6794c1b commit 606f410
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
6 changes: 6 additions & 0 deletions catalog.c
Original file line number Diff line number Diff line change
Expand Up @@ -3409,6 +3409,9 @@ xmlCatalogConvert(void) {
/**
* xmlCatalogGetDefaults:
*
* DEPRECATED: Use XML_PARSE_NO_SYS_CATALOG and
* XML_PARSE_NO_CATALOG_PI.
*
* Used to get the user preference w.r.t. to what catalogs should
* be accepted
*
Expand All @@ -3423,6 +3426,9 @@ xmlCatalogGetDefaults(void) {
* xmlCatalogSetDefaults:
* @allow: what catalogs should be accepted
*
* DEPRECATED: Use XML_PARSE_NO_SYS_CATALOG and
* XML_PARSE_NO_CATALOG_PI.
*
* Used to set the user preference w.r.t. to what catalogs should
* be accepted
*/
Expand Down
4 changes: 3 additions & 1 deletion include/libxml/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,9 @@ typedef enum {
/* since 2.13.0 */
XML_PARSE_NO_XXE = 1<<23,/* disable loading of external content */
/* since 2.14.0 */
XML_PARSE_NO_UNZIP = 1<<24 /* disable compressed content */
XML_PARSE_NO_UNZIP = 1<<24,/* disable compressed content */
XML_PARSE_NO_SYS_CATALOG = 1<<25,/* disable global system catalog */
XML_PARSE_NO_CATALOG_PI = 1<<26 /* ignore catalog PIs */
} xmlParserOption;

XMLPUBFUN void
Expand Down
19 changes: 16 additions & 3 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -5536,13 +5536,14 @@ xmlParsePI(xmlParserCtxtPtr ctxt) {
if ((ctxt->inSubset == 0) &&
(xmlStrEqual(target, XML_CATALOG_PI))) {
xmlCatalogAllow allow = xmlCatalogGetDefaults();
if ((allow == XML_CATA_ALLOW_DOCUMENT) ||
(allow == XML_CATA_ALLOW_ALL))

if (((ctxt->options & XML_PARSE_NO_CATALOG_PI) == 0) &&
((allow == XML_CATA_ALLOW_DOCUMENT) ||
(allow == XML_CATA_ALLOW_ALL)))
xmlParseCatalogPI(ctxt, buf);
}
#endif


/*
* SAX: PI detected.
*/
Expand Down Expand Up @@ -13619,6 +13620,18 @@ xmlCtxtSetOptionsInternal(xmlParserCtxtPtr ctxt, int options, int keepMask)
*
* Available since 2.14.0.
*
* XML_PARSE_NO_SYS_CATALOG
*
* Disables the global system XML catalog.
*
* Available since 2.14.0.
*
* XML_PARSE_NO_CATALOG_PI
*
* Ignore XML catalog processing instructions.
*
* Available since 2.14.0.
*
* Returns 0 in case of success, the set of unknown or unimplemented options
* in case of error.
*/
Expand Down
29 changes: 17 additions & 12 deletions parserInternals.c
Original file line number Diff line number Diff line change
Expand Up @@ -2199,30 +2199,39 @@ xmlResolveResourceFromCatalog(const char *URL, const char *ID,
xmlParserCtxtPtr ctxt) {
xmlChar *resource = NULL;
xmlCatalogAllow pref;
int allowLocal = 0;
int allowGlobal = 0;

/*
* If the resource doesn't exists as a file,
* try to load it from the resource pointed in the catalogs
*/
pref = xmlCatalogGetDefaults();

if ((ctxt != NULL) && (ctxt->catalogs != NULL) &&
((pref == XML_CATA_ALLOW_ALL) ||
(pref == XML_CATA_ALLOW_DOCUMENT)))
allowLocal = 1;

if (((ctxt == NULL) ||
((ctxt->options & XML_PARSE_NO_SYS_CATALOG) == 0)) &&
((pref == XML_CATA_ALLOW_ALL) ||
(pref == XML_CATA_ALLOW_GLOBAL)))
allowGlobal = 1;

if ((pref != XML_CATA_ALLOW_NONE) && (!xmlNoNetExists(URL))) {
/*
* Do a local lookup
*/
if ((ctxt != NULL) && (ctxt->catalogs != NULL) &&
((pref == XML_CATA_ALLOW_ALL) ||
(pref == XML_CATA_ALLOW_DOCUMENT))) {
if (allowLocal) {
resource = xmlCatalogLocalResolve(ctxt->catalogs,
(const xmlChar *)ID,
(const xmlChar *)URL);
}
/*
* Try a global lookup
*/
if ((resource == NULL) &&
((pref == XML_CATA_ALLOW_ALL) ||
(pref == XML_CATA_ALLOW_GLOBAL))) {
if ((resource == NULL) && (allowGlobal)) {
resource = xmlCatalogResolve((const xmlChar *)ID,
(const xmlChar *)URL);
}
Expand All @@ -2235,14 +2244,10 @@ xmlResolveResourceFromCatalog(const char *URL, const char *ID,
if ((resource != NULL) && (!xmlNoNetExists((const char *)resource))) {
xmlChar *tmp = NULL;

if ((ctxt != NULL) && (ctxt->catalogs != NULL) &&
((pref == XML_CATA_ALLOW_ALL) ||
(pref == XML_CATA_ALLOW_DOCUMENT))) {
if (allowLocal) {
tmp = xmlCatalogLocalResolveURI(ctxt->catalogs, resource);
}
if ((tmp == NULL) &&
((pref == XML_CATA_ALLOW_ALL) ||
(pref == XML_CATA_ALLOW_GLOBAL))) {
if ((tmp == NULL) && (allowGlobal)) {
tmp = xmlCatalogResolveURI(resource);
}

Expand Down

0 comments on commit 606f410

Please sign in to comment.