File: validate.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 (325 lines) | stat: -rw-r--r-- 8,839 bytes parent folder | download | duplicates (2)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2021 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 validate

import (
	"encoding/xml"
	"fmt"
	"io"
	"path/filepath"
	"regexp"
	"strings"
)

type Element struct {
	CharData        string     `xml:",chardata"`
	UnknownAttrs    []xml.Attr `xml:",any,attr"`
	UnknownChildren []xml.Name `xml:",any"`
}

type policyConfig struct {
	XMLName xml.Name `xml:"policyconfig"`
	Element

	Vendor    []Element `xml:"vendor"`
	VendorURL []Element `xml:"vendor_url"`
	IconName  []Element `xml:"icon_name"`

	Actions []action `xml:"action"`
}

type action struct {
	Element

	ID string `xml:"id,attr"`

	Vendor    []Element `xml:"vendor"`
	VendorURL []Element `xml:"vendor_url"`
	IconName  []Element `xml:"icon_name"`

	Description []message  `xml:"description"`
	Message     []message  `xml:"message"`
	Defaults    []defaults `xml:"defaults"`
	Annotate    []annotate `xml:"annotate"`
}

type message struct {
	Element

	GettextDomain string `xml:"gettext-domain,attr"`
	Language      string `xml:"lang,attr"` // to match xml:lang
}

type defaults struct {
	Element

	AllowAny      []Element `xml:"allow_any"`
	AllowInactive []Element `xml:"allow_inactive"`
	AllowActive   []Element `xml:"allow_active"`
}

type annotate struct {
	Element

	Key string `xml:"key,attr"`
}

func ValidatePolicy(r io.Reader) (actionIDs []string, err error) {
	decoder := xml.NewDecoder(r)
	var config policyConfig
	if err := decoder.Decode(&config); err != nil {
		return nil, err
	}
	// check for additional data after the root element
	if err := decoder.Decode(new(any)); err != io.EOF {
		return nil, fmt.Errorf("invalid XML: additional data after root element")
	}

	return validateConfig(config)
}

func validateConfig(config policyConfig) ([]string, error) {
	if config.XMLName != (xml.Name{Local: "policyconfig"}) {
		return nil, fmt.Errorf("root element must be <policyconfig>")
	}

	if err := validateElement(config.Element, "<policyconfig>", 0); err != nil {
		return nil, err
	}

	if err := validateOptionalProperty(config.Vendor, "<vendor>", "<policyconfig>"); err != nil {
		return nil, err
	}
	if err := validateOptionalProperty(config.VendorURL, "<vendor_url>", "<policyconfig>"); err != nil {
		return nil, err
	}
	if err := validateOptionalProperty(config.IconName, "<icon_name>", "<policyconfig>"); err != nil {
		return nil, err
	}

	seenIDs := make(map[string]struct{})
	for _, a := range config.Actions {
		if err := validateAction(a, seenIDs); err != nil {
			return nil, err
		}
	}

	actionIDs := make([]string, 0, len(seenIDs))
	for id := range seenIDs {
		actionIDs = append(actionIDs, id)
	}
	return actionIDs, nil
}

type validateFlags int

const (
	allowCharData validateFlags = 1 << 1
)

func validateElement(element Element, name string, flags validateFlags) error {
	if len(element.UnknownAttrs) != 0 {
		return fmt.Errorf("%s element contains unexpected attributes", name)
	}
	if len(element.UnknownChildren) != 0 {
		return fmt.Errorf("%s element contains unexpected children", name)
	}
	if flags&allowCharData == 0 && len(strings.TrimSpace(element.CharData)) != 0 {
		return fmt.Errorf("%s element contains unexpected character data", name)
	}
	return nil
}

func validateOptionalProperty(prop []Element, name, parent string) error {
	switch len(prop) {
	case 0:
		// nothing
	case 1:
		if err := validateElement(prop[0], name, allowCharData); err != nil {
			return err
		}
		if len(strings.TrimSpace(prop[0].CharData)) == 0 {
			return fmt.Errorf("%s element has no character data", name)
		}
	default:
		return fmt.Errorf("multiple %s elements found under %s", name, parent)
	}
	return nil
}

func validateAction(action action, seenIDs map[string]struct{}) error {
	if err := validateElement(action.Element, "<action>", 0); err != nil {
		return err
	}

	if action.ID == "" {
		return fmt.Errorf("<action> elements must have an ID")
	}
	seenIDs[action.ID] = struct{}{}

	if err := validateOptionalProperty(action.Vendor, "<vendor>", "<action>"); err != nil {
		return err
	}
	if err := validateOptionalProperty(action.VendorURL, "<vendor_url>", "<action>"); err != nil {
		return err
	}
	if err := validateOptionalProperty(action.IconName, "<icon_name>", "<action>"); err != nil {
		return err
	}

	// There must be at least one description
	if len(action.Description) == 0 {
		return fmt.Errorf("<action> element missing <description> child")
	}
	for _, d := range action.Description {
		if err := validateElement(d.Element, "<description>", allowCharData); err != nil {
			return err
		}
	}

	// There must be at least one message
	if len(action.Message) == 0 {
		return fmt.Errorf("<action> element missing <message> child")
	}
	for _, m := range action.Message {
		if err := validateElement(m.Element, "<message>", allowCharData); err != nil {
			return err
		}
	}

	// Check defaults
	if err := validateActionDefaults(action.Defaults); err != nil {
		return err
	}

	// Check annotations
	for _, annotation := range action.Annotate {
		if err := validateElement(annotation.Element, "<annotate>", allowCharData); err != nil {
			return err
		}
		if len(annotation.Key) == 0 {
			return fmt.Errorf("<annotate> elements must have a key attribute")
		}
		value := strings.TrimSpace(annotation.CharData)
		if len(value) == 0 {
			return fmt.Errorf("<annotate> elements must contain character data")
		}

		// Is this a known annotation?
		switch annotation.Key {
		case "org.freedesktop.policykit.imply":
			// Value contains a space separated list of action IDs
			for _, id := range strings.Fields(value) {
				seenIDs[id] = struct{}{}
			}

		default:
			return fmt.Errorf("unsupported annotation %q", annotation.Key)
		}
	}

	return nil
}

func validateActionDefaults(defaults []defaults) error {
	switch len(defaults) {
	case 0:
		return nil
	case 1:
		// nothing
	default:
		return fmt.Errorf("<action> element has multiple <defaults> children")
	}

	d := defaults[0]
	if err := validateElement(d.Element, "<defaults>", 0); err != nil {
		return err
	}
	if err := validateDefaultAuth(d.AllowAny, "<allow_any>"); err != nil {
		return err
	}
	if err := validateDefaultAuth(d.AllowInactive, "<allow_inactive>"); err != nil {
		return err
	}
	if err := validateDefaultAuth(d.AllowActive, "<allow_active>"); err != nil {
		return err
	}

	return nil
}

func validateDefaultAuth(auth []Element, name string) error {
	switch len(auth) {
	case 0:
		// nothing
	case 1:
		if err := validateElement(auth[0], name, allowCharData); err != nil {
			return err
		}
		value := strings.TrimSpace(auth[0].CharData)
		switch value {
		case "no", "yes", "auth_self", "auth_admin", "auth_self_keep", "auth_admin_keep":
			// nothing
		default:
			return fmt.Errorf("invalid value for %s: %q", name, value)
		}
	default:
		return fmt.Errorf("multiple %s elements found under <defaults>", name)
	}
	return nil
}

var ruleNameSuffixRegexp = regexp.MustCompile(`^(\w[\w-]*)(\.\w[\w-]*)*$`)

const maxRuleNameSuffixLength = 64

// ValidateRuleFileName validates polkit rule file name to be installed
// according to the following rules:
//   - Suffix passes ValidateRuleNameSuffix.
//   - Ends with ".rules".
func ValidateRuleFileName(filename string) error {
	base := filepath.Base(filename)
	if !strings.HasSuffix(base, ".rules") {
		return fmt.Errorf("invalid polkit rule file name: %q must end with \".rules\"", filename)
	}
	suffix := strings.TrimSuffix(base, ".rules")
	if err := ValidateRuleNameSuffix(suffix); err != nil {
		return fmt.Errorf("invalid polkit rule file name: %v", err)
	}
	return nil
}

// ValidateRuleFileName validates polkit rule file name suffix to be installed
// according to the following rules:
//   - A sequence of non-empty elements separated by dots.
//   - Each of which contains only characters from the set [A-Za-z0-9-_].
//   - Has maximum length of maxRuleFileNameLength characters.
func ValidateRuleNameSuffix(suffix string) error {
	if len(suffix) == 0 {
		return fmt.Errorf("rule file name cannot be empty")
	}
	if len(suffix) > maxRuleNameSuffixLength {
		return fmt.Errorf("%q is longer than %d characters", suffix, maxRuleNameSuffixLength)
	}
	if !ruleNameSuffixRegexp.MatchString(suffix) {
		return fmt.Errorf("%q does not match %q", suffix, ruleNameSuffixRegexp)
	}
	return nil
}