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
12 changes: 9 additions & 3 deletions remediation/workflow/pin/pinactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -229,8 +228,15 @@ func getSemanticVersion(client *github.Client, owner, repo, tagOrBranch, commitS
// Function to check if an action matches any pattern in the list
func ActionExists(actionName string, patterns []string) bool {
for _, pattern := range patterns {
// Use filepath.Match to match the pattern
matched, err := filepath.Match(pattern, actionName)
// Convert glob pattern to regex for path matching
// Replace * with [^/]* to match within a path segment
// Replace **/ with .* to match across path segments
regexPattern := strings.ReplaceAll(pattern, "**", "§§")
regexPattern = strings.ReplaceAll(regexPattern, "*", "[^/]*")
regexPattern = strings.ReplaceAll(regexPattern, "§§", ".*")
regexPattern = "^" + regexPattern + "($|/)"

matched, err := regexp.MatchString(regexPattern, actionName)
if err != nil {
// Handle invalid patterns
fmt.Printf("Error matching pattern: %v\n", err)
Expand Down
35 changes: 34 additions & 1 deletion remediation/workflow/pin/pinactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func TestPinActions(t *testing.T) {
{fileName: "actionwithcomment.yml", wantUpdated: true, pinToImmutable: true},
{fileName: "repeatedactionwithcomment.yml", wantUpdated: true, pinToImmutable: true},
{fileName: "immutableaction-1.yml", wantUpdated: true, pinToImmutable: true},
{fileName: "exemptaction.yml", wantUpdated: true, exemptedActions: []string{"actions/checkout", "rohith/*"}, pinToImmutable: true},
{fileName: "exemptaction.yml", wantUpdated: true, exemptedActions: []string{"actions/checkout", "rohith/*", "praveen/*", "aman-*/*", "*/seperate*"}, pinToImmutable: true},
{fileName: "donotpintoimmutable.yml", wantUpdated: true, pinToImmutable: false},
{fileName: "invertedcommas.yml", wantUpdated: true, pinToImmutable: false},
}
Expand Down Expand Up @@ -345,3 +345,36 @@ func Test_isAbsolute(t *testing.T) {
})
}
}

func TestActionExists(t *testing.T) {
result := ActionExists("actions/checkout", []string{"actions/checkout"})
t.Log(result)
if !result {
t.Errorf("ActionExists returned false for actions/checkout")
}

result = ActionExists("actions/checkout", []string{"actions/*"})
t.Log(result)
if !result {
t.Errorf("ActionExists returned false for actions/checkout")
}

result = ActionExists("actions/checkout/something", []string{"actions/*"})
t.Log(result)
if !result {
t.Errorf("ActionExists returned true for actions/checkout/something")
}

result = ActionExists("step-security/checkout/something", []string{"step-*/*"})
t.Log(result)
if !result {
t.Errorf("ActionExists returned true for actions/checkout/something")
}

result = ActionExists("step-security/checkout-release/something", []string{"*/checkout-*"})
t.Log(result)
if !result {
t.Errorf("ActionExists returned true for actions/checkout/something")
}

}
24 changes: 24 additions & 0 deletions testfiles/pinactions/input/exemptaction.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ jobs:
- name: publish on version change
id: publish_nuget
uses: rohith/publish-nuget@v2
with:
PROJECT_FILE_PATH: Core/Core.csproj
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json

- name: publish on version change 2
id: publish_nuget
uses: praveen/publish-nuget/to-version@v2
with:
PROJECT_FILE_PATH: Core/Core.csproj
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json

- name: publish on version change 3
id: publish_nuget
uses: aman-action/move/to-main@v2
with:
PROJECT_FILE_PATH: Core/Core.csproj
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json

- name: publish on version change 2
id: publish_nuget
uses: smith/seperate/from-version@v2
with:
PROJECT_FILE_PATH: Core/Core.csproj
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
Expand Down
24 changes: 24 additions & 0 deletions testfiles/pinactions/output/exemptaction.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ jobs:
- name: publish on version change
id: publish_nuget
uses: rohith/publish-nuget@v2
with:
PROJECT_FILE_PATH: Core/Core.csproj
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json

- name: publish on version change 2
id: publish_nuget
uses: praveen/publish-nuget/to-version@v2
with:
PROJECT_FILE_PATH: Core/Core.csproj
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json

- name: publish on version change 3
id: publish_nuget
uses: aman-action/move/to-main@v2
with:
PROJECT_FILE_PATH: Core/Core.csproj
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
NUGET_SOURCE: https://nuget.pkg.github.com/OWNER/index.json

- name: publish on version change 2
id: publish_nuget
uses: smith/seperate/from-version@v2
with:
PROJECT_FILE_PATH: Core/Core.csproj
NUGET_KEY: ${{ secrets.GITHUB_TOKEN }}
Expand Down