question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Static code analysis support

See original GitHub issue

Problem description People can write valid JSLT code that contains incorrect regular expressions. The code parses correctly but fails at runtime. The failure can be deterministic (regexp as a standalone expression) or indeterministic (regepx is part of an OR boolean clause that is triggered only under certain conditions).

The Java Pattern compile set of methods will throw a PatternSyntaxException when the expression is compiled.

The static helper method getRegexp will be called in BuiltinFunctions at runtime, which means people may write JSLT that compiles and they expect to be valid but fails under some circumstances (or always if on a standalone expression).

Expected behaviour Incorrect regexp patterns in functions like test fail at JSLT parsing time, surfacing the issue raised by the underlying regexp runtime, as that is independent from the input and can potentially be known at compile time. This behaviour is expected even when regexp expressions are nested within boolean expressions.

Actual behaviour Parsing does not fail, running the JSLT fails at runtime when the right boolean expression is triggered or all the time when the expression is standalone.

Workarounds Partial: Expression public interface returned by the public API Parser allows you to call apply. You could build a tester that parses any JSLT input code and runs it on an static JSON input, say an empty JSON doc. This would deterministically catch invalid regexes that are standalone and not part of a nested boolean.

Complete: To catch nested regexes in nested booleans, there would need to be a test document in sync with the JSLT code that triggers all the necessary boolean expressions and checks the regexp. This is equivalent to writing comprehensive unit tests for all JSLT code.

Complete: use static analysis techniques, obtain the JSLT Abstract Syntax Tree (AST), do a search for test and other regexp using expression nodes, check the regexp correctness (check them against an empty JSON doc). This is a deterministic method that catches all invalid regexes.

Expression public interface returned by the public API Parser does not expose useful methods to implement this, like getting children expressions and the name of the functions. It can be fairly trivially implemented by using internal interfaces ExpressionImpl, which works but is very brittle as is not using public contracts.

Complete: extending Expression to return children expression and the name/type of the expression. Exposing the name is not mandatory but means the testing can be done only on relevant nodes and not have to brute force test all JSLT code expressions to look for PatternSyntaxExceptions which is computationally expensive. Once that interface is exposed, it is fairly trivial to implement static code analysis like testing regexes, and may be useful for other use-cases.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
danigiricommented, May 13, 2021

Thank you very much, we will test it but looking at the PR and tests I would not expect any problems 😄

0reactions
rodrigo-molinacommented, May 13, 2021

thanks 🙏

I’ll add a task to our backlog to do so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Static Code Analysis - OWASP Foundation
Static Code Analysis commonly refers to the running of Static Code Analysis tools that attempt to highlight possible vulnerabilities within 'static' (non- ...
Read more >
Best Static Code Analysis Tools in 2022 - G2
Top Static Code Analysis Tools. Choose the right Static Code Analysis Tools using real-time, up-to-date product reviews from 701 verified user reviews.
Read more >
TOP 40 Static Code Analysis Tools (Best Source Code ...
TOP 40 Static Code Analysis Tools (Best Source Code Analysis Tools) · #1) Raxis · #2) SonarQube · #3) PVS-Studio · #4) DeepSource...
Read more >
List of tools for static code analysis - Wikipedia
This is a list of notable tools for static program analysis Contents. 1 Static code analysis ... Static code analyzer with support for...
Read more >
Static Code Analysis - Veracode
Static code analysis is a process for analyzing an application's code for potential errors. It is “static” because it analyses applications without running...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found