Skip to content
New issue

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

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

Already on GitHub? # to your account

Bug: Token Regex for fileuniqueid* matches too broadly #751

Open
heinrich-ulbricht opened this issue Sep 29, 2022 · 2 comments
Open

Bug: Token Regex for fileuniqueid* matches too broadly #751

heinrich-ulbricht opened this issue Sep 29, 2022 · 2 comments

Comments

@heinrich-ulbricht
Copy link
Contributor

heinrich-ulbricht commented Sep 29, 2022

This Regex matches more than just fileuniqueid* tokens:

Regex fileUniqueIdToken = new Regex("(?<token>[{]{1,2}(?:fileuniqueid:fileuniqueidencoded:)|[^}]*[}]{1,2})(?:[^{]*)", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);

image

This will be handled OK since exceptions are swallowed, but calls are made that are not necessary (for every field content containing curly braces). So this Regex could be improved to not trigger those redundant calls.

Alternative easy improvement: after this line:

var tokenParts = replacementVal.Trim(new char[] { '{', '}' }).Split(':');

Add:

if (tokenParts.Length < 2)
{
    continue;
}

This catches most mismatches and prevents further redundant calls.

heinrich-ulbricht added a commit to WikiTransformationProject/pnpframework that referenced this issue Sep 29, 2022
@heinrich-ulbricht heinrich-ulbricht changed the title Small improvement: Token Regexr for fileuniqueid* matches too broadly Small improvement: Token Regex for fileuniqueid* matches too broadly Oct 5, 2022
@heinrich-ulbricht heinrich-ulbricht changed the title Small improvement: Token Regex for fileuniqueid* matches too broadly Bug: Token Regex for fileuniqueid* matches too broadly Oct 14, 2022
@heinrich-ulbricht
Copy link
Contributor Author

Ramping this up to bug as I got a case where some content was matched by the RegEx and tokenParts[1] was an empty string. This causes the ID of a folder to be loaded here:

var folder = web.GetFolderByServerRelativePath(ResourcePath.FromDecodedUrl(tokenParts[1]));
folder.EnsureProperties(f => f.UniqueId);
UniqueId = folder.UniqueId;

But the invalid tokenParts then throw here with invalid pattern exception:

value = Regex.Replace(value, $"{{{tokenParts[0]}:{tokenParts[1]}}}", replTest);

Proposed fix:

WikiTransformationProject@277dd27

heinrich-ulbricht added a commit to heinrich-ulbricht/pnpframework that referenced this issue Oct 17, 2022
heinrich-ulbricht added a commit to heinrich-ulbricht/pnpframework that referenced this issue Oct 17, 2022
@carlosmiguelsns
Copy link

carlosmiguelsns commented Oct 11, 2024

I noticed that there is a RegEx responsible for capturing tokens in the UpdateListItem process. However, if the list item contains a JSON, the existing RegEx does not validate correctly.

There could probably be a rewrite of the regex here or in the next process of the condition to exclude tokens that start with a quotation mark or single quote, because it identifies a JSON property.

However, I've made an improvement to the RegEx:

Regex fileUniqueIdToken = new Regex("(?<token>\\{\\{(?!\\\")[^{}]+?:[^{}]+?\\}\\}|(?<!\\{)\\{(?!\\\")[^{}]+?:[^{}]+?\\}(?!\\}))", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);

Matching - all tokens that containst at list :

  • {fileuniqueid:[siteRelativePath]}
  • {fileuniqueidencoded:[siteRelativePath]}
  • {othertokenname:anyvalue} or {{tokenname:value}}

I'm not specifying fileuniqueid or fileuniqueidencoded because it's implicit in the RegEx.

Can someone please validate if there is a match that is not being adressed?

If this is the right solution, I can do a PR of my code

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants