245 lines
6.4 KiB
Go
245 lines
6.4 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"ispctl/model"
|
|
)
|
|
|
|
func TestAuditModuleIntersection(t *testing.T) {
|
|
schema, err := json.Marshal(map[string]any{
|
|
"type": "object",
|
|
"properties": map[string]any{
|
|
"ui": map[string]any{"type": "string", "x-ui-exposed": true},
|
|
"plain": map[string]any{"type": "string"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
configData := map[string]any{
|
|
"ui": "_Var(s1) _Var(t1)",
|
|
"plain": "_Var(s1)",
|
|
}
|
|
secrets := map[string]struct{}{"s1": {}}
|
|
|
|
violations, err := auditModule("m1", schema, configData, secrets)
|
|
if err != nil {
|
|
t.Fatalf("auditModule: %v", err)
|
|
}
|
|
// Only the UI leaf with the secret reference is reported; the same
|
|
// reference in the plain leaf and the TEXT reference in the UI leaf are
|
|
// not.
|
|
want := []model.AuditViolation{{
|
|
Module: "m1",
|
|
Path: "$.ui",
|
|
Variables: []string{"s1"},
|
|
Reason: "secret variable in ui-exposed field",
|
|
}}
|
|
if !reflect.DeepEqual(violations, want) {
|
|
t.Fatalf("violations = %v, want %v", violations, want)
|
|
}
|
|
}
|
|
|
|
func TestAuditModuleSecretOutsideUI(t *testing.T) {
|
|
schema, err := json.Marshal(map[string]any{
|
|
"type": "object",
|
|
"properties": map[string]any{
|
|
"plain": map[string]any{"type": "string"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
violations, err := auditModule(
|
|
"m1",
|
|
schema,
|
|
map[string]any{"plain": "_Var(s1)"},
|
|
map[string]struct{}{"s1": {}},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("auditModule: %v", err)
|
|
}
|
|
if len(violations) != 0 {
|
|
t.Fatalf("violations = %v, want none: a secret outside ui-exposed fields is not a violation", violations)
|
|
}
|
|
}
|
|
|
|
func TestAuditModuleArrayPath(t *testing.T) {
|
|
schema, err := json.Marshal(map[string]any{
|
|
"type": "object",
|
|
"properties": map[string]any{
|
|
"clients": map[string]any{
|
|
"type": "array",
|
|
"items": map[string]any{"type": "string", "x-ui-exposed": true},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
configData := map[string]any{
|
|
"clients": []any{"_Var(s1)", "nope"},
|
|
}
|
|
|
|
violations, err := auditModule("m1", schema, configData, map[string]struct{}{"s1": {}})
|
|
if err != nil {
|
|
t.Fatalf("auditModule: %v", err)
|
|
}
|
|
want := []model.AuditViolation{{Module: "m1", Path: "$.clients[]", Variables: []string{"s1"}, Reason: "secret variable in ui-exposed field"}}
|
|
if !reflect.DeepEqual(violations, want) {
|
|
t.Fatalf("violations = %v, want %v", violations, want)
|
|
}
|
|
}
|
|
|
|
func TestAuditModuleCompositeReported(t *testing.T) {
|
|
// the report covers references hidden inside composite JSON strings
|
|
// (report-only in stage 1, the policy itself does not apply to them)
|
|
schema, err := json.Marshal(map[string]any{
|
|
"type": "object",
|
|
"properties": map[string]any{
|
|
"secrets": map[string]any{"type": "string", "x-ui-exposed": true},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
composite, err := json.Marshal(map[string]any{"a": "_Var(s1)"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
configData := map[string]any{
|
|
"secrets": string(composite),
|
|
}
|
|
|
|
violations, err := auditModule("m1", schema, configData, map[string]struct{}{"s1": {}})
|
|
if err != nil {
|
|
t.Fatalf("auditModule: %v", err)
|
|
}
|
|
want := []model.AuditViolation{{Module: "m1", Path: "$.secrets", Variables: []string{"s1"}, Reason: "secret variable in ui-exposed field"}}
|
|
if !reflect.DeepEqual(violations, want) {
|
|
t.Fatalf("violations = %v, want %v", violations, want)
|
|
}
|
|
}
|
|
|
|
func TestAuditModuleEmptySchema(t *testing.T) {
|
|
violations, err := auditModule("m1", nil, map[string]any{"a": "_Var(s1)"}, map[string]struct{}{"s1": {}})
|
|
if err != nil {
|
|
t.Fatalf("auditModule: %v", err)
|
|
}
|
|
if len(violations) != 0 {
|
|
t.Fatalf("violations = %v, want none: an empty schema exposes no fields", violations)
|
|
}
|
|
}
|
|
|
|
func sensitiveTestSchema(t *testing.T) json.RawMessage {
|
|
t.Helper()
|
|
schema, err := json.Marshal(map[string]any{
|
|
"type": "object",
|
|
"properties": map[string]any{
|
|
"token": map[string]any{"type": "string", "x-sensitive": true},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return schema
|
|
}
|
|
|
|
func TestAuditModuleSensitiveLiteral(t *testing.T) {
|
|
violations, err := auditModule(
|
|
"m1",
|
|
sensitiveTestSchema(t),
|
|
map[string]any{"token": "legacy-pass-123"},
|
|
map[string]struct{}{"s1": {}},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("auditModule: %v", err)
|
|
}
|
|
want := []model.AuditViolation{{
|
|
Module: "m1",
|
|
Path: "$.token",
|
|
Variables: []string{},
|
|
Reason: "plain value in sensitive field",
|
|
}}
|
|
if !reflect.DeepEqual(violations, want) {
|
|
t.Fatalf("violations = %v, want %v", violations, want)
|
|
}
|
|
}
|
|
|
|
func TestAuditModuleSensitiveNonSecretRef(t *testing.T) {
|
|
violations, err := auditModule(
|
|
"m1",
|
|
sensitiveTestSchema(t),
|
|
map[string]any{"token": "_Var(t1)"},
|
|
map[string]struct{}{"s1": {}},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("auditModule: %v", err)
|
|
}
|
|
want := []model.AuditViolation{{
|
|
Module: "m1",
|
|
Path: "$.token",
|
|
Variables: []string{"t1"},
|
|
Reason: "references non-SECRET variable t1",
|
|
}}
|
|
if !reflect.DeepEqual(violations, want) {
|
|
t.Fatalf("violations = %v, want %v", violations, want)
|
|
}
|
|
}
|
|
|
|
func TestAuditModuleSensitiveCompliant(t *testing.T) {
|
|
for name, config := range map[string]string{
|
|
"secret reference": `{"token": "_Var(s1)"}`,
|
|
"empty value": `{"token": ""}`,
|
|
"absent field": `{}`,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
var configData map[string]any
|
|
if err := json.Unmarshal([]byte(config), &configData); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
violations, err := auditModule("m1", sensitiveTestSchema(t), configData, map[string]struct{}{"s1": {}})
|
|
if err != nil {
|
|
t.Fatalf("auditModule: %v", err)
|
|
}
|
|
if len(violations) != 0 {
|
|
t.Fatalf("violations = %v, want none", violations)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAuditModuleBothClasses(t *testing.T) {
|
|
schema, err := json.Marshal(map[string]any{
|
|
"type": "object",
|
|
"properties": map[string]any{
|
|
"secrets": map[string]any{"type": "string", "x-ui-exposed": true},
|
|
"token": map[string]any{"type": "string", "x-sensitive": true},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
violations, err := auditModule(
|
|
"m1",
|
|
schema,
|
|
map[string]any{"secrets": "_Var(s1)", "token": "plain-pass"},
|
|
map[string]struct{}{"s1": {}},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("auditModule: %v", err)
|
|
}
|
|
want := []model.AuditViolation{
|
|
{Module: "m1", Path: "$.secrets", Variables: []string{"s1"}, Reason: "secret variable in ui-exposed field"},
|
|
{Module: "m1", Path: "$.token", Variables: []string{}, Reason: "plain value in sensitive field"},
|
|
}
|
|
if !reflect.DeepEqual(violations, want) {
|
|
t.Fatalf("violations = %v, want %v", violations, want)
|
|
}
|
|
}
|