aboutsummaryrefslogtreecommitdiff
path: root/cmd/diff_target_files/allow_list.go
blob: ca55b43e1fd7ab46d267381ccc1f659594bb53d0 (plain)
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
// Copyright 2019 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"bufio"
	"bytes"
	"encoding/json"
	"io"
	"os"
	"regexp"
	"strings"
	"unicode"
)

type allowList struct {
	path                string
	ignoreMatchingLines []string
}

func parseAllowLists(allowLists []string, allowListFiles []string) ([]allowList, error) {
	var ret []allowList

	add := func(path string, ignoreMatchingLines []string) {
		for _, x := range ret {
			if x.path == path {
				x.ignoreMatchingLines = append(x.ignoreMatchingLines, ignoreMatchingLines...)
				return
			}
		}

		ret = append(ret, allowList{
			path:                path,
			ignoreMatchingLines: ignoreMatchingLines,
		})
	}

	for _, file := range allowListFiles {
		newAllowlists, err := parseAllowListFile(file)
		if err != nil {
			return nil, err
		}

		for _, w := range newAllowlists {
			add(w.path, w.ignoreMatchingLines)
		}
	}

	for _, s := range allowLists {
		colon := strings.IndexRune(s, ':')
		var ignoreMatchingLines []string
		if colon >= 0 {
			ignoreMatchingLines = []string{s[colon+1:]}
		}
		add(s, ignoreMatchingLines)
	}

	return ret, nil
}

func parseAllowListFile(file string) ([]allowList, error) {
	r, err := os.Open(file)
	if err != nil {
		return nil, err
	}
	defer r.Close()

	d := json.NewDecoder(newJSONCommentStripper(r))

	var jsonAllowLists []struct {
		Paths               []string
		IgnoreMatchingLines []string
	}

	if err := d.Decode(&jsonAllowLists); err != nil {
		return nil, err
	}

	var allowLists []allowList
	for _, w := range jsonAllowLists {
		for _, p := range w.Paths {
			allowLists = append(allowLists, allowList{
				path:                p,
				ignoreMatchingLines: w.IgnoreMatchingLines,
			})
		}
	}

	return allowLists, err
}

func filterModifiedPaths(l [][2]*ZipArtifactFile, allowLists []allowList) ([][2]*ZipArtifactFile, error) {
outer:
	for i := 0; i < len(l); i++ {
		for _, w := range allowLists {
			if match, err := Match(w.path, l[i][0].Name); err != nil {
				return l, err
			} else if match {
				if match, err := diffIgnoringMatchingLines(l[i][0], l[i][1], w.ignoreMatchingLines); err != nil {
					return l, err
				} else if match || len(w.ignoreMatchingLines) == 0 {
					l = append(l[:i], l[i+1:]...)
					i--
				}
				continue outer
			}
		}
	}

	if len(l) == 0 {
		l = nil
	}

	return l, nil
}

func filterNewPaths(l []*ZipArtifactFile, allowLists []allowList) ([]*ZipArtifactFile, error) {
outer:
	for i := 0; i < len(l); i++ {
		for _, w := range allowLists {
			if match, err := Match(w.path, l[i].Name); err != nil {
				return l, err
			} else if match && len(w.ignoreMatchingLines) == 0 {
				l = append(l[:i], l[i+1:]...)
				i--
			}
			continue outer
		}
	}

	if len(l) == 0 {
		l = nil
	}

	return l, nil
}

func diffIgnoringMatchingLines(a *ZipArtifactFile, b *ZipArtifactFile, ignoreMatchingLines []string) (match bool, err error) {
	lineMatchesIgnores := func(b []byte) (bool, error) {
		for _, m := range ignoreMatchingLines {
			if match, err := regexp.Match(m, b); err != nil {
				return false, err
			} else if match {
				return match, nil
			}
		}
		return false, nil
	}

	filter := func(z *ZipArtifactFile) ([]byte, error) {
		var ret []byte

		r, err := z.Open()
		if err != nil {
			return nil, err
		}
		s := bufio.NewScanner(r)

		for s.Scan() {
			if match, err := lineMatchesIgnores(s.Bytes()); err != nil {
				return nil, err
			} else if !match {
				ret = append(ret, "\n"...)
				ret = append(ret, s.Bytes()...)
			}
		}

		return ret, nil
	}

	bufA, err := filter(a)
	if err != nil {
		return false, err
	}
	bufB, err := filter(b)
	if err != nil {
		return false, err
	}

	return bytes.Compare(bufA, bufB) == 0, nil
}

func applyAllowLists(diff zipDiff, allowLists []allowList) (zipDiff, error) {
	var err error

	diff.modified, err = filterModifiedPaths(diff.modified, allowLists)
	if err != nil {
		return diff, err
	}
	diff.onlyInA, err = filterNewPaths(diff.onlyInA, allowLists)
	if err != nil {
		return diff, err
	}
	diff.onlyInB, err = filterNewPaths(diff.onlyInB, allowLists)
	if err != nil {
		return diff, err
	}

	return diff, nil
}

func newJSONCommentStripper(r io.Reader) *jsonCommentStripper {
	return &jsonCommentStripper{
		r: bufio.NewReader(r),
	}
}

type jsonCommentStripper struct {
	r   *bufio.Reader
	b   []byte
	err error
}

func (j *jsonCommentStripper) Read(buf []byte) (int, error) {
	for len(j.b) == 0 {
		if j.err != nil {
			return 0, j.err
		}

		j.b, j.err = j.r.ReadBytes('\n')

		if isComment(j.b) {
			j.b = nil
		}
	}

	n := copy(buf, j.b)
	j.b = j.b[n:]
	return n, nil
}

var commentPrefix = []byte("//")

func isComment(b []byte) bool {
	for len(b) > 0 && unicode.IsSpace(rune(b[0])) {
		b = b[1:]
	}
	return bytes.HasPrefix(b, commentPrefix)
}