package main import ( "os" "path/filepath" "regexp" "slices" "strings" "unicode" ) // The deterministic personal data classifier (ADR 0007). The spec's personal // flags come from a model, so for a factory-generated plugin a name is also // personal when its words, its sample value's shape, its column type or its // place in a visitor form say so, and the stricter result applies. The CMS // host runs the same name rules at runtime (cms backend/plugin/factory_guard.go // personalKind); keep the two aligned. // personalKind classifies a field or column name: email, phone, birth, // address, secret or name, or "" when the name says nothing personal. func personalKind(name string) string { tokens := nameTokens(name) has := func(t string) bool { return slices.Contains(tokens, t) } switch { case has("email") || has("e") && has("mail") || len(tokens) == 1 && tokens[0] == "mail": return "email" case has("phone") || has("mobile") || has("tel") || has("telephone") || has("cell") || has("fax") || has("whatsapp") || has("sms"): return "phone" case has("birth") || has("dob") || has("birthday") || has("birthdate"): return "birth" case has("address") && !has("email") || has("street") || has("postcode") || has("zip") || has("postal"): return "address" case has("ip") || has("ssn") || has("passport") || has("password") || has("medicare") || has("licence") || has("license"): return "secret" case has("name"): if len(tokens) == 1 || has("first") || has("last") || has("full") || has("given") || has("family") || has("middle") || has("sur") { return "name" } if slices.ContainsFunc([]string{"customer", "client", "contact", "person", "your", "guest", "visitor", "attendee", "member", "user", "student", "parent", "child", "patient", "owner", "sender", "recipient", "buyer", "requester", "applicant"}, has) { return "name" } case has("surname") || has("firstname") || has("lastname") || has("fullname"): return "name" } return "" } func nameTokens(name string) []string { var out []string var cur []rune flush := func() { if len(cur) > 0 { out = append(out, strings.ToLower(string(cur))) cur = cur[:0] } } runes := []rune(name) for i, r := range runes { switch { case r == '_' || r == '-' || r == ' ' || r == '.': flush() case unicode.IsUpper(r) && i > 0 && unicode.IsLower(runes[i-1]): flush() cur = append(cur, r) default: cur = append(cur, r) } } flush() return out } var ( classifyEmail = regexp.MustCompile(`(?i)^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$`) classifyPhone = regexp.MustCompile(`^\+?[0-9 ()./-]{7,22}$`) classifyDate = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}`) ) // sampleKind recognises an email address or phone number by its shape. func sampleKind(v string) string { v = strings.TrimSpace(v) if classifyEmail.MatchString(v) { return "email" } if !classifyPhone.MatchString(v) || classifyDate.MatchString(v) { return "" } digits := 0 for _, r := range v { if r >= '0' && r <= '9' { digits++ } } if digits < 8 || digits > 15 || strings.Contains(v, ".") && !strings.ContainsAny(v, " -()") { return "" } if digits == len(v) && !strings.HasPrefix(v, "0") { return "" } return "phone" } var ( createTablePattern = regexp.MustCompile(`(?is)create\s+table\s+(?:if\s+not\s+exists\s+)?([\w."]+)\s*\((.*?)\)\s*;`) personalColumnType = regexp.MustCompile(`(?i)^(inet|cidr)\b`) ) // migrationColumns reads CREATE TABLE statements in the plugin's migrations // and returns, per table, the columns that are personal by name or type. func migrationColumns(root string) map[string][]string { out := map[string][]string{} files, _ := filepath.Glob(filepath.Join(root, "migrations", "*.sql")) for _, path := range files { raw, err := os.ReadFile(path) if err != nil { continue } for _, m := range createTablePattern.FindAllStringSubmatch(string(raw), -1) { table := strings.Trim(m[1], `"`) if i := strings.LastIndex(table, "."); i >= 0 { table = strings.Trim(table[i+1:], `"`) } for _, def := range splitColumnDefs(m[2]) { fields := strings.Fields(def) if len(fields) < 2 { continue } col := strings.Trim(fields[0], `"`) switch strings.ToLower(col) { case "primary", "unique", "constraint", "foreign", "check", "exclude": continue } if personalKind(col) != "" || personalColumnType.MatchString(fields[1]) { out[table] = append(out[table], col) } } } } return out } func splitColumnDefs(body string) []string { var out []string depth, start := 0, 0 for i, r := range body { switch r { case '(': depth++ case ')': depth-- case ',': if depth == 0 { out = append(out, strings.TrimSpace(body[start:i])) start = i + 1 } } } return append(out, strings.TrimSpace(body[start:])) }