-
Notifications
You must be signed in to change notification settings - Fork 563
fix: critical security bug in manual mode project selection #2009
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
Open
htplbc
wants to merge
4
commits into
diggerhq:develop
Choose a base branch
from
htplbc:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−8
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,10 @@ package github | |||||||||||||||||
import ( | ||||||||||||||||||
"errors" | ||||||||||||||||||
"fmt" | ||||||||||||||||||
"log/slog" | ||||||||||||||||||
"os" | ||||||||||||||||||
"strings" | ||||||||||||||||||
|
||||||||||||||||||
"github.com/diggerhq/digger/cli/pkg/digger" | ||||||||||||||||||
"github.com/diggerhq/digger/cli/pkg/drift" | ||||||||||||||||||
github_models "github.com/diggerhq/digger/cli/pkg/github/models" | ||||||||||||||||||
|
@@ -21,9 +25,6 @@ import ( | |||||||||||||||||
"github.com/google/go-github/v61/github" | ||||||||||||||||||
"github.com/samber/lo" | ||||||||||||||||||
"gopkg.in/yaml.v3" | ||||||||||||||||||
"log/slog" | ||||||||||||||||||
"os" | ||||||||||||||||||
"strings" | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
func initLogger() { | ||||||||||||||||||
|
@@ -146,11 +147,28 @@ func GitHubCI(lock core_locking.Lock, policyCheckerProvider core_policy.PolicyCh | |||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
var projectConfig digger_config.Project | ||||||||||||||||||
for _, projectConfig = range diggerConfig.Projects { | ||||||||||||||||||
if projectConfig.Name == project { | ||||||||||||||||||
var projectFound bool | ||||||||||||||||||
for _, config := range diggerConfig.Projects { | ||||||||||||||||||
if config.Name == project { | ||||||||||||||||||
projectConfig = config | ||||||||||||||||||
projectFound = true | ||||||||||||||||||
break | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @motatoes updated |
||||||||||||||||||
|
||||||||||||||||||
if !projectFound { | ||||||||||||||||||
// Log available projects to help with debugging | ||||||||||||||||||
var availableProjects []string | ||||||||||||||||||
for _, p := range diggerConfig.Projects { | ||||||||||||||||||
availableProjects = append(availableProjects, p.Name) | ||||||||||||||||||
} | ||||||||||||||||||
slog.Error("Project not found in digger configuration", | ||||||||||||||||||
"requestedProject", project, | ||||||||||||||||||
"availableProjects", availableProjects) | ||||||||||||||||||
usage.ReportErrorAndExit(githubActor, fmt.Sprintf("Project '%s' not found in digger configuration. Available projects: %v", project, availableProjects), 1) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
slog.Info("Found project configuration", "projectName", projectConfig.Name, "projectDir", projectConfig.Dir) | ||||||||||||||||||
htplbc marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
workflow := diggerConfig.Workflows[projectConfig.Workflow] | ||||||||||||||||||
|
||||||||||||||||||
stateEnvVars, commandEnvVars := digger_config.CollectTerraformEnvConfig(workflow.EnvVars, true) | ||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package github | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/diggerhq/digger/libs/digger_config" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestManualModeProjectValidation(t *testing.T) { | ||
// Create a test digger config with some projects | ||
diggerConfig := &digger_config.DiggerConfig{ | ||
Projects: []digger_config.Project{ | ||
{ | ||
Name: "project-a", | ||
Dir: "./project-a", | ||
}, | ||
{ | ||
Name: "project-b", | ||
Dir: "./project-b", | ||
}, | ||
{ | ||
Name: "project-c", | ||
Dir: "./project-c", | ||
}, | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
requestedProject string | ||
shouldFind bool | ||
expectedProject string | ||
}{ | ||
{ | ||
name: "Valid project should be found", | ||
requestedProject: "project-b", | ||
shouldFind: true, | ||
expectedProject: "project-b", | ||
}, | ||
{ | ||
name: "Non-existent project should not be found", | ||
requestedProject: "non-existent-project", | ||
shouldFind: false, | ||
expectedProject: "", | ||
}, | ||
{ | ||
name: "Empty project name should not be found", | ||
requestedProject: "", | ||
shouldFind: false, | ||
expectedProject: "", | ||
}, | ||
{ | ||
name: "Case sensitive project name should not be found", | ||
requestedProject: "Project-A", | ||
shouldFind: false, | ||
expectedProject: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Simulate the fixed project selection logic | ||
var projectConfig digger_config.Project | ||
var projectFound bool | ||
|
||
for _, config := range diggerConfig.Projects { | ||
if config.Name == tt.requestedProject { | ||
projectConfig = config | ||
projectFound = true | ||
break | ||
} | ||
} | ||
|
||
if tt.shouldFind { | ||
assert.True(t, projectFound, "Expected to find project %s", tt.requestedProject) | ||
assert.Equal(t, tt.expectedProject, projectConfig.Name, "Expected project name to match") | ||
htplbc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
assert.False(t, projectFound, "Expected NOT to find project %s", tt.requestedProject) | ||
// In the old buggy code, projectConfig would contain the last project from the loop | ||
// With our fix, projectFound will be false and we should exit with an error | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestManualModeProjectValidationWithOldBuggyLogic(t *testing.T) { | ||
// This test demonstrates the old buggy behavior | ||
diggerConfig := &digger_config.DiggerConfig{ | ||
Projects: []digger_config.Project{ | ||
{ | ||
Name: "project-a", | ||
Dir: "./project-a", | ||
}, | ||
{ | ||
Name: "project-b", | ||
Dir: "./project-b", | ||
}, | ||
{ | ||
Name: "dangerous-project", | ||
Dir: "./dangerous-project", | ||
}, | ||
}, | ||
} | ||
|
||
// Simulate the OLD buggy logic | ||
requestedProject := "non-existent-project" | ||
var projectConfig digger_config.Project | ||
|
||
// This is the OLD BUGGY CODE that would cause the issue | ||
for _, projectConfig = range diggerConfig.Projects { | ||
if projectConfig.Name == requestedProject { | ||
break | ||
} | ||
} | ||
|
||
// In the old code, projectConfig would now contain "dangerous-project" | ||
// (the last project in the loop) even though we requested "non-existent-project" | ||
assert.Equal(t, "dangerous-project", projectConfig.Name, | ||
"This demonstrates the old bug: projectConfig contains the last project from the loop") | ||
assert.NotEqual(t, requestedProject, projectConfig.Name, | ||
"This shows the dangerous behavior: we're using a different project than requested") | ||
} | ||
|
||
func TestAvailableProjectsLogging(t *testing.T) { | ||
// Test that we properly log available projects when a project is not found | ||
diggerConfig := &digger_config.DiggerConfig{ | ||
Projects: []digger_config.Project{ | ||
{Name: "web-app"}, | ||
{Name: "api-service"}, | ||
{Name: "database"}, | ||
}, | ||
} | ||
|
||
requestedProject := "missing-project" | ||
var projectFound bool | ||
var availableProjects []string | ||
|
||
// Simulate the project search | ||
for _, config := range diggerConfig.Projects { | ||
if config.Name == requestedProject { | ||
projectFound = true | ||
break | ||
} | ||
} | ||
htplbc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if !projectFound { | ||
// Collect available projects for error message | ||
for _, p := range diggerConfig.Projects { | ||
availableProjects = append(availableProjects, p.Name) | ||
} | ||
} | ||
|
||
assert.False(t, projectFound) | ||
assert.Equal(t, []string{"web-app", "api-service", "database"}, availableProjects) | ||
} | ||
|
||
// This test would actually call usage.ReportErrorAndExit in a real scenario | ||
// but we can't easily test that without mocking the exit behavior | ||
func TestProjectNotFoundErrorMessage(t *testing.T) { | ||
diggerConfig := &digger_config.DiggerConfig{ | ||
Projects: []digger_config.Project{ | ||
{Name: "project-1"}, | ||
{Name: "project-2"}, | ||
}, | ||
} | ||
|
||
requestedProject := "invalid-project" | ||
var projectFound bool | ||
var availableProjects []string | ||
|
||
for _, config := range diggerConfig.Projects { | ||
if config.Name == requestedProject { | ||
projectFound = true | ||
break | ||
} | ||
} | ||
|
||
if !projectFound { | ||
for _, p := range diggerConfig.Projects { | ||
availableProjects = append(availableProjects, p.Name) | ||
} | ||
|
||
// This would normally call usage.ReportErrorAndExit | ||
expectedErrorMsg := "Project 'invalid-project' not found in digger configuration. Available projects: [project-1 project-2]" | ||
|
||
// Verify the error message format | ||
actualErrorMsg := "Project '" + requestedProject + "' not found in digger configuration. Available projects: " + | ||
"[project-1 project-2]" | ||
assert.Equal(t, expectedErrorMsg, actualErrorMsg) | ||
htplbc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to replace this block of looping with a call to
findProjectInConfig
so the tests now measure real codeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@motatoes updated