Settings › Privacy gets two sections under What to mask:
- When a value is found: Mask, Warn or Block per kind (redactActions;
a kind left out is masked). Warn sends the value as written and records
it with a placeholder nobody remembers, not counted as masked. Block
masks it as before and the gateway answers 400 naming the kind and the
value's hint, never the value, with nothing sent (maskBlocked); the
record is kept as Blocked. The secret kinds come first, the personal
ones behind Show all kinds. A Never mask list (redactAllow) lets a whole
value go as written in any case; a longer value it is part of is masked.
- Try it: POST /api/masking/preview masks a text as a request would
(redact.Preview) and remembers nothing: no placeholder stands for it
afterwards and its values aren't masked later as seen before. The page
shows the vendor's text, what was found and what would be done, and
keeps the text in no storage.
The masking card counts requests held back and values sent as written; a
call whose values only went as written gets an amber badge on Recent calls
and the Routing log. zh, zh-TW, ja and de throughout. Docs: gateway-routing
and gui-shell.
Broken by hand: the new GUI badge assertion fails on the old maskedBadge
(no .warn badge drawn); TestSettingsSaveKeepsWhatItDoesNotSend went red
until the two settings were in its fixture; "{n} held back" turned
i18n-keys red against the routing plugin's "{n} held" and was renamed.
Run at this tree (base origin/main 9b0f48612):
- gofmt clean; go vet on macOS, GOOS=linux -tags nogui, GOOS=windows.
- go test -count=1 -timeout 45m redact, maskrec, settings, gwmw, gui,
gateway: ok.
- GUI, both engines: every Privacy file, mask-actions, masking-records,
routing-bodies, i18n-keys, old-webkit: 263/263.
- Whole GUI suite earlier at 083b1c8e7's base, beside 7 other runs:
53 red; alone, the ones left red fail the same on origin/main
6fc532198 and are filed (#3179, #2228, #2448, #2276, #2549, #2163,
#2399), except privacy-usage-stats and account-discovery, which this
change's taller page moved off screen; both fixed here.
Not tried: a real vendor request held back by Block from a real agent.
133 lines
4.0 KiB
Go
133 lines
4.0 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/gwmw"
|
|
"github.com/yetone/magpie/internal/masking"
|
|
"github.com/yetone/magpie/internal/maskrec"
|
|
)
|
|
|
|
// previewMax is how much text the preview masks.
|
|
const previewMax = 64 << 10
|
|
|
|
// maskedRecord is a record as the page shows it: each value says whether
|
|
// the masker still knows what it stood for, so the page offers to show it
|
|
// only where it can.
|
|
type maskedRecord struct {
|
|
maskrec.Record
|
|
Values []maskedValue `json:"values,omitempty"`
|
|
}
|
|
|
|
type maskedValue struct {
|
|
maskrec.Value
|
|
Known bool `json:"known,omitempty"`
|
|
}
|
|
|
|
// teller is the served masker, where it can say what a placeholder stood
|
|
// for.
|
|
func teller() (gwmw.Teller, bool) {
|
|
m, ok := gwmw.Served()
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
t, ok := m.(gwmw.Teller)
|
|
return t, ok
|
|
}
|
|
|
|
func shownRecord(r maskrec.Record, t gwmw.Teller) maskedRecord {
|
|
out := maskedRecord{Record: r}
|
|
for _, v := range r.Values {
|
|
mv := maskedValue{Value: v}
|
|
if t != nil {
|
|
_, mv.Known = t.Value(v.Placeholder)
|
|
}
|
|
out.Values = append(out.Values, mv)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// maskingRoutes are Settings → Privacy's record of what masking swapped:
|
|
// - GET /api/masking/summary?days= is maskrec.Sum;
|
|
// - GET /api/masking/records?before=&limit=&kind=&by=&agent=&provider=
|
|
// a page of records, newest first;
|
|
// - GET /api/masking/record?key= one record;
|
|
// - GET /api/masking/value?p= what a placeholder stood for, while this
|
|
// magpie's masker remembers it (404 after a restart: it is never on
|
|
// disk);
|
|
// - POST /api/masking/preview {text} the text as a vendor would get it
|
|
// under the settings now, and what was found, each told by its kind
|
|
// and hint, never its value; nothing of it is remembered (#1977).
|
|
func maskingRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("POST /api/masking/preview", func(rw http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
Text string `json:"text"`
|
|
}
|
|
if err := json.NewDecoder(io.LimitReader(r.Body, 4*previewMax)).Decode(&in); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
if len(in.Text) > previewMax {
|
|
in.Text = strings.ToValidUTF8(in.Text[:previewMax], "")
|
|
}
|
|
text, hits, off := masking.Preview(in.Text)
|
|
type hit struct {
|
|
Kind string `json:"kind"`
|
|
By string `json:"by"`
|
|
Action string `json:"action,omitempty"`
|
|
Placeholder string `json:"placeholder"`
|
|
Head string `json:"head,omitempty"`
|
|
Len int `json:"len"`
|
|
}
|
|
out := []hit{}
|
|
for _, h := range hits {
|
|
out = append(out, hit{h.Kind, h.By, h.Action, h.Placeholder, h.Head, h.Len})
|
|
}
|
|
writeJSON(rw, map[string]any{"text": text, "hits": out, "off": off})
|
|
})
|
|
mux.HandleFunc("GET /api/masking/summary", func(rw http.ResponseWriter, r *http.Request) {
|
|
days, _ := strconv.Atoi(r.URL.Query().Get("days"))
|
|
writeJSON(rw, maskrec.Sum(min(max(days, 1), 30)))
|
|
})
|
|
mux.HandleFunc("GET /api/masking/records", func(rw http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
before, _ := strconv.ParseInt(q.Get("before"), 10, 64)
|
|
limit, _ := strconv.Atoi(q.Get("limit"))
|
|
rs, more := maskrec.List(before, min(max(limit, 1), 200), maskrec.Filter{Kind: q.Get("kind"), By: q.Get("by"), Agent: q.Get("agent"), Provider: q.Get("provider")})
|
|
t, _ := teller()
|
|
out := []maskedRecord{}
|
|
for _, rec := range rs {
|
|
out = append(out, shownRecord(rec, t))
|
|
}
|
|
writeJSON(rw, map[string]any{"records": out, "more": more})
|
|
})
|
|
mux.HandleFunc("GET /api/masking/record", func(rw http.ResponseWriter, r *http.Request) {
|
|
key, _ := strconv.ParseInt(r.URL.Query().Get("key"), 10, 64)
|
|
rec, ok := maskrec.Get(key)
|
|
if !ok {
|
|
http.NotFound(rw, r)
|
|
return
|
|
}
|
|
t, _ := teller()
|
|
writeJSON(rw, shownRecord(rec, t))
|
|
})
|
|
mux.HandleFunc("GET /api/masking/value", func(rw http.ResponseWriter, r *http.Request) {
|
|
rw.Header().Set("Cache-Control", "no-store")
|
|
t, ok := teller()
|
|
if !ok {
|
|
http.NotFound(rw, r)
|
|
return
|
|
}
|
|
v, ok := t.Value(r.URL.Query().Get("p"))
|
|
if !ok {
|
|
http.NotFound(rw, r)
|
|
return
|
|
}
|
|
writeJSON(rw, map[string]string{"value": v})
|
|
})
|
|
}
|