169 Commits

Author SHA1 Message Date
Nick Wellnhofer
4a0d74dcc3 SAX2: Reenable 'directory' as base URI fallback
Apparently, some users overwrite this member manually to set a base URI
for memory streams.

Fixes #753.
2024-07-03 12:19:07 +02:00
Nick Wellnhofer
2f6766dba6 valid: Restore ID lookup
Revert a change from d025cfbb and don't overwrite ID table entries, so
that the first attribute will be returned if there are duplicate IDs.

This requires two other changes:

- Attributes in entity content are never added to the ID table. This
  seems reasonable.

- Remove the optimization to skip ID lookup when copying and the target
  document has an empty ID table. This also seems more correct since the
  document could have ID declarations nevertheless or we could be
  copying xml:ids into the document for the first time.

Fixes #757.
2024-07-03 12:19:00 +02:00
Nick Wellnhofer
4ff2dccf9f SAX2: Warn if URI resolution failed 2024-05-13 12:50:08 +02:00
Nick Wellnhofer
71a7a33e18 parser: Fix base URI of internal parameter entities
Search parent inputs of internal parameter entities for base URI.

Fixes a long-standing bug, which manifested in a different way after
commit 955c177f. Reproduce with

    xmllint --noent xmlconf/eduni/errata-2e/E18.xml
2024-05-03 11:53:45 +02:00
Nick Wellnhofer
af2bda4e87 SAX2: Also check URI length before resolving
We don't want to exceed the size limit of 1 MB in uri.c. Such errors
can't be distinguished from malloc failures.
2024-04-05 13:09:45 +02:00
Nick Wellnhofer
2cc7f71016 SAX2: Fix xmlSAX2EntityDecl with empty base
Short-lived regression.
2024-03-29 13:44:28 +01:00
Nick Wellnhofer
730de88b16 SAX2: Optimize appending children
xmlSAX2AppendChild can make several assumptions which make appending
nodes more efficient. Also handle line numbers in xmlSAX2AppendChild.
2024-03-29 12:59:20 +01:00
Nick Wellnhofer
05c147c3ef SAX2: Report malloc failure in xmlSAX2AttributeNs 2024-03-22 13:03:37 +01:00
Nick Wellnhofer
6a49bb777c tree: Introduce xmlSearchNsSafe
After the failed experiment with a static XML namespace, introduce
versions of xmlSearchNs that report malloc failures.

Optimize the no-document case by only adding the XML namespace
declaration if it wasn't found in an ancestor.
2024-03-17 21:07:46 +01:00
Nick Wellnhofer
047ea3ecb3 Revert "tree: Allocate XML namespace statically"
This reverts commit 2840e33c5e4b51589a0b96e8102638eeaea6df72.
2024-03-17 21:04:40 +01:00
Nick Wellnhofer
9f049afa6d tree: Refactor element creation and parsing of attribute values
Replace xmlStringGetNodeList and xmlStringLenGetNodeList with
xmlNodeParseContentInternal which also updates an optional parent
node.

Don't look up entities a second time via xmlNewReference.
2024-03-15 19:54:26 +01:00
Nick Wellnhofer
2840e33c5e tree: Allocate XML namespace statically 2024-03-15 19:47:07 +01:00
Nick Wellnhofer
84a71860a8 xmlreader: Fix xmlTextReaderConstEncoding
Regression from commit f1c1f5c6.

Fixes #697.
2024-02-26 15:33:06 +01:00
Nick Wellnhofer
7dc8600a7f SAX2: Report malloc failure in xmlCheckDefaultedAttributes 2024-02-20 12:32:17 +01:00
Nick Wellnhofer
2e19d0ef04 SAX2: Make sure that OOM errors aren't overwritten 2024-01-26 11:39:51 +01:00
Nick Wellnhofer
57c687592f SAX2: Limit entity URI length to 2000 bytes
Avoid quadratic behavior when loading entities with long URIs multiple
times.

This limitation could be dropped if we cached external entities.
2024-01-10 15:58:23 +01:00
Nick Wellnhofer
02cc5c3609 parser: Add XML_PARSE_NO_XXE parser option 2024-01-05 20:39:40 +01:00
Nick Wellnhofer
9912c36904 SAX2: Enforce size limit in xmlSAX2Text with XML_PARSE_HUGE 2024-01-02 19:48:23 +01:00
Nick Wellnhofer
37c6618be5 parser: Rework parsing of attribute and entity values
Don't use a separate function to handle "complex" attributes. Validate
UTF-8 byte sequences without decoding. This should improve performance
considerably when parsing multi-byte UTF-8 sequences.

Use a string buffer to avoid unnecessary allocations and copying when
expanding entities.

Normalize attribute values in a single pass while expanding entities.

Be more lenient in recovery mode.

If no entity substitution was requested, validate entities without
expanding. Fixes #596.

Also fixes #655.
2024-01-02 15:42:03 +01:00
Nick Wellnhofer
6a9a88a17f parser: Move progressive flag into input struct 2023-12-29 01:20:08 +01:00
Nick Wellnhofer
d944a41515 parser: Fix in-parameter-entity and in-external-dtd checks
Use in ctxt->input->entity instead of ctxt->inputNr to determine whether
we are inside a parameter entity.

Stop using ctxt->external to check whether we're in an external DTD.
This is signaled by ctxt->inSubset == 2.
2023-12-29 01:19:56 +01:00
Nick Wellnhofer
5f319304c8 SAX2: Fix error code
Today I learned that the TSCII character encoding [1] can blow up the
size of text 12 times when converted to UTF-8:

    $ printf '\x82' |iconv -f TSCII -t UTF-8 |hexdump -C
    00000000  e0 ae b8 e0 af 8d e0 ae  b0 e0 af 80
    0000000c

[1] https://en.wikipedia.org/wiki/Tamil_Script_Code_for_Information_Interchange
2023-12-28 19:43:48 +01:00
Nick Wellnhofer
955c177f69 parser: Stop using 'directory' struct member
This was only used as a pointless fallback for URI resolution.
2023-12-25 23:38:40 +01:00
Nick Wellnhofer
130436917c parser: Rename xmlErrParser to xmlCtxtErr 2023-12-21 15:02:24 +01:00
Nick Wellnhofer
54c70ed57f parser: Improve error handling
Introduce xmlCtxtSetErrorHandler allowing to set a structured error for
a parser context. There already was the "serror" SAX handler but this
always receives the parser context as argument.

Start to use xmlRaiseMemoryError.

Remove useless arguments from memory error functions. Rename
xmlErrMemory to xmlCtxtErrMemory.

Remove a few calls to xmlGenericError.

Remove support for runtime entity debugging.
2023-12-21 02:46:27 +01:00
Nick Wellnhofer
e58ea29f17 SAX2: Report malloc failures
Fix many places where malloc failures aren't reported.

Improve error handling when parsing entity declarations.

Fixes #308.
2023-12-11 22:13:05 +01:00
Nick Wellnhofer
7f00273cf0 parser: Fix invalid free in xmlParseBalancedChunkMemoryRecover
Set the dictionary for newDoc in xmlParseBalancedChunkMemoryRecover.
This is a long-standing bug which was masked by

- xmlParseBalancedChunkMemoryRecover changing the document of the root
  node. This is a really bad idea, resulting in a mismatch between
  ctxt->myDoc and ctxt->node->doc.
- SAX2.c preferring ctxt->node->doc over ctxt->myDoc until commit
  a31e1b06.

Fixes #641.
2023-12-01 19:44:37 +01:00
Nick Wellnhofer
a31e1b0665 SAX2: Fix quadratic behavior in xmlSAX2AttributeNs
The last missing piece to make parsing of attributes O(n).
2023-11-04 20:21:54 +01:00
Nick Wellnhofer
e0dd330b8f parser: Use hash tables to avoid quadratic behavior
Use a hash table to lookup namespaces by prefix. The hash table stores
an index into the namespace table. Auxiliary data for namespaces is
stored in a separate array along the main namespace table.

Use a hash table to verify attribute uniqueness. The hash table stores
an index into the attribute table.

Reuse hash value from the dictionary to avoid computing them twice.

See #346.
2023-09-29 12:43:22 +02:00
Nick Wellnhofer
da274bfa55 build: Fix build when certain modules are disabled 2023-09-21 02:26:43 +02:00
Nick Wellnhofer
9b5cce7a71 include: Remove more unnecessary includes 2023-09-21 01:50:53 +02:00
Nick Wellnhofer
699299cae3 globals: Stop including globals.h 2023-09-20 22:07:40 +02:00
Nick Wellnhofer
a77f9ab84c globals: Don't include SAX2.h from globals.h 2023-09-20 22:06:49 +02:00
Nick Wellnhofer
4e1c13ebfd debug: Remove debugging code
This is barely useful these days and only clutters the code base.
2023-09-19 17:35:09 +02:00
Nick Wellnhofer
cde4499778 SAX2: Allow multiple top-level elements
When parsing with HTML_PARSE_NOIMPLIED, the result document can contain
multiple top-level elements. Rework xmlSAX2StartElement to simply add
the element as a child of ctxt->node or ctxt->myDoc.

Don't invoke xmlAddSibling for non-element parents. The context node
should always be an element node.

Fixes #584.
2023-08-27 16:35:23 +02:00
Nick Wellnhofer
f1c1f5c6b4 parser: Revert change to doc->encoding
Fixes #579.
2023-08-17 12:47:14 +02:00
Nick Wellnhofer
cb717d7e02 parser: Update line number after coalescing text nodes
This should make the line number of text nodes deterministic. Before,
it depended on the callback sequence which depends on the size of chunks
fed to the parser.
2023-08-09 16:58:33 +02:00
Nick Wellnhofer
ec7be50662 parser: Rework encoding detection
Introduce XML_INPUT_HAS_ENCODING flag for xmlParserInput which is set
when xmlSwitchEncoding is called. The parser can use the flag to
reliably detect whether an encoding was already set via user override,
BOM or other auto-detection. In this case, the encoding declaration
won't be used to switch the encoding.

Before, an inscrutable mix of ctxt->charset, ctxt->input->encoding
and ctxt->input->buf->encoder was used.

Introduce private helper functions to switch encodings used by both the
XML and HTML parser:

- xmlDetectEncoding which skips over the BOM, allowing to remove the
  BOM checks from other encoding functions.
- xmlSetDeclaredEncoding, replacing htmlCheckEncodingDirect, which warns
  about encoding mismatches.

If users override the encoding, store the declared instead of the actual
encoding in xmlDoc. In this case, the actual encoding is known and the
raw value from the doc is more useful.

Also use the input flags to store the ISO-8859-1 fallback state.
Restrict the fallback to cases where no encoding was specified. (The
fallback is only useful in recovery mode and these days broken UTF-8 is
probably more likely than ISO-8859-1, so it might eventually be removed
completely.)

The 'charset' member of xmlParserCtxt is now unused. The 'encoding'
member of xmlParserInput is now unused.

The 'standalone' member of xmlParserInput is renamed to 'flags'.

A new parser state XML_PARSER_XML_DECL is added for the push parser.
2023-08-08 15:19:46 +02:00
Nick Wellnhofer
d38e73f91e parser: Always create UTF-8 in xmlParseReference
It seems that this code path could only be triggered after an encoding
error in recovery mode. Creating char-ref nodes is unnecessary and
typically unexpected.
2023-08-08 15:19:44 +02:00
Nick Wellnhofer
b8961df65d SAX: Always validate xml:ids
The behavior shouldn't depend on mostly random configuration options.
2023-05-09 03:25:24 +02:00
Nick Wellnhofer
235b15a590 SAX: Always initialize SAX1 element handlers
Follow-up to commit d0c3f01e. A parser context will be initialized to
SAX version 2, but this can be overridden with XML_PARSE_SAX1 later,
so we must initialize the SAX1 element handlers as well.

Change the check in xmlDetectSAX2 to only look for XML_SAX2_MAGIC, so
we don't switch to SAX1 if the SAX2 element handlers are NULL.
2023-05-08 19:15:44 +02:00
Nick Wellnhofer
250faf3c83 parser: Fix regression in xmlParserNodeInfo accounting
Commit 62150ed2 broke begin_pos and begin_line when extra node info was
recorded.

Fixes #523.
2023-04-20 15:38:00 +02:00
Nick Wellnhofer
d7d0bc6581 SAX2: Ignore namespaces in HTML documents
In commit 21ca8829, we started to ignore namespaces in HTML element
names but we still called xmlSplitQName, effectively stripping the
namespace prefix. This would cause elements like <o:p> being parsed
as <p>. Now we leave the name untouched.

Fixes #508.
2023-03-31 17:08:43 +02:00
Nick Wellnhofer
cb4334b7ab malloc-fail: Fix memory leak in xmlSAX2StartElementNs
Found with libFuzzer, see #344.
2023-02-17 17:16:51 +01:00
Nick Wellnhofer
0c5f40b788 malloc-fail: Fix null deref in xmlSAX2AttributeInternal
Found with libFuzzer, see #344.
2023-01-24 11:32:15 +01:00
Nick Wellnhofer
b3b53dcce4 malloc-fail: Fix null deref in xmlSAX2Text
Found with libFuzzer, see #344.
2023-01-24 11:32:15 +01:00
Nick Wellnhofer
463bbeeca1 entities: Rework entity amplification checks
This commit implements robust detection of entity amplification attacks,
better known as the "billion laughs" attack.

We now limit the size of the document after substitution of entities to
10 times the size before expansion. This guarantees linear behavior by
definition. There already was a similar check before, but the accounting
of "sizeentities" (size of external entities) and "sizeentcopy" (size of
all copies created by entity references) wasn't accurate.

We also need saturation arithmetic since we're historically limited to
"unsigned long" which is 32-bit on many platforms.

A maximum of 10 MB of substitutions is always allowed. This should make
use cases like DITA work which have caused problems in the past.

The old checks based on the number of entities were removed. This is
accounted for by adding a fixed cost to each entity reference.

Entity amplification checks are now enabled even if XML_PARSE_HUGE is
set. This option is mainly used to allow larger text nodes. Most users
were unaware that it also disabled entity expansion checks.

Some of the limits might be adjusted later. If this change turns out to
affect legitimate use cases, we can add a separate parser option to
disable the checks.

Fixes #294.
Fixes #345.
2022-12-21 20:19:10 +01:00
Nick Wellnhofer
cecd364dd2 parser: Don't call *DefaultSAXHandlerInit from xmlInitParser
Change the default handler definitions to match the result after calling
the initialization functions.

This makes sure that no thread-local variables are accessed when calling
xmlInitParser.
2022-11-25 15:02:04 +01:00
Nick Wellnhofer
68a6518c45 parser: Rewrite push parser boundary checks
Remove inaccurate xmlParseCheckTransition check.

Remove non-incremental xmlParseGetLasts check.

Add functions that check for several boundary constructs more
accurately, keeping track of progress in ctxt->checkIndex.

Fixes #439.
2022-11-20 21:27:08 +01:00
Nick Wellnhofer
7ceaee9430 malloc-fail: Fix memory leak in xmlSAX2ExternalSubset
Found with libFuzzer, see #344.
2022-11-02 16:05:05 +01:00