Visitar URL original
Add Regular Expression limits by rvesse · Pull Request #4315 · apache/jena · GitHub
Skip to content

Add Regular Expression limits - #4315

Draft
rvesse wants to merge 4 commits into
apache:mainfrom
rvesse:regex-limits
Draft

rvesse wants to merge 4 commits into
apache:mainfrom
rvesse:regex-limits

Conversation

@rvesse

@rvesse rvesse commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Various SPARQL functions and Jena provided extension functions provide access to regular expressions. Depending on the expression and data combination some regular expressions can take too long to run leading to slow queries. This PR bounds regular expression evaluation time via statically controlled limits so regular expressions that take too long to run are aborted with an ExprEvalException.

As ShEx also uses the same Jena subsystem is was also upgraded to apply limits.


  • Tests are included.
  • Documentation change and updates are provided for the Apache Jena website
  • Commits have been squashed to remove intermediate development commit messages.
  • Key commit messages start with the issue number (GH-xxxx)

By submitting this pull request, I acknowledge that I am making a contribution to the Apache Software Foundation under the terms and conditions of the Contributor's Agreement.


See the Apache Jena "Contributing" guide.

rvesse added 4 commits October 1, 2026 15:49
When given a poorly formed regular expression depending on data the
regular expression matching can take a long time to compute.  This
commit modifies the expression engine so that regular expression based
functions time bound evaluation to avoid runaway expressions.
Ensures that the ARQ property function strSplit time bounds regular
expression splitting time
REPLACE uses regular expressions to perform replacements so also impose
regular expression limits on its evaluation
Shex regular expression constraints could run too long depending on the
regular expression and data combinations.  This commit uses the shared
RegexEngine.timeBounded() mechanism to limit evaluation time.

Also disables a test scenario where optimisation effects means that the
test does not reliably fail so is not a useful stable test.

@ParameterizedTest(name = "{1}")
@MethodSource("slowSplits")
public void givenSlowRegex_whenSplittingWithTimeLimit_thenFails(String data, String pattern, int expectedResultCount) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A more human oriented name perhaps.


@ParameterizedTest(name = "{1}")
@MethodSource("slowSplits")
public void givenSlowRegex_whenSplittingWithNoTimeLimit_thenEventuallySucceeds(String data, String pattern, int expectedResultCount) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A more human oriented name perhaps.


@ParameterizedTest(name = "{1}")
@MethodSource("backtrackingRegex")
public void givenBacktrackingRegex_whenEvaluatingRegexWithoutTimeLimit_thenEventuallyCompletes(String input, String pattern, String flags, boolean shouldMatch) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A more human oriented name perhaps.


@ParameterizedTest(name = "{1}")
@MethodSource("backtrackingRegex")
public void givenBacktrackingRegex_whenEvaluatingRegexWithTimeLimit_thenFails(String input, String pattern, String flags, boolean shouldMatch) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A more human oriented name perhaps.

* Default maximum permitted regular expression evaluation time
* @see #MAX_REGEX_EVALUATION_TIME
*/
public static final long DEFAULT_MAX_REGEX_EVALUATION_TIME = 250;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it would be useful to put all limits (settings and defaults) in one place, including MathLimits from #4276. It makes things findable.

One possible place is org.apache.jena.sparql.ARQConstants; another is a new class for this purpose org.apache.jena.sparql.SysLimits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants