46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package xmpp
|
|
|
|
import (
|
|
"bytes"
|
|
)
|
|
|
|
const saslNS = "urn:ietf:params:xml:ns:xmpp-sasl"
|
|
|
|
// Redact returns a copy of xml with the character data of any <auth> element
|
|
// in the SASL namespace replaced by the literal string "redacted". It is used
|
|
// before logging or diagnostic publication.
|
|
func Redact(xml []byte) []byte {
|
|
var out []byte
|
|
i := 0
|
|
for i < len(xml) {
|
|
j := bytes.Index(xml[i:], []byte("<auth"))
|
|
if j < 0 {
|
|
break
|
|
}
|
|
j += i
|
|
endTag, ok := scanTagEnd(xml[j:])
|
|
if !ok {
|
|
break
|
|
}
|
|
startTag := xml[j : j+endTag]
|
|
isSASL := bytes.Contains(startTag, []byte(`xmlns="`+saslNS+`"`)) ||
|
|
bytes.Contains(startTag, []byte(`xmlns='`+saslNS+`'`))
|
|
if !isSASL {
|
|
out = append(out, xml[i:j+endTag]...)
|
|
i = j + endTag
|
|
continue
|
|
}
|
|
closeStart := bytes.Index(xml[j+endTag:], []byte("</auth>"))
|
|
if closeStart < 0 {
|
|
break
|
|
}
|
|
closeStart += j + endTag
|
|
out = append(out, xml[i:j+endTag]...)
|
|
out = append(out, []byte("redacted")...)
|
|
out = append(out, xml[closeStart:closeStart+len("</auth>")]...)
|
|
i = closeStart + len("</auth>")
|
|
}
|
|
out = append(out, xml[i:]...)
|
|
return out
|
|
}
|