Sanitize non utf8 strings before returning them to iOS apps
In swift, strings must be strictly utf8, and when golang returns a string with non utf8 characters, it gets translated to an empty string for utf8. To avoid this situation, we sanitize strings before returning them. This behavior is only enabled when building with the "ios" build tag.
This commit is contained in:
parent
6021e54d03
commit
a2fd1c6a3b
4 changed files with 21 additions and 2 deletions
|
|
@ -202,7 +202,7 @@ func (msg *PlainMessage) GetBinary() []byte {
|
|||
|
||||
// GetString returns the content of the message as a string.
|
||||
func (msg *PlainMessage) GetString() string {
|
||||
return strings.ReplaceAll(string(msg.Data), "\r\n", "\n")
|
||||
return sanitizeString(strings.ReplaceAll(string(msg.Data), "\r\n", "\n"))
|
||||
}
|
||||
|
||||
// GetBase64 returns the base-64 encoded binary content of the message as a
|
||||
|
|
|
|||
|
|
@ -49,7 +49,8 @@ func (keyRing *KeyRing) DecryptMIMEMessage(
|
|||
callbacks.OnVerified(constants.SIGNATURE_OK)
|
||||
}
|
||||
bodyContent, bodyMimeType := body.GetBody()
|
||||
callbacks.OnBody(bodyContent, bodyMimeType)
|
||||
bodyContentSanitized := sanitizeString(bodyContent)
|
||||
callbacks.OnBody(bodyContentSanitized, bodyMimeType)
|
||||
for i := 0; i < len(attachments); i++ {
|
||||
callbacks.OnAttachment(attachmentHeaders[i], []byte(attachments[i]))
|
||||
}
|
||||
|
|
|
|||
8
crypto/sanitize_string.go
Normal file
8
crypto/sanitize_string.go
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
//go:build !ios
|
||||
// +build !ios
|
||||
|
||||
package crypto
|
||||
|
||||
func sanitizeString(input string) string {
|
||||
return input
|
||||
}
|
||||
10
crypto/sanitize_string_ios.go
Normal file
10
crypto/sanitize_string_ios.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
//go:build ios
|
||||
// +build ios
|
||||
|
||||
package crypto
|
||||
|
||||
import "strings"
|
||||
|
||||
func sanitizeString(input string) string {
|
||||
return strings.ToValidUTF8(input, "\ufffd")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue