Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Fix unescaped log path and KQL in DCR template JSON

Cursor Agent28a6b2b0d57a
diff --git a/cli/beacon/internal/endpoint/rapid7/rapid7.go b/cli/beacon/internal/endpoint/rapid7/rapid7.go
index 8b305dce..cb46102d 100644
--- a/cli/beacon/internal/endpoint/rapid7/rapid7.go
+++ b/cli/beacon/internal/endpoint/rapid7/rapid7.go
@@ -19,6 +19,7 @@ type File struct {
Name string
Content string
TemplateLogPath bool
+ JSONEscape bool
}
func Files() []File {
diff --git a/cli/beacon/internal/endpoint/sentinel/sentinel.go b/cli/beacon/internal/endpoint/sentinel/sentinel.go
index 2555e833..073b46b1 100644
--- a/cli/beacon/internal/endpoint/sentinel/sentinel.go
+++ b/cli/beacon/internal/endpoint/sentinel/sentinel.go
@@ -21,7 +21,7 @@ func Files() []siempack.File {
{Name: "README.md", Content: mustRead("pack/README.md")},
{Name: "dcr-transform.kql", Content: DCRTransform()},
{Name: "table-schema.json", Content: mustRead("pack/table-schema.json")},
- {Name: "dcr-template.json", Content: renderDCRTemplate(), TemplateLogPath: true},
+ {Name: "dcr-template.json", Content: renderDCRTemplate(), TemplateLogPath: true, JSONEscape: true},
{Name: "queries.kql", Content: mustRead("pack/queries.kql")},
{Name: "detections.kql", Content: mustRead("pack/detections.kql")},
{Name: "sample-event.jsonl", Content: mustRead("pack/sample-event.jsonl")},
@@ -44,7 +44,7 @@ func InstallPack(outputDir, logPath string) error {
func renderDCRTemplate() string {
tmpl := mustRead("pack/dcr-template.json")
- return strings.ReplaceAll(tmpl, "{{DCR_TRANSFORM}}", minifyKQL(DCRTransform()))
+ return strings.ReplaceAll(tmpl, "{{DCR_TRANSFORM}}", siempack.JSONEscapeForString(minifyKQL(DCRTransform())))
}
func minifyKQL(kql string) string {
diff --git a/cli/beacon/internal/endpoint/sentinel/sentinel_test.go b/cli/beacon/internal/endpoint/sentinel/sentinel_test.go
index 6b42a7cf..5413a6ab 100644
--- a/cli/beacon/internal/endpoint/sentinel/sentinel_test.go
+++ b/cli/beacon/internal/endpoint/sentinel/sentinel_test.go
@@ -54,6 +54,24 @@ func TestInstallPackWritesExpectedFiles(t *testing.T) {
}
}
+func TestInstallPackBackslashPathProducesValidJSON(t *testing.T) {
+ dir := t.TempDir()
+ if err := InstallPack(dir, `C:\Users\me\beacon\runtime.jsonl`); err != nil {
+ t.Fatalf("InstallPack returned error: %v", err)
+ }
+ dcrTemplate, err := os.ReadFile(filepath.Join(dir, "dcr-template.json"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ var doc map[string]interface{}
+ if err := json.Unmarshal(dcrTemplate, &doc); err != nil {
+ t.Fatalf("DCR template with backslash path is not valid JSON: %v\n%s", err, dcrTemplate)
+ }
+ if !strings.Contains(string(dcrTemplate), `C:\\Users\\me\\beacon\\runtime.jsonl`) {
+ t.Fatalf("DCR template missing escaped backslash path: %s", dcrTemplate)
+ }
+}
+
func TestPackJSONFilesAreValid(t *testing.T) {
for _, path := range []string{
"pack/table-schema.json",
diff --git a/cli/beacon/internal/endpoint/siempack/siempack.go b/cli/beacon/internal/endpoint/siempack/siempack.go
index e58d5f90..f9144d1b 100644
--- a/cli/beacon/internal/endpoint/siempack/siempack.go
+++ b/cli/beacon/internal/endpoint/siempack/siempack.go
@@ -1,6 +1,7 @@
package siempack
import (
+ "encoding/json"
"io/fs"
"os"
"path/filepath"
@@ -13,6 +14,7 @@ type File struct {
Name string
Content string
TemplateLogPath bool
+ JSONEscape bool
}
func ReadFile(fsys fs.FS, path string) (string, error) {
@@ -27,6 +29,13 @@ func RenderLogPath(content, logPath string) string {
return strings.ReplaceAll(content, logPathToken, logPath)
}
+// JSONEscapeForString escapes s for safe embedding inside a JSON string literal
+// (the surrounding quotes are not included).
+func JSONEscapeForString(s string) string {
+ b, _ := json.Marshal(s)
+ return string(b[1 : len(b)-1])
+}
+
func Install(outputDir string, files []File, logPath string) error {
if err := os.MkdirAll(outputDir, 0755); err != nil {
return err
@@ -34,7 +43,11 @@ func Install(outputDir string, files []File, logPath string) error {
for _, file := range files {
content := file.Content
if file.TemplateLogPath {
- content = RenderLogPath(content, logPath)
+ lp := logPath
+ if file.JSONEscape {
+ lp = JSONEscapeForString(lp)
+ }
+ content = RenderLogPath(content, lp)
}
if err := os.WriteFile(filepath.Join(outputDir, file.Name), []byte(content), Mode(file.Name)); err != nil {
return err
diff --git a/cli/beacon/internal/endpoint/siempack/siempack_test.go b/cli/beacon/internal/endpoint/siempack/siempack_test.go
index 1c559c9a..93b5a822 100644
--- a/cli/beacon/internal/endpoint/siempack/siempack_test.go
+++ b/cli/beacon/internal/endpoint/siempack/siempack_test.go
@@ -15,6 +15,22 @@ func TestRenderLogPath(t *testing.T) {
}
}
+func TestJSONEscapeForString(t *testing.T) {
+ tests := []struct {
+ in, want string
+ }{
+ {"/var/log/beacon/runtime.jsonl", "/var/log/beacon/runtime.jsonl"},
+ {`C:\Users\me\beacon\runtime.jsonl`, `C:\\Users\\me\\beacon\\runtime.jsonl`},
+ {`path with "quotes"`, `path with \"quotes\"`},
+ }
+ for _, tt := range tests {
+ got := JSONEscapeForString(tt.in)
+ if got != tt.want {
+ t.Errorf("JSONEscapeForString(%q) = %q, want %q", tt.in, got, tt.want)
+ }
+ }
+}
+
func TestReadFile(t *testing.T) {
got, err := ReadFile(fstest.MapFS{
"pack/example.txt": {Data: []byte("hello")},
diff --git a/cli/beacon/internal/endpoint/sumo/sumo.go b/cli/beacon/internal/endpoint/sumo/sumo.go
index 199823b3..cc6149a3 100644
--- a/cli/beacon/internal/endpoint/sumo/sumo.go
+++ b/cli/beacon/internal/endpoint/sumo/sumo.go
@@ -19,6 +19,7 @@ type File struct {
Name string
Content string
TemplateLogPath bool
+ JSONEscape bool
}
func Files() []File {