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 {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { ArrowUpRight } from "@lucide/vue";
|
||||
import { computed } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
|
||||
/**
|
||||
* A dashboard_navigate result: a clickable chip that routes the operator to a
|
||||
@@ -10,12 +12,18 @@ import { ArrowUpRight } from "@lucide/vue";
|
||||
* A RouterLink rather than a `#/path` anchor: history routing means the server
|
||||
* stores bare paths, and an anchor would reload the whole app to change one
|
||||
* segment.
|
||||
*
|
||||
* The server only checks that the path stays on the dashboard, so a path no
|
||||
* route serves shows as plain text instead of a link to the not-found page.
|
||||
*/
|
||||
defineProps<{ path: string; label?: string }>();
|
||||
const props = defineProps<{ path: string; label?: string }>();
|
||||
const router = useRouter();
|
||||
const known = computed(() => router.resolve(props.path).name !== "not-found");
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<RouterLink
|
||||
v-if="known"
|
||||
:to="path"
|
||||
:title="`Go to ${label || path}`"
|
||||
class="inline-flex max-w-full items-center gap-1.5 rounded-md border border-primary/45 bg-primary-soft px-2.5 py-1 text-caption font-semibold text-link transition-colors hover:border-primary hover:bg-primary/15 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
||||
@@ -23,4 +31,5 @@ defineProps<{ path: string; label?: string }>();
|
||||
<ArrowUpRight aria-hidden="true" class="size-3.5 shrink-0" />
|
||||
<span class="truncate">{{ label || path }}</span>
|
||||
</RouterLink>
|
||||
<span v-else class="text-caption text-muted-foreground">{{ label || path }}</span>
|
||||
</template>
|
||||
Reference in new issue
Block a user