Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cozitive attrs escape #443

Merged
merged 7 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/document/crdt/rht.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (rht *RHT) Marshal() string {
sb.WriteString(",")
}
value := members[k]
sb.WriteString(fmt.Sprintf(`"%s":"%s"`, k, value))
sb.WriteString(fmt.Sprintf(`"%s":"%s"`, EscapeString(k), EscapeString(value)))
}
sb.WriteString("}")

Expand Down
2 changes: 1 addition & 1 deletion pkg/document/crdt/rht_pq_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (rht *RHTPriorityQueueMap) Marshal() string {
sb.WriteString(",")
}
value := members[k]
sb.WriteString(fmt.Sprintf(`"%s":%s`, k, value.Marshal()))
sb.WriteString(fmt.Sprintf(`"%s":%s`, EscapeString(k), value.Marshal()))
}
sb.WriteString("}")

Expand Down
23 changes: 23 additions & 0 deletions pkg/document/crdt/rht_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package crdt

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMarshal(t *testing.T) {
t.Run("marshal test", func(t *testing.T) {
key1 := `hello\\\t`
value1 := "world\"\f\b"
key2 := "hi"
value2 := `test\r`
expected := `{"hello\\\\\\t":"world\\"\\f\\b","hi":"test\\r"}`

rht := NewRHT()
rht.Set(key1, value1, nil)
rht.Set(key2, value2, nil)
actual := rht.Marshal()
assert.Equal(t, expected, actual)
})
}
19 changes: 16 additions & 3 deletions pkg/document/crdt/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,39 @@ func EscapeString(s string) string {
continue
}
switch c {
case '\\', '"':
case '\\':
buf.WriteByte('\\')
buf.WriteByte(c)
buf.WriteByte('\\')
case '"':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('"')
case '\n':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('n')
case '\f':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('f')
case '\b':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('b')
case '\r':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('r')
case '\t':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('t')
default:
buf.Write([]byte{'\\', 'u', '0', '0'})
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('u')
buf.WriteByte('0')
buf.WriteByte('0')
buf.WriteByte(hex[c>>4])
buf.WriteByte(hex[c&0xF])
}
Expand Down
33 changes: 30 additions & 3 deletions pkg/document/crdt/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,38 @@ import (
)

func TestEscapeString(t *testing.T) {
t.Run("escape string", func(t *testing.T) {
str := `"hello world"`
expected := `\"hello world\"`
t.Run("escape normal string", func(t *testing.T) {
str := `hello world`
expected := `hello world`
actual := EscapeString(str)
assert.Equal(t, expected, actual)
})

t.Run("escape string with doublequote", func(t *testing.T) {
str := `hello world"`
expected := `hello world\\"`
actual := EscapeString(str)
assert.Equal(t, expected, actual)
})

t.Run("escape string with raw control character", func(t *testing.T) {
str := "hello world\n"
expected := `hello world\\n`
actual := EscapeString(str)
assert.Equal(t, expected, actual)
})

t.Run("escape string with escaped control character", func(t *testing.T) {
str := `hello world\n`
expected := `hello world\\n`
actual := EscapeString(str)
assert.Equal(t, expected, actual)
})

t.Run("escape string with raw unicode character", func(t *testing.T) {
str := "hello world\u1234"
expected := "hello world\u1234"
actual := EscapeString(str)
assert.Equal(t, expected, actual)
})
}
4 changes: 2 additions & 2 deletions pkg/document/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func TestDocument(t *testing.T) {
assert.Equal(
t,
`[0:0:00:0 {} ""][1:2:00:0 {"b":"1"} "Hel"][1:2:00:3 {"b":"1","i":"1"} "lo"]`+
`[5:1:00:0 {"list":"true"} "\n"][4:1:00:0 {} " Yorkie"]{1:2:00:5 {} " world"}`,
`[5:1:00:0 {"list":"true"} "\\n"][4:1:00:0 {} " Yorkie"]{1:2:00:5 {} " world"}`,
text.StructureAsString(),
)
return nil
Expand All @@ -345,7 +345,7 @@ func TestDocument(t *testing.T) {
assert.Equal(
t,
`{"k1":[{"attrs":{"b":"1"},"val":"Hel"},{"attrs":{"b":"1","i":"1"},"val":"lo"},`+
`{"attrs":{"list":"true"},"val":"\n"},{"attrs":{},"val":" Yorkie"}]}`,
`{"attrs":{"list":"true"},"val":"\\n"},{"attrs":{},"val":" Yorkie"}]}`,
doc.Marshal(),
)
})
Expand Down