@@ -13,6 +13,7 @@ import (
1313 "fmt"
1414 "runtime/debug"
1515 "strings"
16+ "unicode/utf8"
1617
1718 "github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1819 "github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
@@ -277,25 +278,27 @@ func (db *VaultPersistence) UpdateStatusesValid(txIDs []driver.TxID, offset int)
277278 return query , params
278279}
279280
281+ const minUnicodeRuneValue = string (0 )
282+ const maxUnicodeRuneValue = string (utf8 .MaxRune )
280283const forbiddenRune = "\x00 "
281- const forbiddenRuneReplacement = "??"
284+
285+ var replacements = map [string ]string {
286+ minUnicodeRuneValue : "?0?" ,
287+ maxUnicodeRuneValue : "?1?" ,
288+ forbiddenRune : "?2?" ,
289+ }
282290
283291// TODO: Better sanitization of inputs
284292func escape (s string ) (string , error ) {
285- if strings .HasPrefix (s , forbiddenRune ) {
286- logger .Infof ("Escaping [%s]" , s )
287- s = forbiddenRuneReplacement + strings .TrimPrefix (s , forbiddenRune )
288- }
289- if strings .Contains (s , forbiddenRune ) {
290- return "" , errors .Errorf ("invalid character in [%s]" , s )
293+ for forbidden , replacement := range replacements {
294+ s = strings .ReplaceAll (s , forbidden , replacement )
291295 }
292296 return s , nil
293297}
294298
295299func unescape (s string ) string {
296- if strings .HasPrefix (s , forbiddenRuneReplacement ) {
297- logger .Infof ("Reverting escaped [%s]" , s )
298- return forbiddenRune + strings .TrimPrefix (s , forbiddenRuneReplacement )
300+ for forbidden , replacement := range replacements {
301+ s = strings .ReplaceAll (s , replacement , forbidden )
299302 }
300303 return s
301304}
0 commit comments