Index debug sensitivity paths once per capture
This commit is contained in:
@@ -61,18 +61,30 @@ func newRunEvidenceRecorder(recorder *frameworkdebug.Recorder, result *configkit
|
||||
return writer
|
||||
}
|
||||
sensitive := config.Definition("").Sensitive
|
||||
paths := result.Metadata().HistoricalPaths()
|
||||
register := func(masks map[string][][]string, id string, prefix configkit.Path) {
|
||||
for _, field := range paths {
|
||||
if !strings.HasPrefix(field.String(), prefix.String()+"/") || !sensitive(field) {
|
||||
continue
|
||||
}
|
||||
segments := strings.Split(strings.TrimPrefix(field.String(), prefix.String()+"/"), "/")
|
||||
for index, segment := range segments {
|
||||
segments[index] = strings.ReplaceAll(strings.ReplaceAll(segment, "~1", "/"), "~0", "~")
|
||||
}
|
||||
masks[id] = append(masks[id], segments)
|
||||
masksByPrefix := map[string][][]string{}
|
||||
for _, field := range result.Metadata().HistoricalPaths() {
|
||||
segments := strings.Split(strings.TrimPrefix(field.String(), "/"), "/")
|
||||
extraParams := -1
|
||||
// Match native root/collection resource paths before unescaping keys.
|
||||
if len(segments) >= 5 && segments[0] == "promptkit" &&
|
||||
(segments[1] == "profiles" || segments[1] == "backends") && segments[3] == "extra_params" {
|
||||
extraParams = 3
|
||||
} else if len(segments) >= 7 && segments[0] == "promptkit" && segments[1] == "collections" &&
|
||||
(segments[3] == "profiles" || segments[3] == "backends") && segments[5] == "extra_params" {
|
||||
extraParams = 5
|
||||
}
|
||||
if extraParams < 0 || !sensitive(field) {
|
||||
continue
|
||||
}
|
||||
prefix := "/" + strings.Join(segments[:extraParams+1], "/")
|
||||
suffix := segments[extraParams+1:]
|
||||
for i, segment := range suffix {
|
||||
suffix[i] = strings.ReplaceAll(strings.ReplaceAll(segment, "~1", "/"), "~0", "~")
|
||||
}
|
||||
masksByPrefix[prefix] = append(masksByPrefix[prefix], suffix)
|
||||
}
|
||||
register := func(masks map[string][][]string, id string, prefix configkit.Path) {
|
||||
masks[id] = append(masks[id], masksByPrefix[prefix.String()]...)
|
||||
}
|
||||
root := configkit.Keys("promptkit")
|
||||
for index, profile := range result.Config.PromptKit.Profiles {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -15,6 +16,53 @@ import (
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestRunCaptureRoutesRootAndCollectionSensitivityMasks(t *testing.T) {
|
||||
result, err := configkit.LoadLayered(configkit.Input{Source: configkit.Bytes("mixed resources", []byte(`version: 6
|
||||
promptkit:
|
||||
profiles:
|
||||
- id: chosen
|
||||
extra_params:
|
||||
token: root-profile
|
||||
nested/key:
|
||||
a~b: [{authorization: root-nested, caption: ordinary-caption}]
|
||||
backends:
|
||||
- id: chosen
|
||||
endpoint: https://example.invalid/v1
|
||||
extra_params: {password: root-backend}
|
||||
collections:
|
||||
fixture:
|
||||
profiles:
|
||||
- id: chosen
|
||||
extra_params: {token: collection-profile}
|
||||
backends:
|
||||
- id: chosen
|
||||
endpoint: https://example.invalid/v1
|
||||
extra_params: {password: collection-backend}
|
||||
`))}, configkit.LayeredOptions[config.Document]{Definition: config.Definition(t.TempDir())})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writer := newRunEvidenceRecorder(nil, result).(*runEvidenceRecorder)
|
||||
nested := map[string]any{"a~b": []any{map[string]any{"authorization": "root-nested", "caption": "ordinary-caption"}}}
|
||||
for _, namespace := range []string{"", "fixture/"} {
|
||||
target := promptkit.ExecutionTarget{ExtraParams: map[string]any{"token": "original-token", "password": "original-password", "nested/key": nested, "caption": "ordinary-caption"}}
|
||||
masked := writer.target(target, namespace+"chosen", namespace+"chosen")
|
||||
if masked.ExtraParams["token"] != "[REDACTED]" || masked.ExtraParams["password"] != "[REDACTED]" || masked.ExtraParams["caption"] != "ordinary-caption" {
|
||||
t.Fatalf("misrouted resource masks for %q: %#v", namespace, masked)
|
||||
}
|
||||
wantNested := nested
|
||||
if namespace == "" {
|
||||
wantNested = map[string]any{"a~b": []any{map[string]any{"authorization": "[REDACTED]", "caption": "ordinary-caption"}}}
|
||||
}
|
||||
if !reflect.DeepEqual(masked.ExtraParams["nested/key"], wantNested) {
|
||||
t.Fatalf("nested mask crossed resource boundary for %q: %#v", namespace, masked)
|
||||
}
|
||||
if target.ExtraParams["token"] != "original-token" || nested["a~b"].([]any)[0].(map[string]any)["authorization"] != "root-nested" {
|
||||
t.Fatal("native snapshot was mutated")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCaptureUsesFrozenSecretAwareConfiguration(t *testing.T) {
|
||||
result, err := configkit.LoadLayered(configkit.Input{Source: configkit.Bytes("captured application", []byte(`version: 6
|
||||
promptkit:
|
||||
|
||||
Reference in New Issue
Block a user