From a833663added703790afa4bfa8fbf20a7ab59c55 Mon Sep 17 00:00:00 2001 From: Maksim Kochkin Date: Sat, 6 Feb 2021 21:55:43 +0300 Subject: [PATCH] Fixed lexer in decoding base64-encoded []byte field with forward slash in it. (#328) Co-authored-by: Maksim Kochkin --- jlexer/lexer.go | 4 ++++ jlexer/lexer_test.go | 1 + 2 files changed, 5 insertions(+) diff --git a/jlexer/lexer.go b/jlexer/lexer.go index ef0aa61e..b5f5e261 100644 --- a/jlexer/lexer.go +++ b/jlexer/lexer.go @@ -712,6 +712,10 @@ func (r *Lexer) Bytes() []byte { r.errInvalidToken("string") return nil } + if err := r.unescapeStringToken(); err != nil { + r.errInvalidToken("string") + return nil + } ret := make([]byte, base64.StdEncoding.DecodedLen(len(r.token.byteValue))) n, err := base64.StdEncoding.Decode(ret, r.token.byteValue) if err != nil { diff --git a/jlexer/lexer_test.go b/jlexer/lexer_test.go index cc60179b..0cee611d 100644 --- a/jlexer/lexer_test.go +++ b/jlexer/lexer_test.go @@ -89,6 +89,7 @@ func TestBytes(t *testing.T) { }{ {toParse: `"c2ltcGxlIHN0cmluZw=="`, want: "simple string"}, {toParse: " \r\r\n\t " + `"dGVzdA=="`, want: "test"}, + {toParse: `"c3ViamVjdHM\/X2Q9MQ=="`, want: "subjects?_d=1"}, // base64 with forward slash escaped {toParse: `5`, wantError: true}, // not a JSON string {toParse: `"foobar"`, wantError: true}, // not base64 encoded