fix(chat): check dashboard links by shape, not the page catalogue
This commit is contained in:
3 files changed
+38
-20
No files matched your search
+21
-16
@@ -20,7 +20,6 @@ import (
|
||||
"github.com/samcharles93/archie-core/modules/governance"
|
||||
"github.com/samcharles93/archie-core/modules/judgement"
|
||||
"github.com/samcharles93/archie-core/modules/memory"
|
||||
"github.com/samcharles93/archie-core/ui/pages"
|
||||
)
|
||||
|
||||
// principal is who hard rules see acting when the chat agent calls an action.
|
||||
@@ -496,25 +495,31 @@ func typedTool[In any](name, description, schema string, run func(context.Contex
|
||||
})
|
||||
}
|
||||
|
||||
var pageID = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._:-]*$`)
|
||||
var pageSegment = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._:-]*$`)
|
||||
|
||||
// dashboardPath accepts a page from the dashboard's route catalogue or one item
|
||||
// under it, and nothing that could leave the dashboard.
|
||||
// maxPageDepth is the most segments a dashboard path has: a section, a page
|
||||
// under it, and one item.
|
||||
const maxPageDepth = 3
|
||||
|
||||
// dashboardPath accepts a path the dashboard's router could serve and nothing
|
||||
// that could leave the dashboard. The dashboard ships separately, so it, not
|
||||
// the server, decides whether a page exists and hides a link to one it lacks.
|
||||
func dashboardPath(path string) bool {
|
||||
catalogue, err := pages.All()
|
||||
if err != nil {
|
||||
if path == "/" {
|
||||
return true
|
||||
}
|
||||
rest, ok := strings.CutPrefix(path, "/")
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
for _, p := range catalogue {
|
||||
if path == p.Path {
|
||||
return true
|
||||
}
|
||||
if p.Path != "/" && strings.HasPrefix(path, p.Path+"/") {
|
||||
rest := strings.TrimPrefix(path, p.Path+"/")
|
||||
if pageID.MatchString(rest) {
|
||||
return true
|
||||
}
|
||||
segments := strings.Split(rest, "/")
|
||||
if len(segments) > maxPageDepth {
|
||||
return false
|
||||
}
|
||||
for _, s := range segments {
|
||||
if !pageSegment.MatchString(s) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false
|
||||
return true
|
||||
}
|
||||
@@ -3,7 +3,8 @@ package chat
|
||||
import "testing"
|
||||
|
||||
// TestDashboardPathStaysInTheDashboard guards the link the agent can offer
|
||||
// the operator: it must never lead off the dashboard or outside its pages.
|
||||
// the operator: it must never lead off the dashboard. Whether the page exists
|
||||
// is the dashboard's call.
|
||||
func TestDashboardPathStaysInTheDashboard(t *testing.T) {
|
||||
cases := map[string]bool{
|
||||
"/": true,
|
||||
@@ -12,13 +13,16 @@ func TestDashboardPathStaysInTheDashboard(t *testing.T) {
|
||||
"/controls/github:merge_pr": true,
|
||||
"/proposals": true,
|
||||
"/settings/extensions": true,
|
||||
"/settings/extensions/x": true,
|
||||
"//evil.example/runs": false,
|
||||
"https://evil.example/runs": false,
|
||||
"javascript:alert(1)": false,
|
||||
"/runs/../settings": false,
|
||||
"/runs/a/b": false,
|
||||
"/runs/..": false,
|
||||
"/admin": false,
|
||||
"/runs/": false,
|
||||
"/runs/%2e%2e": false,
|
||||
"/runs\\evil.example": false,
|
||||
"/a/b/c/d": false,
|
||||
"runs": false,
|
||||
}
|
||||
for path, want := range cases {
|
||||
|
||||
Reference in new issue
Block a user