github.com/greenpau/go-authcrunch@v1.1.4/pkg/acl/sanitize.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package acl 16 17 import ( 18 "strings" 19 ) 20 21 func sanitize(m map[string]interface{}) map[string]interface{} { 22 out := make(map[string]interface{}) 23 for k, v := range m { 24 switch val := v.(type) { 25 case string: 26 out[k] = sanitizeStr(k, val) 27 case map[string]interface{}: 28 out[k] = sanitize(val) 29 case []interface{}: 30 var entries []string 31 for _, entry := range val { 32 switch s := entry.(type) { 33 case string: 34 entries = append(entries, sanitizeStr(k, s)) 35 } 36 } 37 if len(entries) > 0 { 38 out[k] = entries 39 } else { 40 out[k] = v 41 } 42 default: 43 out[k] = v 44 } 45 } 46 return out 47 } 48 49 func sanitizeStr(k, s string) string { 50 switch k { 51 case "password", "secret", "old_password": 52 return "***masked***" 53 } 54 s = strings.ReplaceAll(s, "\n", "") 55 s = strings.ReplaceAll(s, "\r", "") 56 s = strings.ReplaceAll(s, "http://", "hxxp://") 57 s = strings.ReplaceAll(s, "https://", "hxxps://") 58 if len(s) > 255 { 59 s = string(s[:254]) 60 } 61 return s 62 }