-
Notifications
You must be signed in to change notification settings - Fork 20
refactor(provider/env): document env var contract and validate reserved names #690
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package provider | ||
|
|
||
| import ( | ||
| "slices" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestToEnvironment_ContainsProviderID(t *testing.T) { | ||
| ws := &Workspace{ | ||
| ID: "test-workspace", | ||
| Context: "default", | ||
| Provider: WorkspaceProviderConfig{ | ||
| Name: "test-provider", | ||
| }, | ||
| Source: WorkspaceSource{}, | ||
| } | ||
|
|
||
| environ := ToEnvironment(ws, nil, nil, nil) | ||
|
|
||
| assertEnvContains(t, environ, "PROVIDER_ID", "test-provider") | ||
| assertEnvContains(t, environ, "WORKSPACE_PROVIDER", "test-provider") | ||
| assertEnvContains(t, environ, "WORKSPACE_ID", "test-workspace") | ||
| } | ||
|
|
||
| func TestToEnvironment_DoesNotDuplicateDevpodProvider(t *testing.T) { | ||
| ws := &Workspace{ | ||
| ID: "test-workspace", | ||
| Context: "default", | ||
| Provider: WorkspaceProviderConfig{ | ||
| Name: "test-provider", | ||
| }, | ||
| Source: WorkspaceSource{}, | ||
| } | ||
|
|
||
| environ := ToEnvironment(ws, nil, nil, nil) | ||
|
|
||
| // DEVPOD_PROVIDER is reserved by the --provider CLI flag. | ||
| // It may appear from os.Environ() but must not be explicitly added. | ||
| count := 0 | ||
| for _, entry := range environ { | ||
| if strings.HasPrefix(entry, "DEVPOD_PROVIDER=") { | ||
| count++ | ||
| } | ||
| } | ||
| if count > 1 { | ||
| t.Errorf( | ||
| "found %d DEVPOD_PROVIDER entries; expected at most 1 (from os.Environ)", | ||
| count, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func TestToEnvironment_IncludesExtraEnv(t *testing.T) { | ||
| extra := map[string]string{"CUSTOM_VAR": "custom_value"} | ||
| environ := ToEnvironment(nil, nil, nil, extra) | ||
|
|
||
| assertEnvContains(t, environ, "CUSTOM_VAR", "custom_value") | ||
| } | ||
|
|
||
| func assertEnvContains(t *testing.T, environ []string, key, value string) { | ||
| t.Helper() | ||
| expected := key + "=" + value | ||
| if !slices.Contains(environ, expected) { | ||
| t.Errorf("expected %s in environment, not found", expected) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,29 @@ var ProviderNameRegEx = regexp.MustCompile(`[^a-z0-9\-]+`) | |
|
|
||
| var optionNameRegEx = regexp.MustCompile(`[^A-Z0-9_]+`) | ||
|
|
||
| // reservedOptionNames are env var names set by the DevPod runtime | ||
| // (via GetBaseEnvironment, ToOptionsWorkspace, ToOptionsMachine). | ||
| // Provider options must not use these names as they would silently | ||
| // overwrite system-set values in the subprocess environment. | ||
| var reservedOptionNames = map[string]bool{ | ||
| "PROVIDER_ID": true, | ||
| "PROVIDER_CONTEXT": true, | ||
| "PROVIDER_FOLDER": true, | ||
| "WORKSPACE_ID": true, | ||
| "WORKSPACE_UID": true, | ||
| "WORKSPACE_PROVIDER": true, | ||
| "WORKSPACE_CONTEXT": true, | ||
| "WORKSPACE_FOLDER": true, | ||
| "WORKSPACE_SOURCE": true, | ||
| "WORKSPACE_ORIGIN": true, | ||
| "WORKSPACE_PICTURE": true, | ||
| "MACHINE_ID": true, | ||
| "MACHINE_CONTEXT": true, | ||
| "MACHINE_FOLDER": true, | ||
| "MACHINE_PROVIDER": true, | ||
| "LOFT_PROJECT": true, | ||
| } | ||
|
Comment on lines
+25
to
+42
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. Reserved-name validation is incomplete and misses contract-critical env vars.
Suggested patch var reservedOptionNames = map[string]bool{
+ "DEVPOD": true,
+ "DEVPOD_OS": true,
+ "DEVPOD_ARCH": true,
+ "DEVPOD_LOG_LEVEL": true,
+ "DEVPOD_PROVIDER": true,
"PROVIDER_ID": true,
"PROVIDER_CONTEXT": true,
"PROVIDER_FOLDER": true,
"WORKSPACE_ID": true,
"WORKSPACE_UID": true,
"WORKSPACE_PROVIDER": true,
"WORKSPACE_CONTEXT": true,
"WORKSPACE_FOLDER": true,
"WORKSPACE_SOURCE": true,
"WORKSPACE_ORIGIN": true,
"WORKSPACE_PICTURE": true,
"MACHINE_ID": true,
"MACHINE_CONTEXT": true,
"MACHINE_FOLDER": true,
"MACHINE_PROVIDER": true,
"LOFT_PROJECT": true,
}Also applies to: 101-107 🤖 Prompt for AI Agents |
||
|
|
||
| var allowedTypes = []string{ | ||
| "string", | ||
| "multiline", | ||
|
|
@@ -75,6 +98,14 @@ func validate(config *ProviderConfig) error { | |
| ) | ||
| } | ||
|
|
||
| if reservedOptionNames[optionName] { | ||
| return fmt.Errorf( | ||
| "provider option '%s' uses a reserved environment variable name; "+ | ||
| "choose a different name to avoid overwriting system-set values", | ||
| optionName, | ||
| ) | ||
| } | ||
|
|
||
| // validate option validation | ||
| if optionValue.ValidationPattern != "" { | ||
| _, err := regexp.Compile(optionValue.ValidationPattern) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package provider | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/skevetter/devpod/pkg/types" | ||
| ) | ||
|
|
||
| func TestValidate_RejectsReservedOptionNames(t *testing.T) { | ||
| reserved := []string{ | ||
| "PROVIDER_ID", | ||
| "PROVIDER_CONTEXT", | ||
| "PROVIDER_FOLDER", | ||
| "WORKSPACE_ID", | ||
| "WORKSPACE_UID", | ||
| "WORKSPACE_PROVIDER", | ||
| "WORKSPACE_CONTEXT", | ||
| "WORKSPACE_FOLDER", | ||
| "WORKSPACE_SOURCE", | ||
| "WORKSPACE_ORIGIN", | ||
| "WORKSPACE_PICTURE", | ||
| "MACHINE_ID", | ||
| "MACHINE_CONTEXT", | ||
| "MACHINE_FOLDER", | ||
| "MACHINE_PROVIDER", | ||
| "LOFT_PROJECT", | ||
| } | ||
|
|
||
| for _, name := range reserved { | ||
| cfg := &ProviderConfig{ | ||
| Name: "test-provider", | ||
| Options: map[string]*types.Option{ | ||
| name: {Description: "test"}, | ||
| }, | ||
| } | ||
| err := validate(cfg) | ||
| if err == nil { | ||
| t.Errorf("expected error for reserved option name %q, got nil", name) | ||
| } | ||
|
Comment on lines
+29
to
+39
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. Reserved-name test can pass for unrelated validation errors.
Suggested patch import (
+ "strings"
"testing"
"github.com/skevetter/devpod/pkg/types"
)
@@
for _, name := range reserved {
cfg := &ProviderConfig{
Name: "test-provider",
Options: map[string]*types.Option{
name: {Description: "test"},
},
+ Exec: ProviderCommands{
+ Command: []string{"echo hello"},
+ },
}
err := validate(cfg)
- if err == nil {
- t.Errorf("expected error for reserved option name %q, got nil", name)
+ if err == nil || !strings.Contains(err.Error(), "reserved environment variable name") {
+ t.Errorf("expected reserved-name error for option %q, got: %v", name, err)
}
}
}🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
|
|
||
| func TestValidate_AllowsNonReservedOptionNames(t *testing.T) { | ||
| cfg := &ProviderConfig{ | ||
| Name: "test-provider", | ||
| Options: map[string]*types.Option{ | ||
| "MY_CUSTOM_OPTION": {Description: "test"}, | ||
| "AWS_REGION": {Description: "test"}, | ||
| }, | ||
| Exec: ProviderCommands{ | ||
| Command: []string{"echo hello"}, | ||
| }, | ||
| } | ||
| err := validate(cfg) | ||
| if err != nil { | ||
| t.Errorf("expected no error for non-reserved names, got: %v", err) | ||
| } | ||
| } | ||
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.
Documented DEVPOD_PROVIDER restriction is not enforced for
extraEnv.The comment says
DEVPOD_PROVIDERmust not be explicitly set, but Line 55-Line 57 blindly appends allextraEnventries, includingDEVPOD_PROVIDER.Suggested patch
for k, v := range extraEnv { + if k == "DEVPOD_PROVIDER" { + continue + } osEnviron = append(osEnviron, k+"="+v) }Also applies to: 55-57
🤖 Prompt for AI Agents