Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Rules/AvoidUsingInternalURLs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Management.Automation.Language;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
using System.ComponentModel.Composition;
Expand Down Expand Up @@ -44,8 +46,19 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
foreach (StringConstantExpressionAst expressionAst in expressionAsts)
{
//Check if XPath is used. If XPath is used, then we don't throw warnings.

Ast parentAst = expressionAst.Parent;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is this gonna raise a NullPointerException if parentAst is null?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think it will ever be null..At least the root Ast should be a ScriptBlockAst?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Have you tried on a script with only a string as its content?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah..I tried with a single line script and it didn't throw warnings or errors.

//Check if -replace is used, if it is string replace, we don't throw warnings.
Ast grandParentAst = parentAst.Parent;
if (grandParentAst is BinaryExpressionAst)
{
if ((grandParentAst as BinaryExpressionAst).Operator.Equals(TokenKind.Ireplace))
{
continue;
}
}

//Check if XPath is used. If XPath is used, then we don't throw warnings.
if (parentAst is InvokeMemberExpressionAst)
{
InvokeMemberExpressionAst invocation = parentAst as InvokeMemberExpressionAst;
Expand All @@ -55,7 +68,9 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
String.Equals(invocation.Member.ToString(), "SelectNodes",StringComparison.OrdinalIgnoreCase) ||
String.Equals(invocation.Member.ToString(), "Select", StringComparison.OrdinalIgnoreCase) ||
String.Equals(invocation.Member.ToString(), "Evaluate",StringComparison.OrdinalIgnoreCase) ||
String.Equals(invocation.Member.ToString(), "Matches",StringComparison.OrdinalIgnoreCase))
String.Equals(invocation.Member.ToString(), "Matches",StringComparison.OrdinalIgnoreCase) ||
String.Equals(invocation.Expression.ToString(), "[System.String]",StringComparison.OrdinalIgnoreCase) ||
String.Equals(invocation.Expression.ToString(), "[String]", StringComparison.OrdinalIgnoreCase))
{
continue;
}
Expand Down
4 changes: 3 additions & 1 deletion Tests/Rules/AvoidUsingInternalURLsNoViolations.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ function Test
{
$filesNode = $infoXml.SelectSingleNode("//files")
}
$sd = "O:BAG:BAD:(A;;0x800;;;WD)(A;;0x120fff;;;SY)(A;;0x120fff;;;LS)(A;;0x120fff;;;NS)(A;;0x120fff;;;BA)(A;;0xee5;;;LU)(A;;LC;;;MU)(A;;0x800;;;AG)"
$sd = "O:BAG:BAD:(A;;0x800;;;WD)(A;;0x120fff;;;SY)(A;;0x120fff;;;LS)(A;;0x120fff;;;NS)(A;;0x120fff;;;BA)(A;;0xee5;;;LU)(A;;LC;;;MU)(A;;0x800;;;AG)"
$msg = [System.String]::Format("1:{0}", "test")
$internalId = $Id -replace '^([^/])','/$1' -as $Id.GetType()