Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Integrating OpenAI Code Analysis for identifying harmful code #4

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
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
54 changes: 54 additions & 0 deletions Engine.Tests/Analyze/Conditions/AIisBestPracticeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Engine;
using Engine.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
using OpenAI_API;
using System.Xml.Serialization;
using System.IO;
using System.Threading.Tasks;
using Xunit;

namespace PowerShellProtect.Analyze.Conditions.Tests
{
public class AIisBestPracticeConditionTests
{
[Fact]
public void Analyze_ReturnsTrue_WhenOpenAIResponseRatingIsGreaterThanOrEqualToConditionAIRating()
{
// Arrange
var context = new ScriptContext();
var condition = new Condition
{
APIKey = "your-api-key",
AIRating = 0.5m
};
var aIisBestPracticeCondition = new AIisBestPracticeCondition();

// Act
var result = aIisBestPracticeCondition.Analyze(context, condition);

// Assert
Assert.True(result);
}

[Fact]
public void Analyze_ReturnsFalse_WhenOpenAIResponseIsNull()
{
// Arrange
var context = new ScriptContext();
var condition = new Condition
{
APIKey = "your-api-key",
AIRating = 0.5m
};
var aIisBestPracticeCondition = new AIisBestPracticeCondition();

// Act
var result = aIisBestPracticeCondition.Analyze(context, condition);

// Assert
Assert.False(result);
}
}
}
54 changes: 54 additions & 0 deletions Engine.Tests/Analyze/Conditions/AIisSuspiciousTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Engine;
using Engine.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
using OpenAI_API;
using System.Xml.Serialization;
using System.IO;
using System.Threading.Tasks;
using Xunit;

namespace PowerShellProtect.Analyze.Conditions.Tests
{
public class AIisSuspiciousConditionTests
{
[Fact]
public void Analyze_ReturnsTrue_WhenOpenAIResponseRatingIsGreaterThanOrEqualToConditionAIRating()
{
// Arrange
var context = new ScriptContext();
var condition = new Condition
{
APIKey = "your-api-key",
AIRating = 0.5m
};
var aiIsSuspiciousCondition = new AIisSuspiciousCondition();

// Act
var result = aiIsSuspiciousCondition.Analyze(context, condition);

// Assert
Assert.True(result);
}

[Fact]
public void Analyze_ReturnsFalse_WhenOpenAIResponseIsNull()
{
// Arrange
var context = new ScriptContext();
var condition = new Condition
{
APIKey = "your-api-key",
AIRating = 0.5m
};
var aiIsSuspiciousCondition = new AIisSuspiciousCondition();

// Act
var result = aiIsSuspiciousCondition.Analyze(context, condition);

// Assert
Assert.False(result);
}
}
}
1 change: 1 addition & 0 deletions Engine.Tests/Engine.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" />
Expand Down
210 changes: 0 additions & 210 deletions Engine.Tests/Resource1.resx

This file was deleted.

4 changes: 2 additions & 2 deletions PowerShellProtect.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'PowerShellProtect.psm1'

# Version number of this module.
ModuleVersion = '2022.11.0'
ModuleVersion = '2022.11.1'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -72,7 +72,7 @@
FunctionsToExport = @('Install-PowerShellProtect', 'Uninstall-PowerShellProtect')

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @('Get-PSPConfiguration', 'Set-PSPConfiguration', 'Test-PSPConfiguration', 'New-PSPRule', 'New-PSPCondition', 'New-PSPAction', 'New-PSPConfiguration', 'Save-PSPConfiguration')
CmdletsToExport = @('Get-PSPConfiguration', 'Set-PSPConfiguration', 'Test-PSPConfiguration', 'New-PSPRule', 'New-PSPCondition', 'New-PSPAction', 'New-PSPConfiguration', 'Save-PSPConfiguration','New-PSPAICondition')

# Variables to export from this module
VariablesToExport = @()
Expand Down
13 changes: 13 additions & 0 deletions PowerShellProtect/Analyze/AIResultCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace PowerShellProtect.Analyze
{
public enum AIResultCondition
{
LessThan,
EqualTo,
GreaterThan,
GreaterThanOrEquals,
LessThanOrEquals,
NotEqualTo

}
}
2 changes: 2 additions & 0 deletions PowerShellProtect/Analyze/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public Analyzer()
new VariableCondition(),
new MemberCondition(),
new ScriptStringCondition(),
new AIisBestPracticeCondition(),
new AIisSuspiciousCondition()
}.ToDictionary(m => m.Name.ToLower(), m => m);

_builtInConditions = new List<ICondition>
Expand Down
Loading