Debian Codegraph @

archive.go

raw
1 package desync
2
3 import (
4 "fmt"
5 "io"
6 "os"
7 "path"
8 "path/filepath"
9 "reflect"
10 "strings"
11 "time"
12 )
13
14 type Xattrs map[string]string
15
16 // NodeDirectory represents a directory in a catar archive
17 type NodeDirectory struct {
18 Name string
19 UID int
20 GID int
21 Mode os.FileMode
22 MTime time.Time
23 Xattrs Xattrs
24 }
25
26 // NodeFile holds file permissions and data in a catar archive
27 type NodeFile struct {
28 UID int
29 GID int
30 Mode os.FileMode
31 Name string
32 MTime time.Time
33 Xattrs Xattrs
34 Size uint64
35 Data io.Reader
36 }
37
38 // NodeSymlink holds symlink information in a catar archive
39 type NodeSymlink struct {
40 Name string
41 UID int
42 GID int
43 Mode os.FileMode
44 MTime time.Time
45 Xattrs Xattrs
46 Target string
47 }
48
49 // NodeDevice holds device information in a catar archive
50 type NodeDevice struct {
51 Name string
52 UID int
53 GID int
54 Mode os.FileMode
55 Major uint64
56 Minor uint64
57 Xattrs Xattrs
58 MTime time.Time
59 }
60
61 // ArchiveDecoder is used to decode a catar archive.
62 type ArchiveDecoder struct {
63 d FormatDecoder
64 dir string
65 last any
66 }
67
68 // NewArchiveDecoder initializes a decoder for a catar archive.
69 func NewArchiveDecoder(r io.Reader) ArchiveDecoder {
70 return ArchiveDecoder{d: NewFormatDecoder(r), dir: "."}
71 }
72
73 // safeComponent validates a single path component as it appears in a catar
74 // FormatFilename element. casync filenames are always a single, non-empty
75 // path component. Anything else (empty, ".", "..", containing a path
76 // separator, or absolute) is rejected so that a crafted archive cannot place
77 // or traverse entries outside the extraction root - this catches the
78 // embedded-slash trick (e.g. "evil/passwd") regardless of the writer in use.
79 func safeComponent(name string) error {
80 switch name {
81 case "", ".", "..":
82 return InvalidFormat{Msg: fmt.Sprintf("invalid filename %q in archive", name)}
83 }
84 if strings.ContainsRune(name, '/') || strings.ContainsRune(name, '\\') {
85 return InvalidFormat{Msg: fmt.Sprintf("filename %q contains a path separator", name)}
86 }
87 if path.IsAbs(name) || filepath.IsAbs(name) {
88 return InvalidFormat{Msg: fmt.Sprintf("absolute filename %q in archive", name)}
89 }
90 return nil
91 }
92
93 // confined reports whether p, the cumulative path of an archive entry, stays
94 // within the archive root (".").
95 func confined(p string) bool {
96 if path.IsAbs(p) {
97 return false
98 }
99 c := path.Clean(p)
100 return c == "." || (c != ".." && !strings.HasPrefix(c, "../"))
101 }
102
103 // Next returns a node from an archive, or nil if the end is reached. If NodeFile
104 // is returned, the caller should read the file body before calling Next() again
105 // as that invalidates the reader.
106 func (a *ArchiveDecoder) Next() (any, error) {
107 var (
108 entry *FormatEntry
109 payload *FormatPayload
110 symlink *FormatSymlink
111 device *FormatDevice
112 xattrs map[string]string
113 name string
114 c any
115 err error
116 )
117
118 loop:
119 for {
120 // First process any elements left over from the last loop before reading
121 // new ones from the decoder
122 if a.last != nil {
123 c = a.last
124 a.last = nil
125 } else {
126 c, err = a.d.Next()
127 if err != nil {
128 return nil, err
129 }
130 }
131
132 switch d := c.(type) {
133 case FormatEntry:
134 if entry != nil {
135 return nil, InvalidFormat{}
136 }
137 entry = &d
138 case FormatUser: // Not supported yet
139 case FormatGroup:
140 case FormatSELinux:
141 case FormatACLUser:
142 case FormatACLGroup:
143 case FormatACLGroupObj:
144 case FormatACLDefault:
145 case FormatFCaps:
146 case FormatPayload:
147 if entry == nil {
148 return nil, InvalidFormat{}
149 }
150 payload = &d
151 break loop
152 case FormatXAttr:
153 idx := strings.IndexRune(d.NameAndValue, '\000')
154 if entry == nil || idx == -1 {
155 return nil, InvalidFormat{}
156 }
157 if xattrs == nil {
158 xattrs = make(map[string]string)
159 }
160 xattrs[d.NameAndValue[0:idx]] = d.NameAndValue[idx+1:]
161 case FormatSymlink:
162 if entry == nil {
163 return nil, InvalidFormat{}
164 }
165 symlink = &d
166 case FormatDevice:
167 if entry == nil {
168 return nil, InvalidFormat{}
169 }
170 device = &d
171 case FormatFilename:
172 if entry != nil { // Store and come back to it in the next iteration
173 a.last = c
174 break loop
175 }
176 if err := safeComponent(d.Name); err != nil {
177 return nil, err
178 }
179 name = d.Name
180 case FormatGoodbye: // This will effectively be a "cd .."
181 if entry != nil {
182 a.last = c
183 break loop
184 }
185 // path.Dir, not filepath.Dir: node names are slash-separated
186 // (built with path.Join below) and have to stay that way on
187 // Windows too, where filepath.Dir would rewrite the separators.
188 a.dir = path.Dir(a.dir)
189 case nil:
190 return nil, nil
191
192 default:
193 return nil, fmt.Errorf("unsupported element %s in archive", reflect.TypeOf(d))
194 }
195 }
196
197 // If it doesn't have a payload or is a device/symlink, it must be a directory
198 if payload == nil && device == nil && symlink == nil {
199 a.dir = path.Join(a.dir, name)
200 if !confined(a.dir) {
201 return nil, InvalidFormat{Msg: fmt.Sprintf("entry %q escapes the archive root", a.dir)}
202 }
203 return NodeDirectory{
204 Name: a.dir,
205 UID: entry.UID,
206 GID: entry.GID,
207 Mode: entry.Mode,
208 MTime: entry.MTime,
209 Xattrs: xattrs,
210 }, nil
211 }
212
213 p := path.Join(a.dir, name)
214 if !confined(p) {
215 return nil, InvalidFormat{Msg: fmt.Sprintf("entry %q escapes the archive root", p)}
216 }
217
218 // Regular file
219 if payload != nil {
220 return NodeFile{
221 Name: p,
222 UID: entry.UID,
223 GID: entry.GID,
224 Mode: entry.Mode,
225 MTime: entry.MTime,
226 Xattrs: xattrs,
227 Size: payload.Size - 16,
228 Data: payload.Data,
229 }, nil
230 }
231
232 // Device
233 if device != nil {
234 return NodeDevice{
235 Name: p,
236 UID: entry.UID,
237 GID: entry.GID,
238 Mode: entry.Mode,
239 MTime: entry.MTime,
240 Xattrs: xattrs,
241 Major: device.Major,
242 Minor: device.Minor,
243 }, nil
244 }
245
246 // Symlink
247 if symlink != nil {
248 return NodeSymlink{
249 Name: p,
250 UID: entry.UID,
251 GID: entry.GID,
252 Mode: entry.Mode,
253 MTime: entry.MTime,
254 Xattrs: xattrs,
255 Target: symlink.Target,
256 }, nil
257 }
258
259 return nil, nil
260 }