mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 12:24:44 +00:00
The recursive-force-delete rules required the recursive and the force flag in one token, so rm -r -f /, rm -R -f / and rm -f -r / passed while the combined rm -rf / was denied. The v1 and v2 seed patterns now read the short flags combined or separate, and a v4 seed rewrites the rows an upgraded install already has (leaving operator-edited patterns alone).
170 lines
5.4 KiB
Go
170 lines
5.4 KiB
Go
package db
|
|
|
|
import (
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
// The built-in "delete-like endpoint path" rule matches the entire tool_input JSON
|
|
// string, so the cases are given directly in JSON form, consistent with the subject
|
|
// the Interceptor actually receives.
|
|
func TestDeleteEndpointPathPattern(t *testing.T) {
|
|
re := regexp.MustCompile(deleteEndpointPathPattern)
|
|
|
|
hit := []string{
|
|
`{"command":"curl -s 'http://t.com/api/user/delete?id=1'"}`, // GET hitting a delete endpoint
|
|
`{"command":"curl -X POST http://t.com/admin/delete -d id=1"}`, // POST hitting a delete endpoint
|
|
`{"command":"curl 'http://t.com/api/deleteAll'"}`,
|
|
`{"command":"curl 'http://t.com/api/delete_user?id=1'"}`,
|
|
`{"command":"curl 'http://t.com/api/delete-user?id=1'"}`,
|
|
`{"url":"http://t.com/api/remove?id=1"}`,
|
|
`{"command":"curl http://t.com/files/unlink/3"}`,
|
|
`{"command":"curl http://t.com/api/del?id=2"}`,
|
|
`{"command":"curl -X POST http://t/v1/erase"}`,
|
|
`{"command":"curl http://t/admin/destroyAll"}`, // v1's path rule forbids a suffix; covered here
|
|
}
|
|
for _, s := range hit {
|
|
if !re.MatchString(s) {
|
|
t.Errorf("should have hit but was allowed: %s", s)
|
|
}
|
|
}
|
|
|
|
// The verb must be followed by a separator, so read-only paths like /delivery and
|
|
// /details are not falsely intercepted.
|
|
miss := []string{
|
|
`{"command":"curl 'http://t.com/api/delivery?id=1'"}`,
|
|
`{"command":"curl 'http://t.com/order/details'"}`,
|
|
`{"command":"curl 'http://t.com/api/delta/sync'"}`,
|
|
`{"command":"curl 'http://t.com/user/delegate'"}`,
|
|
`{"command":"curl 'http://delete.example.com/'"}`, // the delete verb appears in the domain, not the path
|
|
`{"command":"curl 'http://t.com/remote/status'"}`,
|
|
`{"command":"nmap -p80 10.0.0.1"}`,
|
|
}
|
|
for _, s := range miss {
|
|
if re.MatchString(s) {
|
|
t.Errorf("false interception: %s", s)
|
|
}
|
|
}
|
|
}
|
|
|
|
// GHSA-2crf: the recursive and the force flag are destructive whether they are
|
|
// combined (-rf, -fr) or written separately (-r -f, -R -f, -f -r). A recursive
|
|
// delete without force is not what this rule targets.
|
|
func TestRecursiveForceDeleteSeparateFlags(t *testing.T) {
|
|
re := regexp.MustCompile(recursiveForceDeletePattern)
|
|
hit := []string{
|
|
"rm -rf /",
|
|
"rm -fr /",
|
|
"rm -r -f /",
|
|
"rm -R -f /",
|
|
"rm -f -r /",
|
|
"rm -Rf /",
|
|
"rm -v -r -f /etc",
|
|
"rm -r --force /",
|
|
"rm --recursive --force /",
|
|
`{"command":"rm -r -f /"}`,
|
|
}
|
|
for _, s := range hit {
|
|
if !re.MatchString(s) {
|
|
t.Errorf("should have hit but was allowed: %s", s)
|
|
}
|
|
}
|
|
miss := []string{
|
|
"rm -r ./dir",
|
|
"rm -f ./file",
|
|
"rm ./file",
|
|
"rm -r -- -f /", // -f is an operand after --, not a flag
|
|
"ls -la",
|
|
}
|
|
for _, s := range miss {
|
|
if re.MatchString(s) {
|
|
t.Errorf("false interception: %s", s)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The v2 "Destructive system commands" rule takes the same separated flags for
|
|
// its rm-against-root form.
|
|
func TestDestructiveSystemCommandsSeparateFlags(t *testing.T) {
|
|
re := regexp.MustCompile(destructiveSystemCommandsPattern)
|
|
hit := []string{
|
|
"rm -rf /",
|
|
"rm -r -f /",
|
|
"rm -R -f /",
|
|
"rm -f -r /",
|
|
"rm -rf /tmp/x",
|
|
"mkfs /dev/sda",
|
|
"dd if=/dev/zero of=/dev/sda",
|
|
"shutdown -h now",
|
|
}
|
|
for _, s := range hit {
|
|
if !re.MatchString(s) {
|
|
t.Errorf("should have hit but was allowed: %s", s)
|
|
}
|
|
}
|
|
miss := []string{
|
|
"rm -r ./dir",
|
|
"rm -rf ./dir",
|
|
"rm -f -r ./dir",
|
|
"ls -la",
|
|
}
|
|
for _, s := range miss {
|
|
if re.MatchString(s) {
|
|
t.Errorf("false interception: %s", s)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The v4 seed rewrites the rows an install that already ran v1/v2 has, and
|
|
// leaves a rule the operator edited alone.
|
|
func TestSeedDefaultInterceptRulesV4RewritesOldPattern(t *testing.T) {
|
|
d := openTargetDB(t)
|
|
const v1Name = "[built-in] Recursive force delete rm -rf"
|
|
const v2Name = "[built-in] Destructive system commands"
|
|
t.Cleanup(func() {
|
|
_, _ = d.Exec(`UPDATE intercept_rules SET pattern=$1 WHERE name=$2`, recursiveForceDeletePattern, v1Name)
|
|
_, _ = d.Exec(`UPDATE intercept_rules SET pattern=$1 WHERE name=$2`, destructiveSystemCommandsPattern, v2Name)
|
|
_, _ = d.Exec(`DELETE FROM settings WHERE key='intercept_default_rules_v4'`)
|
|
_ = d.seedDefaultInterceptRulesV4()
|
|
})
|
|
pattern := func(name string) string {
|
|
t.Helper()
|
|
var p string
|
|
if err := d.QueryRow(`SELECT pattern FROM intercept_rules WHERE name=$1`, name).Scan(&p); err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
return p
|
|
}
|
|
for _, tc := range []struct{ name, old, want string }{
|
|
{v1Name, oldRecursiveForceDeletePattern, recursiveForceDeletePattern},
|
|
{v2Name, oldDestructiveSystemCommandsPattern, destructiveSystemCommandsPattern},
|
|
} {
|
|
if _, err := d.Exec(`UPDATE intercept_rules SET pattern=$1 WHERE name=$2`, tc.old, tc.name); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := d.Exec(`DELETE FROM settings WHERE key='intercept_default_rules_v4'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := d.seedDefaultInterceptRulesV4(); err != nil {
|
|
t.Fatalf("%s: %v", tc.name, err)
|
|
}
|
|
if got := pattern(tc.name); got != tc.want {
|
|
t.Errorf("%s: pattern=%q, want the corrected seed", tc.name, got)
|
|
}
|
|
}
|
|
// An operator-edited rule is not overwritten.
|
|
const edited = `(?i)operator-edit`
|
|
if _, err := d.Exec(`UPDATE intercept_rules SET pattern=$1 WHERE name=$2`, edited, v1Name); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := d.Exec(`DELETE FROM settings WHERE key='intercept_default_rules_v4'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := d.seedDefaultInterceptRulesV4(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := pattern(v1Name); got != edited {
|
|
t.Errorf("an operator-edited pattern was rewritten: %q", got)
|
|
}
|
|
}
|