Files
magpie/plugins/redact/redact.go
T
yetone 319753d892 gateway: a key can follow, force on or force off masking (y0ngb1n on X)
A gateway key now carries Redact: "" follows the global setting, "on"
masks that key's requests even when masking is off, "off" sends them as
written. Trusted devices turn it off, external ones are forced on.

- access: Key.Redact, CleanRedact and the redact-key action; an issued key
  inherits "on" from its issuer and can't turn it off; the default key
  takes the same values.
- gateway: maskingFor(ctx) decides per request; every route that masked
  (chat, native, text hits, the Masked-As header) asks it. A local request
  with no key keeps the global setting. masking.WithForced carries "on"
  into the masker; the redact plugin's InUse counts keys set on.
- CLI: magpie gateway keys redact <key> follow|on|off.
- GUI: key-row badge and an app menu; 7 strings in zh, zh-TW, ja, de.
- docs/subsystems/gateway-routing.md: the Redact paragraph.

Note: "on" with global masking off masks secrets and personal data by the
defaults. An off key is still masked on a Remote magpie hop, which sends no
Masked-As header.

Run (mote-run): go test for access, masking, redact, cli, gui and
plugins/redact packages and the new gateway key_redact tests; gofmt -l and
go vet on darwin, windows and linux nogui, all clean; the new GUI test.
Broken by hand: maskingFor returning the global value goes red in
key_redact_test.go.
Not tried: a real remote-to-remote hop with two keys; the full suite runs
after the tag.
2026-11-12 01:01:32 +08:00

59 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package redact is Settings → Privacy's masking: secrets, personal data
// and the user's own words go to a vendor as placeholders, and come back
// as they were. While the settings ask for any of it, it stays on.
//
// It is the masker of the gateway's middleware point (P3 of
// docs/subsystems/plugin-runtime.md): Apply serves masking.Masker, and the point
// fails closed: a request the settings say to mask is not sent while no
// masker serves it. The logs' and archives' scrubbing (redact.Scrub) is
// the core's, whatever the settings say.
package redact
import (
"slices"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/gwmw"
"github.com/yetone/magpie/internal/masking"
"github.com/yetone/magpie/internal/pluginrt"
"github.com/yetone/magpie/internal/settings"
)
func init() {
pluginrt.Register(pluginrt.Manifest{
ID: "redact", API: "^1", Requires: []string{"settings", "gateway"}, Essential: true,
Name: "Privacy masking", Sub: "Secrets, personal data and your own words go to vendors as placeholders, and come back as they were",
One: "Masking is on", Many: "Masking is on",
InUse: InUse,
SetAt: "privacy #redactList",
// Insights › Masking: what masking did, request by request
Parts: []pluginrt.Part{{Slot: "nav.page", Key: "masking"}},
Client: []string{"i18n.js", "redact.css", "client.js", "page.js"},
Apply: func(c *pluginrt.Ctx) error {
c.OnDispose(gwmw.Mask(masking.Masker{}))
return nil
},
})
}
// InUse is 1 while the settings ask for anything to be masked; settings
// that can't be read are not known, so it stays on.
func InUse() (int, error) {
st, err := settings.LoadChecked()
if err != nil {
return 0, err
}
if masking.Wanted(st) {
return 1, nil
}
// a key held to masking (access.RedactOn) uses it with the setting off
keys, err := access.List()
if err != nil {
return 0, err
}
if slices.ContainsFunc(keys, func(k access.Key) bool { return k.Redact == access.RedactOn && !k.Off }) {
return 1, nil
}
return 0, nil
}