-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
file.go
221 lines (191 loc) · 5.21 KB
/
file.go
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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package osx
import (
"encoding/base64"
"io"
"net/url"
"os"
"strings"
"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
"github.com/ory/x/httpx"
)
type options struct {
disableFileLoader bool
disableHTTPLoader bool
disableBase64Loader bool
base64enc *base64.Encoding
disableResilientBase64Loader bool
hc *retryablehttp.Client
}
type Option func(o *options)
func (o *options) apply(opts []Option) *options {
for _, f := range opts {
f(o)
}
return o
}
func newOptions() *options {
return &options{
disableFileLoader: false,
disableHTTPLoader: false,
disableBase64Loader: false,
base64enc: base64.RawURLEncoding,
hc: httpx.NewResilientClient(),
}
}
// WithDisabledFileLoader disables the file loader.
func WithDisabledFileLoader() Option {
return func(o *options) {
o.disableFileLoader = true
}
}
// WithEnabledFileLoader enables the file loader.
func WithEnabledFileLoader() Option {
return func(o *options) {
o.disableFileLoader = false
}
}
// WithDisabledHTTPLoader disables the HTTP loader.
func WithDisabledHTTPLoader() Option {
return func(o *options) {
o.disableHTTPLoader = true
}
}
// WithEnabledHTTPLoader enables the HTTP loader.
func WithEnabledHTTPLoader() Option {
return func(o *options) {
o.disableHTTPLoader = false
}
}
// WithDisabledBase64Loader disables the base64 loader.
func WithDisabledBase64Loader() Option {
return func(o *options) {
o.disableBase64Loader = true
}
}
// WithEnabledBase64Loader disables the base64 loader.
func WithEnabledBase64Loader() Option {
return func(o *options) {
o.disableBase64Loader = false
}
}
// WithBase64Encoding sets the base64 encoding.
func WithBase64Encoding(enc *base64.Encoding) Option {
return func(o *options) {
o.base64enc = enc
}
}
// WithoutResilientBase64Encoding sets the base64 encoding.
func WithoutResilientBase64Encoding() Option {
return func(o *options) {
o.disableResilientBase64Loader = true
}
}
// WithHTTPClient sets the HTTP client.
func WithHTTPClient(hc *retryablehttp.Client) Option {
return func(o *options) {
o.hc = hc
}
}
// RestrictedReadFile works similar to ReadFileFromAllSources but has all
// sources disabled per default. You need to enable the loaders you wish to use
// explicitly.
func RestrictedReadFile(source string, opts ...Option) (bytes []byte, err error) {
o := newOptions()
o.disableFileLoader = true
o.disableBase64Loader = true
o.disableHTTPLoader = true
return readFile(source, o.apply(opts))
}
// ReadFileFromAllSources reads a file from base64, http, https, and file sources.
//
// Using options, you can disable individual loaders. For example, the following will
// return an error:
//
// ReadFileFromAllSources("https://foo.bar/baz.txt", WithDisabledHTTPLoader())
//
// Possible formats are:
//
// - /path/to/file
// - file:///path/to/file
// - https://host.com/path/to/file
// - http://host.com/path/to/file
// - base64://<base64 encoded string>
//
// For more options, check:
//
// - WithDisabledFileLoader
// - WithDisabledHTTPLoader
// - WithDisabledBase64Loader
// - WithBase64Encoding
// - WithHTTPClient
func ReadFileFromAllSources(source string, opts ...Option) (bytes []byte, err error) {
return readFile(source, newOptions().apply(opts))
}
func readFile(source string, o *options) (bytes []byte, err error) {
parsed, err := url.Parse(source)
if err != nil {
return nil, errors.Wrap(err, "failed to parse URL")
}
switch parsed.Scheme {
case "":
if o.disableFileLoader {
return nil, errors.New("file loader disabled")
}
//#nosec G304 -- false positive
bytes, err = os.ReadFile(source)
if err != nil {
return nil, errors.Wrap(err, "unable to read the file")
}
case "file":
if o.disableFileLoader {
return nil, errors.New("file loader disabled")
}
//#nosec G304 -- false positive
bytes, err = os.ReadFile(parsed.Host + parsed.Path)
if err != nil {
return nil, errors.Wrap(err, "unable to read the file")
}
case "http", "https":
if o.disableHTTPLoader {
return nil, errors.New("http(s) loader disabled")
}
resp, err := o.hc.Get(parsed.String())
if err != nil {
return nil, errors.Wrap(err, "unable to load remote file")
}
defer resp.Body.Close()
bytes, err = io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "unable to read the HTTP response body")
}
case "base64":
if o.disableBase64Loader {
return nil, errors.New("base64 loader disabled")
}
if o.disableResilientBase64Loader {
bytes, err = o.base64enc.DecodeString(strings.TrimPrefix(source, "base64://"))
if err != nil {
return nil, errors.Wrap(err, "unable to base64 decode the location")
}
return bytes, nil
}
for _, enc := range []*base64.Encoding{
base64.StdEncoding,
base64.URLEncoding,
base64.RawURLEncoding,
base64.RawStdEncoding,
} {
bytes, err = enc.DecodeString(strings.TrimPrefix(source, "base64://"))
if err == nil {
return bytes, nil
}
}
return nil, errors.Wrap(err, "unable to base64 decode the location")
default:
return nil, errors.Errorf("unsupported source `%s`", parsed.Scheme)
}
return bytes, nil
}