Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-8891: xml link validator in rich text does not allow to use "tel:" links #244

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions src/lib/eZ/RichText/Resources/schemas/docbook/docbook.iso.sch
Original file line number Diff line number Diff line change
Expand Up @@ -249,24 +249,8 @@
</s:rule>
</s:pattern>
<s:pattern name="Element contents validation">
<s:let name="upperCase" value="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<s:let name="lowerCase" value="'abcdefghijklmnopqrstuvwxyz'"/>
<s:rule context="db:link">
<!-- Using translate() because we don't have XPath 2, so we can't use lower-case(). -->
<s:assert test="starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'http://') or
starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'https://') or
starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'mailto:') or
starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezcontent://') or
starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezlocation://') or
starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezremote://') or
starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezurl://') or
starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), '/') or
starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), '#')"
mode="schematron-get-full-path-2">links must start with one of: http://, https://, mailto:, ezcontent://, ezlocation://, ezremote://, ezurl://, /, #</s:assert>
<s:assert test="not(contains(@*[name()='xlink:href'], '&lt;') or
contains(@*[name()='xlink:href'], '&gt;') or
contains(@*[name()='xlink:href'], '&quot;'))"
mode="schematron-get-full-path-2">using characters [&lt; &gt; &quot;] in links is not allowed</s:assert>
<s:assert test="not(contains(@*[name()='xlink:href'], 'javascript:') or contains(@*[name()='xlink:href'], 'vbscript:'))" mode="schematron-get-full-path-2">using scripts in links is not allowed</s:assert>
</s:rule>
</s:pattern>
</s:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,10 @@
<axsl:choose><axsl:when test="not(.//db:ezlink)"/><axsl:otherwise><svrl:failed-assert xmlns:svrl="http://purl.oclc.org/dsdl/svrl" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:schold="http://www.ascc.net/xml/schematron" test="not(.//db:ezlink)"><axsl:attribute name="location"><axsl:apply-templates select="." mode="schematron-get-full-path"/></axsl:attribute><svrl:text>ezlink must not occur in the descendants of link</svrl:text></svrl:failed-assert></axsl:otherwise></axsl:choose><axsl:apply-templates select="*|comment()|processing-instruction()" mode="M2"/></axsl:template><axsl:template match="text()" priority="-1" mode="M2"/><axsl:template match="@*|node()" priority="-2" mode="M2"><axsl:apply-templates select="*|comment()|processing-instruction()" mode="M2"/></axsl:template>

<!--PATTERN -->
<axsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/><axsl:variable name="lowerCase" select="'abcdefghijklmnopqrstuvwxyz'"/>


<!--RULE -->
<axsl:template match="db:link" priority="1000" mode="M3"><svrl:fired-rule xmlns:svrl="http://purl.oclc.org/dsdl/svrl" context="db:link"/>

<!--ASSERT -->
<axsl:choose><axsl:when test="starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'http://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'https://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'mailto:') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezcontent://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezlocation://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezremote://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezurl://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), '/') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), '#')"/><axsl:otherwise><svrl:failed-assert xmlns:svrl="http://purl.oclc.org/dsdl/svrl" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:schold="http://www.ascc.net/xml/schematron" test="starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'http://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'https://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'mailto:') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezcontent://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezlocation://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezremote://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), 'ezurl://') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), '/') or starts-with(translate(@*[name()='xlink:href'], $upperCase, $lowerCase), '#')"><axsl:attribute name="location"><axsl:apply-templates select="." mode="schematron-get-full-path-2"/></axsl:attribute><svrl:text>links must start with one of: http://, https://, mailto:, ezcontent://, ezlocation://, ezremote://, ezurl://, /, #</svrl:text></svrl:failed-assert></axsl:otherwise></axsl:choose>

<!--ASSERT -->
<axsl:choose><axsl:when test="not(contains(@*[name()='xlink:href'], '&lt;') or contains(@*[name()='xlink:href'], '&gt;') or contains(@*[name()='xlink:href'], '&quot;'))"/><axsl:otherwise><svrl:failed-assert xmlns:svrl="http://purl.oclc.org/dsdl/svrl" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:schold="http://www.ascc.net/xml/schematron" test="not(contains(@*[name()='xlink:href'], '&lt;') or contains(@*[name()='xlink:href'], '&gt;') or contains(@*[name()='xlink:href'], '&quot;'))"><axsl:attribute name="location"><axsl:apply-templates select="." mode="schematron-get-full-path-2"/></axsl:attribute><svrl:text>using characters [&lt; &gt; "] in links is not allowed</svrl:text></svrl:failed-assert></axsl:otherwise></axsl:choose><axsl:apply-templates select="*|comment()|processing-instruction()" mode="M3"/></axsl:template><axsl:template match="text()" priority="-1" mode="M3"/><axsl:template match="@*|node()" priority="-2" mode="M3"><axsl:apply-templates select="*|comment()|processing-instruction()" mode="M3"/></axsl:template></axsl:stylesheet>
<axsl:choose><axsl:when test="not(contains(@*[name()='xlink:href'], 'javascript:') or contains(@*[name()='xlink:href'], 'vbscript:'))"/><axsl:otherwise><svrl:failed-assert xmlns:svrl="http://purl.oclc.org/dsdl/svrl" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:schold="http://www.ascc.net/xml/schematron" test="not(contains(@*[name()='xlink:href'], 'javascript:') or contains(@*[name()='xlink:href'], 'vbscript:'))"><axsl:attribute name="location"><axsl:apply-templates select="." mode="schematron-get-full-path-2"/></axsl:attribute><svrl:text>using scripts in links is not allowed</svrl:text></svrl:failed-assert></axsl:otherwise></axsl:choose><axsl:apply-templates select="*|comment()|processing-instruction()" mode="M3"/></axsl:template><axsl:template match="text()" priority="-1" mode="M3"/><axsl:template match="@*|node()" priority="-2" mode="M3"><axsl:apply-templates select="*|comment()|processing-instruction()" mode="M3"/></axsl:template></axsl:stylesheet>
11 changes: 11 additions & 0 deletions src/lib/eZ/RichText/Resources/schemas/docbook/ezpublish.rng
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom"
xmlns:a="http://ez.no/xmlns/annotation"
xmlns:m="http://ez.no/xmlns/module"
xmlns:xlink="http://www.w3.org/1999/xlink"
ns="http://docbook.org/ns/docbook"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">

Expand Down Expand Up @@ -117,6 +118,16 @@
</choice>
</interleave>
</define>
<define name="db.xlink.href.attribute">
<attribute name="xlink:href">
<a:documentation>Identifies a link target with a URI</a:documentation>
<choice>
<data type="string">
<param name="pattern">(https?://|mailto:|ezcontent://|ezlocation://|ezremote://|ezurl://|tel:|/|#).+</param>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original fix for ibexa-sa-2024-005-persistent-xss-in-richtext also checks for the existence of &lt;, &gt; and '&quot;

In order to check for this in the reg-ex, I need to use Negative Lookahead, but adding (?!foobar) in the regex is not accepted by the schema validator, ie:
<param name="pattern">(?!.*foobar)(https?://|mailto:|ezcontent://|ezlocation://|ezremote://|ezurl://|tel:|/|#).+</param>-

It returns erros like:

Validation of XML content failed: Fatal error in 0:0: failed to compile: xmlFAParseAtom: expecting ')'
Validation of XML content failed: Fatal error in 0:0: failed to compile: xmlFAParseRegExp: extra characters

regex engine do not support lookahead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for &lt;, &gt; and '&quot; is uneeded as urls are correctly URL encoded when sent to front-end anyway

</data>
</choice>
</attribute>
</define>
<define name="db.abbrev"><notAllowed/></define>
<define name="db.abstract"><notAllowed/></define>
<define name="db.accel"><notAllowed/></define>
Expand Down
20 changes: 17 additions & 3 deletions tests/lib/eZ/FieldType/RichTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,14 @@ public function providerForTestValidate()
[
new ValidationError(
"Validation of XML content failed:\n" .
'/section/para/link: links must start with one of: http://, https://, mailto:, ezcontent://, ezlocation://, ezremote://, ezurl://, /, #',
'Error in 3:0: Element section has extra content: para',
null,
[],
'xml'
),
new ValidationError(
"Validation of XML content failed:\n" .
'/section/para/link: using scripts in links is not allowed',
null,
[],
'xml'
Expand All @@ -239,7 +246,7 @@ public function providerForTestValidate()
[
new ValidationError(
"Validation of XML content failed:\n" .
'/section/para/link: links must start with one of: http://, https://, mailto:, ezcontent://, ezlocation://, ezremote://, ezurl://, /, #',
'Error in 3:0: Element section has extra content: para',
null,
[],
'xml'
Expand Down Expand Up @@ -269,7 +276,14 @@ public function providerForTestValidate()
[
new ValidationError(
"Validation of XML content failed:\n" .
'/section/para/link: links must start with one of: http://, https://, mailto:, ezcontent://, ezlocation://, ezremote://, ezurl://, /, #',
'Error in 3:0: Element section has extra content: para',
null,
[],
'xml'
),
new ValidationError(
"Validation of XML content failed:\n" .
'/section/para/link: using scripts in links is not allowed',
null,
[],
'xml'
Expand Down
Loading