<?xml version="1.0" encoding="UTF-8"?>

<!-- [CS] updated by Chris 07/18/23 -->

<!-- [ST] formatted by Sarah 08/25/23 -->

<!DOCTYPE rfc [
  <!ENTITY nbsp    "&#160;">
  <!ENTITY zwsp   "&#8203;">
  <!ENTITY nbhy   "&#8209;">
  <!ENTITY wj     "&#8288;">
]>

<rfc xmlns:xi="http://www.w3.org/2001/XInclude" ipr="trust200902" docName="draft-ietf-jsonpath-iregexp-08" number="0000" submissionType="IETF" category="std" consensus="true" tocInclude="true" sortRefs="true" symRefs="true" updates="" obsoletes="" xml:lang="en" version="3">

  <front>
    <title abbrev="I-Regexp">I-Regexp: An Interoperable Regexp Format</title>
    <seriesInfo name="RFC" value="0000"/>
    <author initials="C." surname="Bormann" fullname="Carsten Bormann">
      <organization>Universität Bremen TZI</organization>
      <address>
        <postal>
          <street>Postfach 330440</street>
          <city>Bremen</city>
          <code>D-28359</code>
          <country>Germany</country>
        </postal>
        <phone>+49-421-218-63921</phone>
        <email>cabo@tzi.org</email>
      </address>
    </author>
    <author initials="T." surname="Bray" fullname="Tim Bray">
      <organization>Textuality</organization>
      <address>
        <postal>
          <country>Canada</country>
        </postal>
        <email>tbray@textuality.com</email>
      </address>
    </author>
    <date year="2023" month="June" day="23"/>
    <area>art</area>
    <workgroup>jsonpath</workgroup>

<!-- [rfced] Please insert any keywords (beyond those that appear in the title) for use on https://www.rfc-editor.org/search. -->

<keyword>example</keyword>

    <abstract>


<t>This document specifies I-Regexp, a flavor of regular expressions that is
limited in scope with the goal of interoperation across many different
regular-expression libraries.</t>
    </abstract>

  </front>
  <middle>

<section anchor="intro">
      <name>Introduction</name>
      <t>This specification describes an interoperable regular expression ("regexp") flavor, I-Regexp.</t>
      <t>I-Regexp does not provide advanced regular expression features such as capture groups, lookahead, or backreferences.
It supports only a Boolean matching capability, i.e., testing whether a given regular expression matches a given piece of text.</t>
      <t>I-Regexp supports the entire repertoire of Unicode characters (Unicode
scalar values); both the I-Regexp strings themselves and the strings
they are matched against are sequences of Unicode scalar values (often
represented in UTF-8 encoding form <xref target="RFC3629"/> for interchange).</t>
      <t>I-Regexp is a subset of XSD regular expressions <xref target="XSD-2"/>.</t>
      <t>This document includes guidance for converting I-Regexps for use with several well-known regular expression idioms.</t>
      <t>The development of I-Regexp was motivated by the work of the JSONPath Working Group. The Working Group wanted to include
in its specification <xref target="I-D.ietf-jsonpath-base"/> support for the use of regular expressions in JSONPath filters, but was unable to find a useful
specification for regular expressions which would be interoperable across the popular libraries.</t>
      <section anchor="terminology">
        <name>Terminology</name>
        <t>This document uses the abbreviation "regexp" for what are usually
called regular expressions in programming.
"I-Regexp" is used as a noun meaning a character string (sequence of
Unicode scalar values) that conforms to the requirements
in this specification; the plural is "I-Regexps".</t>
        <t>This specification uses Unicode terminology.
A good entry point into that is provided by <xref target="UNICODE-GLOSSARY"/>.</t>
        <t>
    The key words "<bcp14>MUST</bcp14>", "<bcp14>MUST NOT</bcp14>", "<bcp14>REQUIRED</bcp14>", "<bcp14>SHALL</bcp14>", "<bcp14>SHALL
    NOT</bcp14>", "<bcp14>SHOULD</bcp14>", "<bcp14>SHOULD NOT</bcp14>", "<bcp14>RECOMMENDED</bcp14>", "<bcp14>NOT RECOMMENDED</bcp14>",
    "<bcp14>MAY</bcp14>", and "<bcp14>OPTIONAL</bcp14>" in this document are to be interpreted as
    described in BCP&nbsp;14 <xref target="RFC2119"/> <xref target="RFC8174"/> 
    when, and only when, they appear in all capitals, as shown here.
        </t>

<t>The grammatical rules in this document are to be interpreted as ABNF,
as described in <xref target="RFC5234"/> and <xref target="RFC7405"/>, where the "characters" of
<xref section="2.3" sectionFormat="of" target="RFC5234"/> are Unicode scalar values.</t>
      </section>
    </section>
    <section anchor="objectives">
      <name>Objectives</name>
      <t>I-Regexps should handle the vast majority of practical cases where a
matching regexp is needed in a data model specification or a query
language expression.</t>
      <t>The editors of this document conducted a survey of the regexp syntax
used in published RFCs. All examples found there should be covered by I-Regexps,
both syntactically and with their intended semantics.
The exception is the use of multi-character escapes, for which
workaround guidance is provided in <xref target="mapping"/>.</t>
    </section>
    <section anchor="defn">
      <name>I-Regexp Syntax</name>
      <t>An I-Regexp <bcp14>MUST</bcp14> conform to the ABNF specification in
<xref target="iregexp-abnf"/>.</t>
      <figure anchor="iregexp-abnf">
        <name>I-Regexp Syntax in ABNF</name>
        <sourcecode type="abnf"><![CDATA[
i-regexp = branch *( "|" branch )
branch = *piece
piece = atom [ quantifier ]
quantifier = ( "*" / "+" / "?" ) / range-quantifier
range-quantifier = "{" QuantExact [ "," [ QuantExact ] ] "}"
QuantExact = 1*%x30-39 ; '0'-'9'

atom = NormalChar / charClass / ( "(" i-regexp ")" )
NormalChar = ( %x00-27 / "," / "-" / %x2F-3E ; '/'-'>'
 / %x40-5A ; '@'-'Z'
 / %x5E-7A ; '^'-'z'
 / %x7E-10FFFF )
charClass = "." / SingleCharEsc / charClassEsc / charClassExpr
SingleCharEsc = "\" ( %x28-2B ; '('-'+'
 / "-" / "." / "?" / %x5B-5E ; '['-'^'
 / %s"n" / %s"r" / %s"t" / %x7B-7D ; '{'-'}'
 )
charClassEsc = catEsc / complEsc
charClassExpr = "[" [ "^" ] ( "-" / CCE1 ) *CCE1 [ "-" ] "]"
CCE1 = ( CCchar [ "-" CCchar ] ) / charClassEsc
CCchar = ( %x00-2C / %x2E-5A ; '.'-'Z'
 / %x5E-10FFFF ) / SingleCharEsc
catEsc = %s"\p{" charProp "}"
complEsc = %s"\P{" charProp "}"
charProp = IsCategory
IsCategory = Letters / Marks / Numbers / Punctuation / Separators /
    Symbols / Others
Letters = %s"L" [ ( %s"l" / %s"m" / %s"o" / %s"t" / %s"u" ) ]
Marks = %s"M" [ ( %s"c" / %s"e" / %s"n" ) ]
Numbers = %s"N" [ ( %s"d" / %s"l" / %s"o" ) ]
Punctuation = %s"P" [ ( %x63-66 ; 'c'-'f'
 / %s"i" / %s"o" / %s"s" ) ]
Separators = %s"Z" [ ( %s"l" / %s"p" / %s"s" ) ]
Symbols = %s"S" [ ( %s"c" / %s"k" / %s"m" / %s"o" ) ]
Others = %s"C" [ ( %s"c" / %s"f" / %s"n" / %s"o" ) ]
]]></sourcecode>
      </figure>
      <t>As an additional restriction, <tt>charClassExpr</tt> is not allowed to
match <tt>[^]</tt>, which according to this grammar would parse as a
positive character class containing the single character <tt>^</tt>.</t>
      <t>This is essentially XSD regexp without character class
subtraction, without multi-character escapes such as <tt>\s</tt>,
<tt>\S</tt>, and <tt>\w</tt>, and without Unicode blocks.</t>
      <t>An I-Regexp implementation <bcp14>MUST</bcp14> be a complete implementation of this
limited subset.
In particular, full support for the Unicode functionality defined in
this specification is <bcp14>REQUIRED</bcp14>; the implementation
<bcp14>MUST NOT</bcp14> limit itself to 7- or 8-bit character sets such as ASCII and
<bcp14>MUST</bcp14> support the Unicode character property set in character classes.</t>
      <section anchor="checking">
        <name>Checking Implementations</name>
        <t>A <em>checking</em> I-Regexp implementation is one that checks a supplied
regexp for compliance with this specification and reports any problems.
Checking implementations give their users confidence that they didn't
accidentally insert non-interoperable syntax, so checking is <bcp14>RECOMMENDED</bcp14>.
Exceptions to this rule may be made for low-effort implementations
that map I-Regexp to another regexp library by simple steps such as
performing the mapping operations discussed in <xref target="mapping"/>; here, the
effort needed to do full checking may dwarf the rest of the
implementation effort.
Implementations <bcp14>SHOULD</bcp14> document whether they are checking or not.</t>
        <t>Specifications that employ I-Regexp may want to define in which
cases their implementations can work with a non-checking I-Regexp
implementation and when full checking is needed, possibly in the
process of defining their own implementation classes.</t>
      </section>
    </section>
    <section anchor="i-regexp-semantics">
      <name>I-Regexp Semantics</name>
      <t>This syntax is a subset of that of <xref target="XSD-2"/>.
Implementations which interpret I-Regexps <bcp14>MUST</bcp14>
yield Boolean results as specified in <xref target="XSD-2"/>.
(See also <xref target="xsd-regexps"/>.)</t>
    </section>
    <section anchor="mapping">
      <name>Mapping I-Regexp to Regexp Dialects</name>
      <t>The material in this section is non-normative, provided as guidance
to developers who want to use I-Regexps in the context of other
regular expression dialects.</t>
      <section anchor="multi-character-escapes">
        <name>Multi-Character Escapes</name>
        <t>Common multi-character escapes (MCEs), and character classes built around them,
which are not supported in I-Regexp, can usually
be replaced as shown for example in <xref target="tbl-sub"/>.</t>
        <table anchor="tbl-sub" align="center">
          <name>Example substitutes for multi-character escapes</name>
          <thead>
            <tr>
              <th align="left">MCE/class</th>
              <th align="left">Replace with</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td align="left">
                <tt>\S</tt></td>
              <td align="left">
                <tt>[^ \t\n\r]</tt></td>
            </tr>
            <tr>
              <td align="left">
                <tt>[\S ]</tt></td>
              <td align="left">
                <tt>[^\t\n\r]</tt></td>
            </tr>
            <tr>
              <td align="left">
                <tt>\d</tt></td>
              <td align="left">
                <tt>[0-9]</tt></td>
            </tr>
          </tbody>
        </table>
        <t>Note that the semantics of <tt>\d</tt> in XSD regular expressions
        is that of <tt>\p{Nd}</tt>; however, this would include all Unicode
        characters that are digits in various writing systems, which is almost
        certainly not what is required in IETF publications.</t>
        <t>The construct <tt>\p{IsBasicLatin}</tt> is essentially a reference to legacy
ASCII, it can be replaced by the character class <tt>[\u0000-\u007f]</tt>.</t>
      </section>
      <section anchor="xsd-regexps">
        <name>XSD Regexps</name>
        <t>Any I-Regexp also is an XSD Regexp <xref target="XSD-2"/>, so the mapping is an identity
function.</t>
        <t>Note that a few errata for <xref target="XSD-2"/> have been fixed in <xref target="XSD11-2"/>, which
is therefore also included as a normative reference.
XSD 1.1 is less widely implemented than XSD 1.0, and implementations
of XSD 1.0 are likely to include these bugfixes, so for the intents
and purposes of this specification an implementation of XSD 1.0
regexps is equivalent to an implementation of XSD 1.1 regexps.</t>
      </section>
      <section anchor="toESreg">
        <name>ECMAScript Regexps</name>
        <t>Perform the following steps on an I-Regexp to obtain an ECMAScript
regexp <xref target="ECMA-262"/>:</t>
        <ul spacing="normal">
          <li>For any unescaped dots (<tt>.</tt>) outside character classes
          (first alternative of <tt>charClass</tt> production): replace dot by
          <tt>[^\n\r]</tt>.</li>
          <li>Envelope the result in <tt>^(?:</tt> and <tt>)$</tt>.</li>
        </ul>
        <t>The ECMAScript regexp is to be interpreted as a Unicode pattern ("u"
flag; see Section 21.2.2 "Pattern Semantics" of <xref target="ECMA-262"/>).</t>
        <t>Note that where a regexp literal is required,
the actual regexp needs to be enclosed in <tt>/</tt>.</t>
      </section>
      <section anchor="pcre-re2-ruby-regexps">
        <name>PCRE, RE2, Ruby Regexps</name>
        <t>Perform the same steps as in <xref target="toESreg"/> to obtain a valid regexp in
PCRE <xref target="PCRE2"/>, the Go programming language <xref target="RE2"/>, and the Ruby
programming language, except that the last step is:</t>
        <ul spacing="normal">
          <li>Enclose the regexp in <tt>\A(?:</tt> and <tt>)\z</tt>.</li>
        </ul>
      </section>
    </section>
    <section anchor="background">
      <name>Motivation and Background</name>
      <t>While regular expressions originally were intended to describe a
formal language to support a Boolean matching function, they
have been enhanced with parsing functions that support the extraction
and replacement of arbitrary portions of the matched text. With this
accretion of features, parsing regexp libraries have become
more susceptible to bugs and surprising performance degradations which
can be exploited in Denial of Service attacks by
an attacker who controls the regexp submitted for
processing. I-Regexp is designed to offer interoperability, and to be
less vulnerable to such attacks, with the trade-off that its only
function is to offer a boolean response as to whether a character
sequence is matched by a regexp.</t>
      <section anchor="subsetting">
        <name>Implementing I-Regexp</name>
        <t>XSD regexps are relatively easy to implement or map to widely
implemented parsing regexp dialects, with these notable
exceptions:</t>
        <ul spacing="normal">
          <li>Character class subtraction.  This is a very useful feature in
          many specifications, but it is unfortunately mostly absent from
          parsing regexp dialects. Thus, it is omitted from I-Regexp.</li>
          <li>Multi-character escapes.  <tt>\d</tt>, <tt>\w</tt>, <tt>\s</tt>
          and their uppercase complement classes exhibit a large amount of
          variation between regexp flavors.  Thus, they are omitted from
          I-Regexp.</li>
          <li>Not all regexp implementations support accesses to Unicode
          tables that enable executing constructs such as <tt>\p{Nd}</tt>,
          although the <tt>\p</tt>/<tt>\P</tt> feature in general is now quite
          widely available. While in principle it is possible to translate
          these into character-class matches, this also requires access to
          those tables. Thus, regexp libraries in severely constrained
          environments may not be able to support I-Regexp conformance.</li>
        </ul>
      </section>
    </section>
    <section anchor="iana-considerations">
      <name>IANA Considerations</name>
      <t>This document makes no requests of IANA.</t>
    </section>
    <section anchor="security-considerations">
      <name>Security considerations</name>
      <t>While technically out of scope of this specification, Section <xref target="RFC3629" section="10" sectionFormat="bare">Security Considerations</xref> of <xref target="RFC3629"/> applies to implementations.
Particular note needs to be taken of the last paragraph of Section <xref target="RFC3629" section="3" sectionFormat="bare">UTF-8 definition</xref> of <xref target="RFC3629"/>; an I-Regexp implementation may need to
mitigate limitations of the platform implementation in this regard.</t>
      <t>As discussed in <xref target="background"/>, more complex regexp libraries may
contain exploitable bugs leading to crashes and remote code
execution.  There is also the problem that such libraries often have
hard-to-predict performance characteristics, leading to attacks
that overload an implementation by matching against an expensive
attacker-controlled regexp.</t>
      <t>I-Regexps have been designed to allow implementation in a way that is
resilient to both threats; this objective needs to be addressed
throughout the implementation effort.
Non-checking implementations (see <xref target="checking"/>) are likely to expose
security limitations of any regexp engine they use, which may be less
problematic if that engine has been built with security considerations
in mind (e.g., <xref target="RE2"/>); a checking implementation is still <bcp14>RECOMMENDED</bcp14>.</t>
      <t>Implementations that specifically implement the I-Regexp subset can,
with care, be designed to generally run in linear time and space in
the input, and to detect when that would not be the case (see below).</t>
      <t>Existing regexp engines should be able to easily handle most I-Regexps
(after the adjustments discussed in <xref target="mapping"/>), but may consume
excessive resources for some types of I-Regexps or outright reject
them because they cannot guarantee efficient execution.
(Note that different versions of the same regexp library may be more or
less vulnerable to excessive resource consumption for these cases.)</t>
      <t>Specifically, range quantifiers (as in <tt>a{2,4}</tt>) provide particular
challenges for both existing and I-Regexp focused implementations.
These may therefore limit range quantifiers in composability
(disallowing nested range quantifiers such as <tt>(a{2,4}){2,4}</tt>) or
range (disallowing very large ranges such as <tt>a{20,200000}</tt>), or detect
and reject any excessive resource consumption caused by them.</t>
      <t>I-Regexp implementations that are used to evaluate regexps from
untrusted sources need to be robust to these cases.
Implementers using existing regexp libraries are encouraged to check
their documentation to see if mitigations are configurable, such as
limits in resource consumption, and to document their own degree of
robustness resulting from employing such mitigations.</t>
    </section>
  </middle>
  <back>
    <displayreference target="RFC3629" to="STD63"/>
    <displayreference target="I-D.ietf-jsonpath-base" to="JSONPATH-BASE"/>
    <references>
      <name>References</name>
      <references>
        <name>Normative References</name>

<!-- [XSD-2] The URL below is correct.  Also found https://www.w3.org/TR/xmlschema-2/ -->

        <reference anchor="XSD-2" target="https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/">
          <front>
            <title>XML Schema Part 2: Datatypes Second Edition</title>
            <author fullname="Ashok Malhotra" role="editor"/>
            <author fullname="Paul V. Biron" role="editor"/>
            <date day="28" month="October" year="2004"/>
          </front>
          <seriesInfo name="W3C REC" value="REC-xmlschema-2-20041028"/>
          <seriesInfo name="W3C" value="REC-xmlschema-2-20041028"/>
        </reference>

<!-- [XSD11-2] The URL below is correct.  Also found https://www.w3.org/TR/xmlschema11-2/ -->

        <reference anchor="XSD11-2" target="https://www.w3.org/TR/2012/REC-xmlschema11-2-20120405/">
          <front>
            <title>W3C XML Schema Definition Language (XSD) 1.1 Part 2: Datatypes</title>
            <author fullname="Ashok Malhotra" role="editor"/>
            <author fullname="David Peterson" role="editor"/>
            <author fullname="Henry Thompson" role="editor"/>
            <author fullname="Michael Sperberg-McQueen" role="editor"/>
            <author fullname="Paul V. Biron" role="editor"/>
            <author fullname="Sandy Gao" role="editor"/>
            <date day="5" month="April" year="2012"/>
          </front>
          <seriesInfo name="W3C REC" value="REC-xmlschema11-2-20120405"/>
          <seriesInfo name="W3C" value="REC-xmlschema11-2-20120405"/>
        </reference>

<xi:include href="https://bib.ietf.org/public/rfc/bibxml/reference.RFC.5234.xml"/>
<xi:include href="https://bib.ietf.org/public/rfc/bibxml/reference.RFC.7405.xml"/>
<xi:include href="https://bib.ietf.org/public/rfc/bibxml/reference.RFC.2119.xml"/>
<xi:include href="https://bib.ietf.org/public/rfc/bibxml/reference.RFC.8174.xml"/>

      </references>
      <references>
        <name>Informative References</name>

<xi:include href="https://bib.ietf.org/public/rfc/bibxml/reference.RFC.3629.xml"/>

<!-- [I-D.ietf-jsonpath-base] IESG state AD Evaluation -->

<xi:include href="https://datatracker.ietf.org/doc/bibxml3/reference.I-D.ietf-jsonpath-base.xml"/>

<!-- [UNICODE-GLOSSARY] The URL below is correct -->

        <reference anchor="UNICODE-GLOSSARY" target="https://unicode.org/glossary/">
          <front>
            <title>Glossary of Unicode Terms</title>
            <author>
              <organization>Unicode, Inc.</organization>
            </author>
            <date/>
          </front>
        </reference>

<!-- [RE2] The URL below is correct -->

        <reference anchor="RE2" target="https://github.com/google/re2">
          <front>
            <title>RE2 is a fast, safe, thread-friendly alternative to backtracking regular expression engines like those used in PCRE, Perl, and Python. It is a C++ library.</title>
            <author>
              <organization/>
            </author>
            <date>n.d.</date>
          </front>
        </reference>

<!-- [PCRE2] The URL below is correct -->

        <reference anchor="PCRE2" target="http://pcre.org/current/doc/html/">
          <front>
            <title>Perl-compatible Regular Expressions (revised API: PCRE2)</title>
            <author>
              <organization/>
            </author>
            <date>n.d.</date>
          </front>
        </reference>

<!-- [ECMA-262] The URL below is correct.  Also found https://262.ecma-international.org/11.0/ -->

        <reference anchor="ECMA-262" target="https://www.ecma-international.org/wp-content/uploads/ECMA-262.pdf">
          <front>
            <title>ECMAScript 2020 Language Specification</title>
            <author>
              <organization>Ecma International</organization>
            </author>
            <date year="2020" month="June"/>
          </front>
          <seriesInfo name="ECMA" value="Standard ECMA-262, 11th Edition"/>
        </reference>

<xi:include href="https://bib.ietf.org/public/rfc/bibxml/reference.RFC.7493.xml"/>

      </references>
    </references>

    <section numbered="false" anchor="acknowledgements">
      <name>Acknowledgements</name>
      <t>This specification has been motivated by the discussion in the IETF
      JSONPATH WG about whether to include a regexp mechanism into the
      JSONPath query expression specification, as well as by previous
      discussions about the YANG <tt>pattern</tt> and CDDL <tt>.regexp</tt>
      features.</t>
      <t>The basic approach for this specification was inspired by <xref
      target="RFC7493">The I-JSON Message Format</xref>.</t>
    </section>
  </back>

<!-- [rfced] Please review the "type" attribute of the sourcecode element
in the XML file to ensure correctness. If the current list of preferred
values for "type" (https://www.rfc-editor.org/materials/sourcecode-types.txt) 
does not contain an applicable type, then feel free to let us
know. Also, it is acceptable to leave the "type" attribute not
set.  
-->

<!-- [rfced] Please review whether any of the notes in this document
should be in the <aside> element. It is defined as "a container for 
content that is semantically less important or tangential to the 
content that surrounds it" (https://authors.ietf.org/en/rfcxml-vocabulary#aside).
-->

<!-- [rfced] In the html and pdf outputs, the text enclosed in <tt> is output
in fixed-width font. In the txt output, there are no changes to the font,
and the quotation marks have been removed.

In the html and pdf outputs, the text enclosed in <em> is output in
italics. In the txt output, the text enclosed in <em> appears with an
underscore before and after.

Please review carefully and let us know if the output is acceptable or if any
updates are needed.
-->



</rfc>
