Skip to content
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Docs: use placeholder accounts in Gmail auto-reply, Drive audit, and live-testing examples. (#1332) — thanks @vyctorbrzezowski.

- Safety profiles: add `denied-values` to refuse specific values of string and list flags on named commands, and use it in `agent-safe` to refuse the `TRASH` and `SPAM` labels on Gmail modify commands and `--send-updates all|externalOnly` on calendar create, update and move, auto-declining `calendar focus-time` (pass `--auto-decline none`), and focus-time or out-of-office events written through `calendar create` and `calendar update`; a `"*"` entry refuses any value of a flag; `agent-safe` also blocks `calendar propose-time` and Drive and Docs comments, which notify other people. (#1263) — thanks @bautrey.

## 0.48.0 - 2026-11-05

**Highlights:** New backups stay local until you configure a remote; existing configurations keep working. Store service-account keys in the keyring, search Gmail IDs efficiently, and use environment-backed watch tokens, with Calendar fixes and security dependency updates.
Expand Down
76 changes: 76 additions & 0 deletions cmd/bake-safety-profile/denied_values_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"bytes"
"fmt"
"go/parser"
"go/token"
"testing"

"github.com/openclaw/gogcli/internal/safetyprofile"
)

func TestGenerateDeniedValuesEmitsHashedLookup(t *testing.T) {
profile := &safetyprofile.Profile{
Name: "test",
AllowRules: []string{"gmail.thread.modify"},
DeniedValues: []safetyprofile.DeniedValue{
{Path: "calendar.create", Flag: "send-updates", Values: []string{"all", "externalonly"}},
{Path: "gmail.thread.modify", Flag: "add", Values: []string{"spam", "trash"}},
{Path: "drive.upload", Flag: "name", Values: []string{"private,report.txt"}},
{Path: "calendar.create", Flag: "focus-chat-status", Values: []string{"*"}},
},
}

out := generate(profile)

if _, err := parser.ParseFile(token.NewFileSet(), "gen.go", out, parser.AllErrors); err != nil {
t.Fatalf("generated code does not parse as Go:\n%s\n\nerror: %v", out, err)
}

// Path and flag are hashed together like a command rule, and each refused value is
// hashed with them, so no part of a rule is a string an in-place edit could change.
for _, rule := range profile.DeniedValues {
anyValue := len(rule.Values) == 1 && rule.Values[0] == "*"
want := fmt.Sprintf("\tcase 0x%016x:\n\t\treturn true, %t\n", safetyprofile.HashRule(rule.Path+"."+rule.Flag), anyValue)
if !bytes.Contains(out, []byte(want)) {
t.Fatalf("generated output missing rule case for %s %s:\n%s\n\nfull output:\n%s", rule.Path, rule.Flag, want, out)
}
for _, verbatim := range append([]string{rule.Path, rule.Flag}, rule.Values...) {
if bytes.Contains(out, []byte(`"`+verbatim+`"`)) {
t.Fatalf("denied value rule component %q appears verbatim; it should only be hashed\n\nfull output:\n%s", verbatim, out)
}
}
if anyValue {
continue
}
for _, value := range rule.Values {
h := fmt.Sprintf("0x%016x", safetyprofile.HashRule(safetyprofile.DeniedValueKey(rule.Path, rule.Flag, value)))
if !bytes.Contains(out, []byte(h)) {
t.Fatalf("generated output missing value hash %s for %s %s %q:\n%s", h, rule.Path, rule.Flag, value, out)
}
}
}
if !bytes.Contains(out, []byte("func bakedSafetyDeniedValueRuleCount() int { return 4 }\n")) {
t.Fatalf("generated output missing the rule count:\n%s", out)
}
}

// A profile without denied values still needs both functions, so safety_profile
// builds compile whether or not the profile uses the feature.
func TestGenerateWithoutDeniedValuesStillDefinesLookup(t *testing.T) {
out := generate(&safetyprofile.Profile{Name: "test", AllowRules: []string{"gmail.get"}})

if _, err := parser.ParseFile(token.NewFileSet(), "gen.go", out, parser.AllErrors); err != nil {
t.Fatalf("generated code does not parse as Go:\n%s\n\nerror: %v", out, err)
}
for _, want := range []string{
"func bakedSafetyDeniedValueRule(path []string, flag string) (bool, bool) {\n\treturn false, false\n}\n",
"func bakedSafetyDeniedValueMatch(path []string, flag, value string) bool {\n\treturn false\n}\n",
"func bakedSafetyDeniedValueRuleCount() int { return 0 }\n",
} {
if !bytes.Contains(out, []byte(want)) {
t.Fatalf("generated output missing %q:\n%s", want, out)
}
}
}
58 changes: 58 additions & 0 deletions cmd/bake-safety-profile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,68 @@ func generate(profile *safetyprofile.Profile) []byte {
writeMatcher(&out, "bakedSafetyAllowExactMatch", profile.AllowRules, profile.AllowAll && len(profile.DenyRules) == 0, true)
out.WriteString("\n")
writeLockedFlags(&out, profile.LockedFlags)
out.WriteString("\n")
writeDeniedValues(&out, profile.DeniedValues)

return out.Bytes()
}

// writeDeniedValues emits the denied value lookups. The command path and flag name are
// hashed together like a command rule, and the rule case says whether it refuses any
// value. Each refused value is hashed with its path and flag (DeniedValueKey), and the
// runtime hashes the value it was given the same way, so no denied value is a string an
// in-place edit of the binary could change. The count travels alongside so the runtime
// can tell that every rule matched a real flag without the names being written down.
func writeDeniedValues(out *bytes.Buffer, rules []safetyprofile.DeniedValue) {
out.WriteString("func bakedSafetyDeniedValueRule(path []string, flag string) (bool, bool) {\n")
if len(rules) == 0 {
out.WriteString("\treturn false, false\n}\n")
out.WriteString("\nfunc bakedSafetyDeniedValueMatch(path []string, flag, value string) bool {\n\treturn false\n}\n")
out.WriteString("\nfunc bakedSafetyDeniedValueRuleCount() int { return 0 }\n")
return
}
out.WriteString("\tswitch bakedSafetyHashPath(append(append([]string(nil), path...), flag)) {\n")
seen := make(map[uint64]string, len(rules))
var valueCases []string
for _, rule := range rules {
key := rule.Path + "." + rule.Flag
h := safetyprofile.HashRule(key)
if existing, dup := seen[h]; dup {
fmt.Fprintf(os.Stderr, "bake-safety-profile: hash collision between denied value rules %q and %q (FNV-64a=%#x)\n", existing, key, h)
os.Exit(1)
}
seen[h] = key
anyValue := len(rule.Values) == 1 && rule.Values[0] == "*"
fmt.Fprintf(out, "\tcase 0x%016x:\n\t\treturn true, %t\n", h, anyValue)
if anyValue {
continue
}
for _, value := range rule.Values {
valueKey := safetyprofile.DeniedValueKey(rule.Path, rule.Flag, value)
vh := safetyprofile.HashRule(valueKey)
if existing, dup := seen[vh]; dup {
fmt.Fprintf(os.Stderr, "bake-safety-profile: hash collision between denied value rules %q and %q (FNV-64a=%#x)\n", existing, valueKey, vh)
os.Exit(1)
}
seen[vh] = valueKey
valueCases = append(valueCases, fmt.Sprintf("0x%016x", vh))
}
}
out.WriteString("\t}\n\treturn false, false\n}\n")

out.WriteString("\nfunc bakedSafetyDeniedValueMatch(path []string, flag, value string) bool {\n")
if len(valueCases) == 0 {
out.WriteString("\treturn false\n}\n")
} else {
out.WriteString("\tswitch bakedSafetyDeniedValueHash(path, flag, value) {\n")
for _, c := range valueCases {
fmt.Fprintf(out, "\tcase %s:\n\t\treturn true\n", c)
}
out.WriteString("\t}\n\treturn false\n}\n")
}
fmt.Fprintf(out, "\nfunc bakedSafetyDeniedValueRuleCount() int { return %d }\n", len(rules))
}

// writeLockedFlags emits the locked flag lookup. Names are hashed like the command
// rules; the locked boolean values are literals because the flag parser consumes them. The
// count travels alongside so the runtime can tell that every lock matched a real
Expand Down
81 changes: 78 additions & 3 deletions docs/safety-profiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ FNV-64a hash of each dotted command path, not as raw YAML. The compiled rule
table never contains the rule strings themselves, so to re-enable a blocked
command an attacker has to patch compiled machine code rather than flip ASCII
bytes in a YAML blob; the cost goes from a one-line `sed` invocation to
disassembly-level work.
disassembly-level work. Denied values are compiled the same way: each refused
value is hashed together with its command path and flag, and the runtime hashes
the value it was given before comparing, so no denied value is stored as a
string either.

Note that command names may still appear in the binary from unrelated metadata
(API URLs, error message format strings, Kong help text). What this hardening
Expand All @@ -108,8 +111,15 @@ error messages can reference it.

Allows reading, searching, drafting, labeling, archiving, organizing files, and
other low-risk recoverable actions. Blocks sends, deletes, sharing changes, admin
operations, and auth writes. Keeps `schema` available for command, exit-code,
and effective safety-policy discovery.
operations, and auth writes. Refuses the `TRASH` and `SPAM` labels on the Gmail
modify commands, `--send-updates all|externalOnly` on calendar create, update
and move, any `calendar focus-time` that auto-declines (it defaults to
`--auto-decline all`, so pass `--auto-decline none`), and focus-time or
out-of-office events written through `calendar create` and `calendar update`
(`--event-type`, the focus and out-of-office flags, and on update any use of
them), and blocks `calendar propose-time` and Drive and Docs comments, because
those notify other people. See [Denied Flag Values](#denied-flag-values). Keeps
`schema` available for command, exit-code, and effective safety-policy discovery.

Good for:

Expand Down Expand Up @@ -163,6 +173,7 @@ Rules:
- command names are written as dot paths internally, such as `gmail.drafts.create`.
- `aliases:` controls root shortcuts such as `send`, `ls`, `search`, and `upload`.
- `locked-flags:` is not a command path; it locks flag values, see [Locked Flags](#locked-flags).
- `denied-values:` is not a command path; it refuses values of string flags on named commands, see [Denied Flag Values](#denied-flag-values).

Parent rules are prefix matches. For example, `drive: true` allows every `drive`
subcommand unless a child is explicitly blocked. For restrictive profiles, prefer
Expand Down Expand Up @@ -248,6 +259,70 @@ To make an existing preset stricter without changing its behavior for everyone,
copy it and add the locks you need. For example, an agent-oriented copy commonly
locks `sanitize-content`, `wrap-untrusted`, and `no-input` to `true`.

## Denied Flag Values

Command rules decide which commands may run, and locked flags fix boolean flags.
Neither stops an allowed command from being pointed at something destructive or
noisy. `gmail thread modify <id> --add TRASH` moves mail to the trash through a
command the profile allows, and `calendar create ... --send-updates all` emails
every attendee.

`denied-values` refuses specific values of string and list flags on specific
commands:

```yaml
denied-values:
gmail.thread.modify:
add: [TRASH, SPAM]
add-label: [TRASH, SPAM]
calendar.create:
send-updates: [all, externalonly]
```

Each key is a canonical dotted command path, matched exactly, and maps flag names
to a non-empty list of values. A command reached through an alias, such as
`gmail thread edit`, resolves to its canonical path first and is covered. Paths
are not prefix matches and wildcards (`*`, `all`) are rejected, so a rule applies
only to the command it names.

A denied value behaves as follows:

- the check runs after parsing and before the command, so `--dry-run` is refused too.
- comparison is case-insensitive. Each flag value, and each element of a repeated
flag, is compared whole and then split on commas and trimmed, so `trash`,
` Trash ` and `INBOX,TRASH` all match `TRASH`, and a denied value that itself
contains a comma still matches. Splitting more aggressively than a command does can only
deny more, never less.
- the refusal is a usage error (exit 2) that echoes the value as typed:

```text
flag --add value "TRASH" is denied for "gmail thread modify" by baked safety profile "agent-safe"
```

- Gmail label IDs are case-sensitive, but a denied value matches any casing, so a
user label spelled `Trash` is refused as well. That fails closed.
- a list holding only `"*"` refuses every value of the flag, as typed with no comma
splitting. An empty value counts when it is typed on the command line, because a
command can treat a supplied but empty flag as supplied: `calendar update` infers
an event type from one, and an empty auto-decline then defaults to `all`. A flag
left off the command line is allowed only when its default is empty: with
`calendar.focus-time: {auto-decline: ["*"]}`, omitting `--auto-decline` still
yields the default `all` and is refused. `"*"` must be the only entry in its list.

```yaml
denied-values:
calendar.update:
focus-chat-status: ["*"]
```

Rules the binary cannot enforce are refused when it starts. Every path and flag
must exist on that command itself, and the flag must hold a string or a list of
strings. A misspelled name would otherwise deny nothing while the profile claimed
otherwise. As with locked flags, the names are hashed in the binary, so the check
counts matches against the number of rules.

Stock builds have no profile, so `denied-values` does nothing there.

## Choosing A Profile

Use `readonly` when the caller should never change Google or local `gog` state.
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func executeWithRuntime(args []string, runtime *app.Runtime) (err error) {
if err != nil {
return reportEarlyError(kctx, runtimeIO.Err, err)
}
if err = verifyLockedFlagsExist(parser.Model.Node); err != nil {
if err = verifyBakedFlagPolicyExists(parser.Model.Node); err != nil {
return reportEarlyError(kctx, runtimeIO.Err, err)
}
args = rewriteDocsCellUpdateContentArgs(parser.Model, args)
Expand Down Expand Up @@ -210,7 +210,7 @@ func executeWithRuntime(args []string, runtime *app.Runtime) (err error) {
if err = enforceBakedSafetyProfile(kctx); err != nil {
return reportEarlyError(kctx, runtimeIO.Err, err)
}
err = enforceLockedFlags(kctx)
err = enforceBakedFlagPolicy(kctx)
jsonErrors = cli.JSON
if err != nil {
return reportEarlyError(kctx, runtimeIO.Err, err)
Expand Down
Loading
Loading