mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-27 11:31:06 +01:00
Add Validate() method to NodeConfig that implements the CustomValidator interface. The method checks all services and files for invalid shell metacharacters (&, $(), etc.) to prevent shell injection attacks. Testing: Added validation_test.go with 6 table-driven test cases covering valid inputs and various shell metacharacter injection attempts.
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package proxmox
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/yusing/godoxy/internal/serialization"
|
|
)
|
|
|
|
func TestValidateCommandArgs(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
yamlCfg string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "valid_services",
|
|
yamlCfg: `services: ["foo", "bar"]`,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "invalid_services",
|
|
yamlCfg: `services: ["foo", "bar & baz"]`,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "invalid_services_with_$(",
|
|
yamlCfg: `services: ["foo", "bar & $(echo 'hello')"]`,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "valid_files",
|
|
yamlCfg: `files: ["foo", "bar"]`,
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "invalid_files",
|
|
yamlCfg: `files: ["foo", "bar & baz"]`,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var cfg NodeConfig
|
|
err := serialization.UnmarshalValidateYAML([]byte(tt.yamlCfg), &cfg)
|
|
if tt.wantErr {
|
|
require.Error(t, err)
|
|
require.ErrorContains(t, err, "input contains invalid characters")
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|