|
| 1 | +package scrub |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "fmt" |
| 6 | + "os/user" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// pathAnonymizer holds the precomputed state for path anonymization. |
| 12 | +type pathAnonymizer struct { |
| 13 | + username string |
| 14 | + hashedUser string // "user_<8hex>" |
| 15 | + macPattern *regexp.Regexp |
| 16 | + linuxPattern *regexp.Regexp |
| 17 | + hyphenMac *regexp.Regexp |
| 18 | + hyphenLinux *regexp.Regexp |
| 19 | + bareUser *regexp.Regexp |
| 20 | +} |
| 21 | + |
| 22 | +var defaultAnonymizer *pathAnonymizer |
| 23 | + |
| 24 | +func init() { |
| 25 | + u, err := user.Current() |
| 26 | + if err != nil { |
| 27 | + return |
| 28 | + } |
| 29 | + defaultAnonymizer = newAnonymizer(u.Username) |
| 30 | +} |
| 31 | + |
| 32 | +func newAnonymizer(username string) *pathAnonymizer { |
| 33 | + if username == "" { |
| 34 | + return nil |
| 35 | + } |
| 36 | + h := sha256.Sum256([]byte(username)) |
| 37 | + hashed := fmt.Sprintf("user_%x", h[:4]) |
| 38 | + |
| 39 | + escaped := regexp.QuoteMeta(username) |
| 40 | + return &pathAnonymizer{ |
| 41 | + username: username, |
| 42 | + hashedUser: hashed, |
| 43 | + macPattern: regexp.MustCompile(`/Users/` + escaped + `(?:/|$)`), |
| 44 | + linuxPattern: regexp.MustCompile(`/home/` + escaped + `(?:/|$)`), |
| 45 | + hyphenMac: regexp.MustCompile(`-Users-` + escaped + `-`), |
| 46 | + hyphenLinux: regexp.MustCompile(`-home-` + escaped + `-`), |
| 47 | + bareUser: regexp.MustCompile(`(?:^|[/\-])` + escaped + `(?:$|[/\-])`), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// projectRelativeDirs are directories under the home folder where we strip |
| 52 | +// the full path and keep only the project-relative portion. |
| 53 | +var projectRelativeDirs = []string{"Documents", "Downloads", "Desktop", "Projects", "projects", "src", "repos", "code", "dev", "workspace"} |
| 54 | + |
| 55 | +// AnonymizePath replaces the username in a single file path with the hashed form. |
| 56 | +func AnonymizePath(path string) string { |
| 57 | + if defaultAnonymizer == nil { |
| 58 | + return path |
| 59 | + } |
| 60 | + return defaultAnonymizer.anonymizePath(path) |
| 61 | +} |
| 62 | + |
| 63 | +func (a *pathAnonymizer) anonymizePath(path string) string { |
| 64 | + // /Users/<username>/... → /Users/<hashed>/... |
| 65 | + path = a.macPattern.ReplaceAllStringFunc(path, func(m string) string { |
| 66 | + return strings.Replace(m, a.username, a.hashedUser, 1) |
| 67 | + }) |
| 68 | + // /home/<username>/... → /home/<hashed>/... |
| 69 | + path = a.linuxPattern.ReplaceAllStringFunc(path, func(m string) string { |
| 70 | + return strings.Replace(m, a.username, a.hashedUser, 1) |
| 71 | + }) |
| 72 | + // Hyphen-encoded: -Users-<username>- → -Users-<hashed>- |
| 73 | + path = a.hyphenMac.ReplaceAllStringFunc(path, func(m string) string { |
| 74 | + return strings.Replace(m, a.username, a.hashedUser, 1) |
| 75 | + }) |
| 76 | + path = a.hyphenLinux.ReplaceAllStringFunc(path, func(m string) string { |
| 77 | + return strings.Replace(m, a.username, a.hashedUser, 1) |
| 78 | + }) |
| 79 | + return path |
| 80 | +} |
| 81 | + |
| 82 | +// AnonymizeText replaces usernames in paths throughout a block of text. |
| 83 | +func AnonymizeText(text string) string { |
| 84 | + if defaultAnonymizer == nil { |
| 85 | + return text |
| 86 | + } |
| 87 | + return defaultAnonymizer.anonymizeText(text) |
| 88 | +} |
| 89 | + |
| 90 | +func (a *pathAnonymizer) anonymizeText(text string) string { |
| 91 | + // Replace full paths first (most specific). |
| 92 | + text = a.macPattern.ReplaceAllStringFunc(text, func(m string) string { |
| 93 | + return strings.Replace(m, a.username, a.hashedUser, 1) |
| 94 | + }) |
| 95 | + text = a.linuxPattern.ReplaceAllStringFunc(text, func(m string) string { |
| 96 | + return strings.Replace(m, a.username, a.hashedUser, 1) |
| 97 | + }) |
| 98 | + // Hyphen-encoded paths. |
| 99 | + text = a.hyphenMac.ReplaceAllStringFunc(text, func(m string) string { |
| 100 | + return strings.Replace(m, a.username, a.hashedUser, 1) |
| 101 | + }) |
| 102 | + text = a.hyphenLinux.ReplaceAllStringFunc(text, func(m string) string { |
| 103 | + return strings.Replace(m, a.username, a.hashedUser, 1) |
| 104 | + }) |
| 105 | + return text |
| 106 | +} |
| 107 | + |
| 108 | +// NewAnonymizerForUser creates an anonymizer for a specific username (for testing). |
| 109 | +func NewAnonymizerForUser(username string) *pathAnonymizer { |
| 110 | + return newAnonymizer(username) |
| 111 | +} |
0 commit comments