-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy patharchive.go
More file actions
260 lines (240 loc) · 5.86 KB
/
Copy patharchive.go
File metadata and controls
260 lines (240 loc) · 5.86 KB
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
package desync
import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"time"
)
type Xattrs map[string]string
// NodeDirectory represents a directory in a catar archive
type NodeDirectory struct {
Name string
UID int
GID int
Mode os.FileMode
MTime time.Time
Xattrs Xattrs
}
// NodeFile holds file permissions and data in a catar archive
type NodeFile struct {
UID int
GID int
Mode os.FileMode
Name string
MTime time.Time
Xattrs Xattrs
Size uint64
Data io.Reader
}
// NodeSymlink holds symlink information in a catar archive
type NodeSymlink struct {
Name string
UID int
GID int
Mode os.FileMode
MTime time.Time
Xattrs Xattrs
Target string
}
// NodeDevice holds device information in a catar archive
type NodeDevice struct {
Name string
UID int
GID int
Mode os.FileMode
Major uint64
Minor uint64
Xattrs Xattrs
MTime time.Time
}
// ArchiveDecoder is used to decode a catar archive.
type ArchiveDecoder struct {
d FormatDecoder
dir string
last any
}
// NewArchiveDecoder initializes a decoder for a catar archive.
func NewArchiveDecoder(r io.Reader) ArchiveDecoder {
return ArchiveDecoder{d: NewFormatDecoder(r), dir: "."}
}
// safeComponent validates a single path component as it appears in a catar
// FormatFilename element. casync filenames are always a single, non-empty
// path component. Anything else (empty, ".", "..", containing a path
// separator, or absolute) is rejected so that a crafted archive cannot place
// or traverse entries outside the extraction root - this catches the
// embedded-slash trick (e.g. "evil/passwd") regardless of the writer in use.
func safeComponent(name string) error {
switch name {
case "", ".", "..":
return InvalidFormat{Msg: fmt.Sprintf("invalid filename %q in archive", name)}
}
if strings.ContainsRune(name, '/') || strings.ContainsRune(name, '\\') {
return InvalidFormat{Msg: fmt.Sprintf("filename %q contains a path separator", name)}
}
if path.IsAbs(name) || filepath.IsAbs(name) {
return InvalidFormat{Msg: fmt.Sprintf("absolute filename %q in archive", name)}
}
return nil
}
// confined reports whether p, the cumulative path of an archive entry, stays
// within the archive root (".").
func confined(p string) bool {
if path.IsAbs(p) {
return false
}
c := path.Clean(p)
return c == "." || (c != ".." && !strings.HasPrefix(c, "../"))
}
// Next returns a node from an archive, or nil if the end is reached. If NodeFile
// is returned, the caller should read the file body before calling Next() again
// as that invalidates the reader.
func (a *ArchiveDecoder) Next() (any, error) {
var (
entry *FormatEntry
payload *FormatPayload
symlink *FormatSymlink
device *FormatDevice
xattrs map[string]string
name string
c any
err error
)
loop:
for {
// First process any elements left over from the last loop before reading
// new ones from the decoder
if a.last != nil {
c = a.last
a.last = nil
} else {
c, err = a.d.Next()
if err != nil {
return nil, err
}
}
switch d := c.(type) {
case FormatEntry:
if entry != nil {
return nil, InvalidFormat{}
}
entry = &d
case FormatUser: // Not supported yet
case FormatGroup:
case FormatSELinux:
case FormatACLUser:
case FormatACLGroup:
case FormatACLGroupObj:
case FormatACLDefault:
case FormatFCaps:
case FormatPayload:
if entry == nil {
return nil, InvalidFormat{}
}
payload = &d
break loop
case FormatXAttr:
idx := strings.IndexRune(d.NameAndValue, '\000')
if entry == nil || idx == -1 {
return nil, InvalidFormat{}
}
if xattrs == nil {
xattrs = make(map[string]string)
}
xattrs[d.NameAndValue[0:idx]] = d.NameAndValue[idx+1:]
case FormatSymlink:
if entry == nil {
return nil, InvalidFormat{}
}
symlink = &d
case FormatDevice:
if entry == nil {
return nil, InvalidFormat{}
}
device = &d
case FormatFilename:
if entry != nil { // Store and come back to it in the next iteration
a.last = c
break loop
}
if err := safeComponent(d.Name); err != nil {
return nil, err
}
name = d.Name
case FormatGoodbye: // This will effectively be a "cd .."
if entry != nil {
a.last = c
break loop
}
// path.Dir, not filepath.Dir: node names are slash-separated
// (built with path.Join below) and have to stay that way on
// Windows too, where filepath.Dir would rewrite the separators.
a.dir = path.Dir(a.dir)
case nil:
return nil, nil
default:
return nil, fmt.Errorf("unsupported element %s in archive", reflect.TypeOf(d))
}
}
// If it doesn't have a payload or is a device/symlink, it must be a directory
if payload == nil && device == nil && symlink == nil {
a.dir = path.Join(a.dir, name)
if !confined(a.dir) {
return nil, InvalidFormat{Msg: fmt.Sprintf("entry %q escapes the archive root", a.dir)}
}
return NodeDirectory{
Name: a.dir,
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
Xattrs: xattrs,
}, nil
}
p := path.Join(a.dir, name)
if !confined(p) {
return nil, InvalidFormat{Msg: fmt.Sprintf("entry %q escapes the archive root", p)}
}
// Regular file
if payload != nil {
return NodeFile{
Name: p,
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
Xattrs: xattrs,
Size: payload.Size - 16,
Data: payload.Data,
}, nil
}
// Device
if device != nil {
return NodeDevice{
Name: p,
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
Xattrs: xattrs,
Major: device.Major,
Minor: device.Minor,
}, nil
}
// Symlink
if symlink != nil {
return NodeSymlink{
Name: p,
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
Xattrs: xattrs,
Target: symlink.Target,
}, nil
}
return nil, nil
}