DominikPieper / obsidian-ReadItLater

MIT License
424 stars 34 forks source link

fix: Use anonymous functions to return replacement strings where necessary #177

Closed CuberL closed 1 week ago

CuberL commented 1 week ago

Use anonymous functions to return replacement strings where necessary to avoid issues caused by special replacement patterns in the content

Description

The code heavily uses the pattern of replacing placeholders with specific content to achieve a template-like functionality. However, if the content contains special replacement patterns as mentioned in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement, there will be issues with incorrect replacements. A minimal reproducible example:

let template = ('[[ReadItLater]] [[Article]] %articleContent%')
let content = 'For example, the Regex pattern or quantifier `^(a+)+$` is represented by the following NFA'
console.log(template.replace(/%articleContent%/g, () => content))

Expect:

[[ReadItLater]] [[Article]] For example, the Regex pattern or quantifier `^(a+)+$` is represented by the following NFA

Got:

[[ReadItLater]] [[Article]] For example, the Regex pattern or quantifier ^(a+)+[[ReadItLater]] [[Article]]  is represented by the following NFA

Motivation and Context

I wrapped the necessary parts with an anonymous function. Using an anonymous function can bypass this issue because JavaScript directly uses the result of the anonymous function for replacement.

How has this been tested?

Real world example:

https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS

Before fix: image

After fix:

image

Screenshots (if appropriate)

Types of changes

Changes visible to users:

Internal changes:

Checklist

adamluckdev commented 1 week ago

Thanks!