AdhocAdam / smletsexchangeconnector

SMLets PowerShell based Exchange Connector for controlling Microsoft System Center Service Manager 2016+
https://adhocadam.github.io/smletsexchangeconnector/
GNU General Public License v3.0
29 stars 19 forks source link

When suggesting KA/RO escape regex #473

Closed AdhocAdam closed 1 year ago

AdhocAdam commented 1 year ago

Discovered an issue wherein if:

The suggestions for that particular email will never return any results. This is due to the conversion from inline images to text that can be written into SCSM. Take the following an example, an email has a:

<img width=233 height=167 style='width:2.427in;height:1.7395in' id="Picture_x0020_1" src="cid:image001.jpg@01D9D51B.5DC75C80" alt="corporate signature">

But when this email is processed by the connector, the alt text property from the above is read and written into the new Work Item Description as:

screen crack

[corporate signature]

When either Search-CiresonKnowledgeBase or Search-AvailablePortalOffering functions engage, an error will be produced to the effect of:

System.ArgumentException: parsing "\b[corporate\b" - Unterminated [] set.
   at System.Text.RegularExpressions.RegexParser.ScanCharClass(Boolean caseInsensitive, Boolean scanOnly)
   at System.Text.RegularExpressions.RegexParser.CountCaptures()
   at System.Text.RegularExpressions.RegexParser.Parse(String re, RegexOptions op)
   at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options, TimeSpan matchTimeout, Boolean useCache)
   at System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options)
   at System.Management.Automation.ParserOps.NewRegex(String patternString, RegexOptions options)
   at System.Management.Automation.ParserOps.MatchOperator(ExecutionContext context, IScriptExtent errorPosition, Object lval, Object rval, Boolean notMatch, Boolean ignoreCase)
   at System.Management.Automation.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

This is due to the the following lines seen in Search-CiresonKnowledgeBase and Search-AvailableCiresonPortalOffering functions:

$wordsMatched = ($searchQuery.Trim().Split() | Where-Object{($kbResult.title -match "\b$_\b")}).count
$wordsMatched = ($searchQuery.Trim().Split() | Where-Object{($serviceCatalogResult.RequestOfferingTitle -match "\b$_\b") -or ($serviceCatalogResult.RequestOfferingDescription -match "\b$_\b")}).count

When this engages, the first result (from the $searchQuery.Trim().Split()) passed down the pipeline looks like:

phone
issue
screen
crack
[corporate
signature]

By introducing a Foreach-Object on the pipeline that escapes relevant characters (known as a result of #453):

$wordsMatched = ($searchQuery.Trim().Split() | ForEach-Object {[regex]::Escape($_)} | Where-Object{($kbResult.title -match "\b$_\b")}).count
$wordsMatched = ($searchQuery.Trim().Split() | ForEach-Object {[regex]::Escape($_)} | Where-Object{($serviceCatalogResult.RequestOfferingTitle -match "\b$_\b") -or ($serviceCatalogResult.RequestOfferingDescription -match "\b$_\b")}).count

the result now looks like:

phone
issue
screen
crack
\[corporate
signature]

And the email will successfully trigger KA and/or RO suggestion URLs