File: safejson.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (202 lines) | stat: -rw-r--r-- 5,198 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2018 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package safejson

import (
	"fmt"
	"strconv"
	"unicode"
	"unicode/utf16"
	"unicode/utf8"

	"github.com/snapcore/snapd/strutil"
)

// String accepts any valid JSON string. Its Clean method will remove
// characters that aren't expected in a short descriptive text.
// I.e.: Cc, Co, Cf, Cs, noncharacters, and � (U+FFFD, the replacement
// character) are removed.
type String struct {
	s string
}

func (str *String) UnmarshalJSON(in []byte) (err error) {
	str.s, err = unmarshal(in, uOpt{})
	return
}

// Clean returns the string, with Cc, Co, Cf, Cs, noncharacters,
// and � (U+FFFD) removed.
func (str String) Clean() string {
	return str.s
}

// Paragraph accepts any valid JSON string. Its Clean method will remove
// characters that aren't expected in a long descriptive text.
// I.e.: Cc (except for \n), Co, Cf, Cs, noncharacters, and � (U+FFFD,
// the replacement character) are removed.
type Paragraph struct {
	s string
}

func (par *Paragraph) UnmarshalJSON(in []byte) (err error) {
	par.s, err = unmarshal(in, uOpt{nlOK: true})
	return
}

// Clean returns the string, with Cc minus \n, Co, Cf, Cs, noncharacters,
// and � (U+FFFD) removed.
func (par Paragraph) Clean() string {
	return par.s
}

func unescapeUCS2(in []byte) (rune, bool) {
	if len(in) < 6 || in[0] != '\\' || in[1] != 'u' {
		return -1, false
	}
	u, err := strconv.ParseUint(string(in[2:6]), 16, 32)
	if err != nil {
		return -1, false
	}
	return rune(u), true
}

type uOpt struct {
	nlOK   bool
	simple bool
}

func unmarshal(in []byte, o uOpt) (string, error) {
	// heavily based on (inspired by?) unquoteBytes from encoding/json

	if len(in) < 2 || in[0] != '"' || in[len(in)-1] != '"' {
		// maybe it's a null and that's alright
		if len(in) == 4 && in[0] == 'n' && in[1] == 'u' && in[2] == 'l' && in[3] == 'l' {
			return "", nil
		}
		return "", fmt.Errorf("missing string delimiters: %q", in)
	}

	// prune the quotes
	in = in[1 : len(in)-1]
	i := 0
	// try the fast track
	for i < len(in) {
		// 0x00..0x19 is the first of Cc
		// 0x20..0x7e is all of printable ASCII (minus control chars)
		if in[i] < 0x20 || in[i] > 0x7e || in[i] == '\\' || in[i] == '"' {
			break
		}
		i++
	}
	if i == len(in) {
		// wee
		return string(in), nil
	}
	if o.simple {
		return "", fmt.Errorf("character %q in string %q unsupported for this value", in[i], in)
	}
	// in[i] is the first problematic one
	out := make([]byte, i, len(in)+2*utf8.UTFMax)
	copy(out, in)
	var r, r2 rune
	var n int
	var c byte
	var ubuf [utf8.UTFMax]byte
	var ok bool
	for i < len(in) {
		c = in[i]
		switch {
		case c == '"':
			return "", fmt.Errorf("unexpected unescaped quote at %d in \"%s\"", i, in)
		case c < 0x20:
			return "", fmt.Errorf("unexpected control character at %d in %q", i, in)
		case c == '\\':
			// handle escapes
			i++
			if i == len(in) {
				return "", fmt.Errorf("unexpected end of string (trailing backslash) in \"%s\"", in)
			}
			switch in[i] {
			case 'u':
				// oh dear, a unicode wotsit
				r, ok = unescapeUCS2(in[i-1:])
				if !ok {
					x := in[i-1:]
					if len(x) > 6 {
						x = x[:6]
					}
					return "", fmt.Errorf(`badly formed \u escape %q at %d of "%s"`, x, i, in)
				}
				i += 5
				if utf16.IsSurrogate(r) {
					// sigh
					r2, ok = unescapeUCS2(in[i:])
					if !ok {
						x := in[i:]
						if len(x) > 6 {
							x = x[:6]
						}
						return "", fmt.Errorf(`badly formed \u escape %q at %d of "%s"`, x, i, in)
					}
					i += 6
					r = utf16.DecodeRune(r, r2)
				}
				if r <= 0x9f {
					// otherwise, it's Cc (both halves, as we're looking at runes)
					if (o.nlOK && r == '\n') || (r >= 0x20 && r <= 0x7e) {
						out = append(out, byte(r))
					}
				} else if r != unicode.ReplacementChar && !unicode.Is(strutil.Ctrl, r) {
					n = utf8.EncodeRune(ubuf[:], r)
					out = append(out, ubuf[:n]...)
				}
			case 'b', 'f', 'r', 't':
				// do nothing
				i++
			case 'n':
				if o.nlOK {
					out = append(out, '\n')
				}
				i++
			case '"', '/', '\\':
				// the spec says just ", / and \ can be backslash-escaped
				// but go adds ' to the list (in unquoteBytes)
				out = append(out, in[i])
				i++
			default:
				return "", fmt.Errorf(`unknown escape '%c' at %d of "%s"`, in[i], i, in)
			}
		case c <= 0x7e:
			// printable ASCII, except " or \
			out = append(out, c)
			i++
		default:
			r, n = utf8.DecodeRune(in[i:])
			j := i + n
			if r > 0x9f && r != unicode.ReplacementChar && !unicode.Is(strutil.Ctrl, r) {
				out = append(out, in[i:j]...)
			}
			i = j
		}
	}

	return string(out), nil
}