fix(wing,server): client names are matched as whole Unicode words; unsafe lists are a startup error (review of #214)
- WrapNames bounds the bare name list with Unicode-aware edges instead of \b
(Go's \b is ASCII-only, so a name with a non-ASCII edge letter such as Åke,
Société, Ørsted would never have matched), and validates the list on its own:
it must compile, must not match the empty string, and cannot break out of its
group ('x)|.*(' compiles alone only if balanced)
- NewRouter refuses any pattern that matches text naming no client
- docs state the whole-word semantics and what is not matched
Mutation-checked. Open decision for Mathias: whole-word versus substring recall
for a confidentiality filter (plurals, CamelCase and digits glued on do not match).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -94,6 +94,9 @@ func NewRouter(brainDir, defaultWing, clientPattern string) (Router, error) {
|
||||
if err != nil {
|
||||
return Router{}, fmt.Errorf("client-content pattern: %w", err)
|
||||
}
|
||||
if re.MatchString("x") || re.MatchString("") {
|
||||
return Router{}, fmt.Errorf("client-content pattern %q matches text that names no client; it would park every note", clientPattern)
|
||||
}
|
||||
r.ClientBlock = re
|
||||
}
|
||||
return r, nil
|
||||
@@ -218,3 +221,28 @@ var latinLookalike = map[rune]rune{
|
||||
'\u0410': 'A', '\u0412': 'B', '\u0415': 'E', '\u041a': 'K', '\u041c': 'M', '\u041d': 'H', '\u041e': 'O', '\u0420': 'P', '\u0421': 'C', '\u0422': 'T', '\u0425': 'X',
|
||||
'\u03bf': 'o', '\u03bd': 'v', '\u03c1': 'p', '\u0391': 'A', '\u0392': 'B', '\u0395': 'E', '\u039f': 'O', '\u03a1': 'P',
|
||||
}
|
||||
|
||||
// WrapNames turns a bare list of client names (a regexp alternation such as
|
||||
// "acme|globex") into a pattern that matches a name only as a whole word, with
|
||||
// word edges decided by Unicode letters and digits: Go's \b is ASCII-only, so
|
||||
// with it a name that begins or ends in a letter like Å, é or Ø would never
|
||||
// match. An underscore or punctuation is a boundary (trucker_ecommerce matches
|
||||
// "trucker"), a letter or digit glued on is not (acmecorp, acme2).
|
||||
//
|
||||
// The list is validated first, on its own: it must compile as a regexp, must not
|
||||
// match the empty string (an empty alternative such as "a||b" would match
|
||||
// everywhere), and must be self-contained: a value like "x)|.*(" compiles alone
|
||||
// only if it is balanced, so it cannot break out of the group it is put in.
|
||||
func WrapNames(names string) (string, error) {
|
||||
if strings.TrimSpace(names) == "" {
|
||||
return "", fmt.Errorf("client names list is empty")
|
||||
}
|
||||
re, err := regexp.Compile("(?i)" + names)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("client names are not a valid regexp alternation: %w", err)
|
||||
}
|
||||
if re.MatchString("") {
|
||||
return "", fmt.Errorf("client names contain an empty alternative; it would match everywhere")
|
||||
}
|
||||
return `(?:^|[^\p{L}\p{N}])(?:` + names + `)(?:[^\p{L}\p{N}]|$)`, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user