Skip to content

Commit

Permalink
Updates to handle entity resolution for libxml2-2.13.?
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Jan 15, 2025
1 parent da9dc23 commit b5e0ef1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 108 deletions.
9 changes: 5 additions & 4 deletions Headers/GNUstepBase/GSXML.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,11 @@ GS_EXPORT_CLASS
GS_EXPORT_CLASS
@interface GSXMLParser : NSObject
{
id src; /* source for parsing */
void *lib; /* parser context */
GSSAXHandler *saxHandler; /* handler for parsing */
NSMutableString *messages; /* append messages here */
id src; /* source for parsing */
void *lib; /* parser context */
GSSAXHandler *saxHandler; /* handler for parsing */
NSMutableString *messages; /* append messages here */
BOOL resolve;
}
+ (NSString*) loadEntity: (NSString*)publicId at: (NSString*)location;
+ (GSXMLParser*) parser;
Expand Down
158 changes: 68 additions & 90 deletions Source/Additions/GSXML.m
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,10 @@ static int xmlNSInputStreamCloseCallback (void *context)
loadEntityFunction(const unsigned char *url, const unsigned char *eid,
void *ctx);
static xmlParserInputPtr
resolveEntityFunction(void *ctx, const unsigned char *eid,
const unsigned char *url);
static xmlEntityPtr
getEntityIgnoreExternal(void *ctx, const xmlChar *name);
resolveEntityFunction(void *ctx,
const unsigned char *eid, const unsigned char *url);
static xmlEntityPtr
getEntityResolveExternal(void *ctx, const xmlChar *name);
getEntityDefault(void *ctx, const xmlChar *name);

@interface GSXPathObject(Private)
+ (id) _newWithNativePointer: (xmlXPathObject *)lib
Expand All @@ -234,6 +232,7 @@ @interface GSXMLParser (Private)
- (BOOL) _initLibXML;
- (NSMutableString*) _messages;
- (void) _parseChunk: (NSData*)data;
- (BOOL) _resolves;
@end

@interface GSSAXHandler (Private)
Expand Down Expand Up @@ -2402,28 +2401,9 @@ - (void) saveMessages: (BOOL)yesno

- (BOOL) resolveEntities: (BOOL)yesno
{
BOOL old;
BOOL old = resolve;;

if (yesno) yesno = YES;
if ((((xmlParserCtxtPtr)lib)->sax)->getEntity
== (void*)getEntityIgnoreExternal)
{
old = NO;
}
else
{
old = YES;
}
if (YES == yesno)
{
(((xmlParserCtxtPtr)lib)->sax)->getEntity
= (void*)getEntityResolveExternal;
}
else
{
(((xmlParserCtxtPtr)lib)->sax)->getEntity
= (void*)getEntityIgnoreExternal;
}
resolve = yesno;
return old;
}

Expand All @@ -2434,14 +2414,11 @@ - (BOOL) resolveEntities: (BOOL)yesno
*/
- (BOOL) substituteEntities: (BOOL)yesno
{
BOOL old;
xmlParserCtxtPtr context = (xmlParserCtxtPtr)lib;
BOOL old;

if (yesno) yesno = YES;
old = (((xmlParserCtxtPtr)lib)->replaceEntities) ? YES : NO;
if (old != yesno)
{
((xmlParserCtxtPtr)lib)->replaceEntities = (yesno ? 1 : 0);
}
old = context->replaceEntities ? YES : NO;
context->replaceEntities = (yesno ? 1 : 0);

return old;
}
Expand Down Expand Up @@ -2494,16 +2471,15 @@ - (BOOL) _initLibXML
}
else
{
/*
* Put saxHandler address in _private member, so we can retrieve
/* Put saxHandler address in _private member, so we can retrieve
* the GSSAXHandler to use in our SAX C Functions.
*/
((xmlParserCtxtPtr)lib)->_private = saxHandler;

/*
* Set the entity loading function for this parser to be our one.
/* Set the entity loading function for this parser to be our one.
*/
((xmlParserCtxtPtr)lib)->sax->resolveEntity = resolveEntityFunction;
[self resolveEntities: NO]; // Off by default
}
return YES;
}
Expand All @@ -2523,6 +2499,10 @@ - (void) _parseChunk: (NSData*)data
xmlParseChunk(lib, [data bytes], [data length], data == nil);
}

- (BOOL) _resolves
{
return resolve;
}
@end

/**
Expand Down Expand Up @@ -2623,11 +2603,12 @@ + (void) initialize
#define HANDLER ((GSSAXHandler*)(((xmlParserCtxtPtr)ctx)->_private))

static xmlEntityPtr
getEntityDefault(void *ctx, const xmlChar *name, BOOL resolve)
getEntityDefault(void *ctx, const xmlChar *name)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlEntityPtr ret = NULL;

// NSLog(@"getEntityDefault called for '%s'", name);
if (ctx != 0)
{
if (0 == ctxt->inSubset)
Expand Down Expand Up @@ -2666,70 +2647,55 @@ + (void) initialize
else
{
ret = xmlGetDocEntity(ctxt->myDoc, name);
}
}
#if LIBXML_VERSION < 21300
/* In older versions we may need to parse the entity content.
*/
if ((ret != NULL)
&& ((ctxt->validate) || (ctxt->replaceEntities))
&& (ret->children == NULL)
&& (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))
{
if (YES == resolve)
{
xmlNodePtr children;
int val;

/*
* for validation purposes we really need to fetch and
* parse the external entity
*/
val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
ret->ExternalID, &children);
if (val == 0)
{
xmlAddChildList((xmlNodePtr) ret, children);
}
else
{
((((xmlParserCtxtPtr)ctxt)->sax)->fatalError)(ctxt,
"Failure to process entity %s\n", name);
xmlStopParser(ctxt);
ctxt->validate = 0;
return NULL;
}
ret->owner = 1;
xmlNodePtr children;
int val;

/*
* for validation purposes we really need to fetch and
* parse the external entity
*/
val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
ret->ExternalID, &children);
if (val == 0)
{
xmlAddChildList((xmlNodePtr) ret, children);
}
else
{
((((xmlParserCtxtPtr)ctxt)->sax)->fatalError)(ctxt,
"Failure to process entity %s\n", name);
xmlStopParser(ctxt);
ctxt->validate = 0;
return NULL;
}
ret->owner = 1;
#if LIBXML_VERSION < 21100
if (ret->checked == 0)
{
ret->checked = 1;
}
#endif
}
}
/* Set old flag ... not prent/needed in cewlater versions.
*/
if (ret->checked == 0)
{
ret->checked = 1;
}
#endif /* LIBXML_VERSION < 21100 */
}
#endif /* LIBXML_VERSION < 21300 */
}
return ret;
}

static xmlEntityPtr
getEntityIgnoreExternal(void *ctx, const xmlChar *name)
{
return getEntityDefault(ctx, name, NO);
}

static xmlEntityPtr
getEntityResolveExternal(void *ctx, const xmlChar *name)
{
return getEntityDefault(ctx, name, YES);
}

/* WARNING ... as far as I can tell libxml2 never uses the resolveEntity
* callback, so this function is never called via that route.
* We therefore also set this as the global default entity loading
* function (in [GSXMLParser+initialize] and [GSSAXHandler+initialize]).
*
* To implement the -resolveEntities method we must permit/deny any attempt
* to load an entity (before the function to resolve is even called),
* We therefore intercept the getEntity callback (using getEntityDefault()),
* re-implementing some of the code inside libxml2 to avoid attempts to
* load/parse external entities unless we have specifically enabled it.
*/
static xmlParserInputPtr
loadEntityFunction(const unsigned char *url,
Expand All @@ -2741,13 +2707,20 @@ + (void) initialize
NSString *location;
NSArray *components;
NSMutableString *local;
GSXMLParser *parser;
unsigned count;
unsigned index;

NSCAssert(ctx, @"No Context");
if (url == NULL)
return NULL;

parser = [HANDLER parser];
if (NO == [parser _resolves])
{
return xmlNewStringInputStream(ctx, (const xmlChar *)"");
}

entityId = (eid != NULL) ? (id)UTF8Str(eid) : nil;
location = UTF8Str(url);
components = [location pathComponents];
Expand Down Expand Up @@ -2942,6 +2915,7 @@ + (void) initialize
resolveEntityFunction(void *ctx,
const unsigned char *eid, const unsigned char *url)
{
NSLog(@"resolveEntityFunction called for %s %s", url, eid);
return loadEntityFunction(url, eid, ctx);
}

Expand Down Expand Up @@ -3729,7 +3703,7 @@ - (BOOL) _initLibXML
LIB->hasInternalSubset = (void*) hasInternalSubsetFunction;
LIB->hasExternalSubset = (void*) hasExternalSubsetFunction;
LIB->resolveEntity = (void*) resolveEntityFunction;
LIB->getEntity = (void*) getEntityIgnoreExternal;
LIB->getEntity = (void*) getEntityDefault;
LIB->entityDecl = (void*) entityDeclFunction;
LIB->notationDecl = (void*) notationDeclFunction;
LIB->attributeDecl = (void*) attributeDeclFunction;
Expand Down Expand Up @@ -3852,7 +3826,7 @@ - (BOOL) _initLibXML
SETCB(getEntity, getEntity:);
if (LIB->getEntity != getEntityFunction)
{
LIB->getEntity = getEntityIgnoreExternal;
LIB->getEntity = getEntityDefault;
}
SETCB(entityDecl, entityDecl:type:public:system:content:);
SETCB(notationDecl, notationDecl:public:system:);
Expand Down Expand Up @@ -3898,7 +3872,11 @@ - (BOOL) _initLibXML
}
else
{
#if LIBXML_VERSION < 21100
memcpy(lib, &htmlDefaultSAXHandler, sizeof(htmlSAXHandler));
#else
xmlSAX2InitHtmlDefaultSAXHandler(lib);
#endif

#define LIB ((htmlSAXHandlerPtr)lib)
LIB->internalSubset = (void*)internalSubsetFunction;
Expand Down
28 changes: 14 additions & 14 deletions Tests/base/GSXML/basic.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ int main()
NSMutableArray *oparams;
GSXMLNode *node;
GSXMLRPC *rpc;
NSString *xml;
NSString *str;
NSString *testPath;
NSString *absolutePath;
Expand Down Expand Up @@ -155,44 +156,43 @@ int main()
stringByAppendingPathComponent: @"GNUmakefile"];
absolutePath = [[NSURL fileURLWithPath: testPath] absoluteString];

str = [NSString stringWithFormat:
xml = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
@"<!DOCTYPE foo [\n"
@"<!ENTITY foo SYSTEM \"%@\">\n"
@"]>\n"
@"<file>&amp;&foo;&#65;</file>", absolutePath];
dat = [xml dataUsingEncoding: NSUTF8StringEncoding];

parser = [GSXMLParser parserWithData:
[str dataUsingEncoding: NSUTF8StringEncoding]];
parser = [GSXMLParser parserWithData: dat];
[parser substituteEntities: YES];
[parser parse];
PASS_EQUAL([[[parser document] root] content], @"&A",
"external entity is ignored")
str = [[[parser document] root] content];
PASS_EQUAL(str, @"&A", "external entity is ignored")

parser = [GSXMLParser parserWithData:
[str dataUsingEncoding: NSUTF8StringEncoding]];
parser = [GSXMLParser parserWithData: dat];
[parser substituteEntities: YES];
[parser resolveEntities: YES];
[parser parse];
str = [[[parser document] root] content];
PASS([str rangeOfString: @"MAKEFILES"].length > 0,
PASS(str != nil && [str rangeOfString: @"MAKEFILES"].length > 0,
"external entity is resolved")

str = @"<!DOCTYPE plist PUBLIC \"-//GNUstep//DTD plist 0.9//EN\""
xml = @"<!DOCTYPE plist PUBLIC \"-//GNUstep//DTD plist 0.9//EN\""
@" \"http://www.gnustep.org/plist-0_9.xml\">\n"
@"<plist></plist>";
parser = [GSXMLParser parserWithData:
[str dataUsingEncoding: NSUTF8StringEncoding]];
dat = [xml dataUsingEncoding: NSUTF8StringEncoding];
parser = [GSXMLParser parserWithData: dat];
[parser substituteEntities: YES];
[parser resolveEntities: YES];
[parser doValidityChecking: YES];
PASS([parser parse] == NO, "empty plist is not valid")

str = @"<!DOCTYPE plist PUBLIC \"-//GNUstep//DTD plist 0.9//EN\""
xml = @"<!DOCTYPE plist PUBLIC \"-//GNUstep//DTD plist 0.9//EN\""
@" \"http://www.gnustep.org/plist-0_9.xml\">\n"
@"<plist><string>xxx</string></plist>";
parser = [GSXMLParser parserWithData:
[str dataUsingEncoding: NSUTF8StringEncoding]];
dat = [xml dataUsingEncoding: NSUTF8StringEncoding];
parser = [GSXMLParser parserWithData: dat];
[parser substituteEntities: YES];
[parser resolveEntities: YES];
[parser doValidityChecking: YES];
Expand Down

0 comments on commit b5e0ef1

Please sign in to comment.