| 1 |
// Copyright Martin Dosch. |
| 2 |
// Use of this source code is governed by the BSD-2-clause |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto/aes" |
| 10 |
"crypto/cipher" |
| 11 |
"crypto/rand" |
| 12 |
"encoding/xml" |
| 13 |
"fmt" |
| 14 |
"log/slog" |
| 15 |
"net/http" |
| 16 |
"net/url" |
| 17 |
"os" |
| 18 |
"path/filepath" |
| 19 |
"regexp" |
| 20 |
"strconv" |
| 21 |
"strings" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/ProtonMail/gopenpgp/v3/crypto" // MIT License |
| 25 |
"github.com/beevik/etree" // BSD-2-clause |
| 26 |
"github.com/gabriel-vasile/mimetype" // MIT License |
| 27 |
"github.com/xmppo/go-xmpp" // BSD-3-Clause |
| 28 |
) |
| 29 |
|
| 30 |
func httpUpload(client *xmpp.Client, iqc chan xmpp.IQ, disc chan xmpp.DiscoItems, drc chan xmpp.DiscoResult, slc chan xmpp.Slot, |
| 31 |
jserver string, filePaths []string, timeout time.Duration, oxPrivKey *crypto.Key, keyRing *crypto.KeyRing, oxEnc bool, |
| 32 |
) (urls []string, err error) { |
| 33 |
var ( |
| 34 |
uploadComponent string |
| 35 |
maxFileSize int64 |
| 36 |
req *http.Request |
| 37 |
aesKey []byte |
| 38 |
nonce []byte |
| 39 |
) |
| 40 |
|
| 41 |
buffer := new(bytes.Buffer) |
| 42 |
|
| 43 |
// Query server for disco#items |
| 44 |
slog.Info("http-upload: querying disco items", "server", jserver) |
| 45 |
discoItems, err := getDiscoItems(client, disc, jserver) |
| 46 |
if err != nil { |
| 47 |
return urls, err |
| 48 |
} |
| 49 |
|
| 50 |
// Check the services reported by disco#items for the http upload service |
| 51 |
for _, r := range discoItems.Items { |
| 52 |
slog.Info("http-upload: querying disco info", "disco-item", r.Jid) |
| 53 |
// Don't check error as it seems some components in the wild do not reply. |
| 54 |
discoResult, err := getDiscoInfo(client, drc, r.Jid) |
| 55 |
if err != nil { |
| 56 |
continue |
| 57 |
} |
| 58 |
slog.Info("http-upload: checking identities for type = \"file\" and category = \"store\"") |
| 59 |
for _, identity := range discoResult.Identities { |
| 60 |
if identity.Type == "file" && |
| 61 |
identity.Category == "store" { |
| 62 |
uploadComponent = r.Jid |
| 63 |
slog.Info("http-upload:", "upload component", uploadComponent) |
| 64 |
for _, x := range discoResult.X { |
| 65 |
for i, field := range x.Field { |
| 66 |
if field.Var == "max-file-size" { |
| 67 |
if i > 0 { |
| 68 |
if x.Field[i-1].Value[0] == nsHTTPUpload { |
| 69 |
maxFileSize, err = strconv.ParseInt(field.Value[0], 10, 64) |
| 70 |
if err != nil { |
| 71 |
return urls, fmt.Errorf("http-upload: error while checking server maximum http upload file size") |
| 72 |
} |
| 73 |
slog.Info("http-upload:", "max file size", maxFileSize) |
| 74 |
break |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
break |
| 81 |
} |
| 82 |
if uploadComponent != "" { |
| 83 |
break |
| 84 |
} |
| 85 |
} |
| 86 |
if uploadComponent != "" { |
| 87 |
break |
| 88 |
} |
| 89 |
} |
| 90 |
if uploadComponent == "" { |
| 91 |
return urls, fmt.Errorf("http-upload: no http upload component found") |
| 92 |
} |
| 93 |
for _, filePath := range filePaths { |
| 94 |
// Read file |
| 95 |
// #nosec G304 -- file path is supplied by the user, it's no untrusted input from the network |
| 96 |
file, err := os.Open(filePath) |
| 97 |
if err != nil { |
| 98 |
return urls, fmt.Errorf("http-upload: read file: %w", err) |
| 99 |
} |
| 100 |
|
| 101 |
fileStat, err := file.Stat() |
| 102 |
if err != nil { |
| 103 |
err2 := file.Close() |
| 104 |
if err2 != nil { |
| 105 |
slog.Warn("closing file failed", "error", err2) |
| 106 |
} |
| 107 |
return urls, fmt.Errorf("http-upload: stat file: %w", err) |
| 108 |
} |
| 109 |
fileSize := fileStat.Size() |
| 110 |
slog.Info("http-upload: checking file size:", "file", filePath, "size", fileSize) |
| 111 |
// Get mime type |
| 112 |
mtype, err := mimetype.DetectFile(filePath) |
| 113 |
if err != nil { |
| 114 |
err2 := file.Close() |
| 115 |
if err2 != nil { |
| 116 |
slog.Warn("closing file failed", "error", err2) |
| 117 |
} |
| 118 |
return urls, fmt.Errorf("http-upload: detect mimetype: %w", err) |
| 119 |
} |
| 120 |
mimeType := mtype.String() |
| 121 |
slog.Info("http-upload:", "mime type", mimeType) |
| 122 |
var mimeTypeEscaped bytes.Buffer |
| 123 |
xml.Escape(&mimeTypeEscaped, []byte(mimeType)) |
| 124 |
if oxEnc { |
| 125 |
buf, err := readFile(filePath) |
| 126 |
if err != nil { |
| 127 |
err2 := file.Close() |
| 128 |
if err2 != nil { |
| 129 |
slog.Warn("closing file failed", "error", err2) |
| 130 |
} |
| 131 |
return nil, fmt.Errorf("http-upload: %w", err) |
| 132 |
} |
| 133 |
|
| 134 |
aesKey = make([]byte, 32) |
| 135 |
_, err = rand.Read(aesKey) |
| 136 |
if err != nil { |
| 137 |
return nil, fmt.Errorf("http-upload: ox: generating aes key: %w", err) |
| 138 |
} |
| 139 |
block, err := aes.NewCipher(aesKey) |
| 140 |
if err != nil { |
| 141 |
return nil, fmt.Errorf("http-upload: ox: generating block cipher: %w", err) |
| 142 |
} |
| 143 |
nonce = make([]byte, 12) |
| 144 |
_, err = rand.Read(nonce) |
| 145 |
if err != nil { |
| 146 |
return nil, fmt.Errorf("http-upload: ox: generating nonce: %w", err) |
| 147 |
} |
| 148 |
aesgcm, err := cipher.NewGCM(block) |
| 149 |
if err != nil { |
| 150 |
return nil, fmt.Errorf("http-upload: ox: %w", err) |
| 151 |
} |
| 152 |
encBytes := aesgcm.Seal(nil, nonce, buf.Bytes(), nil) |
| 153 |
buffer = bytes.NewBuffer(encBytes) |
| 154 |
fileSize = int64(buffer.Len()) |
| 155 |
err = file.Close() |
| 156 |
if err != nil { |
| 157 |
slog.Warn("closing file failed", "error", err) |
| 158 |
} |
| 159 |
} |
| 160 |
if oxPrivKey != nil { |
| 161 |
buf, err := readFile(filePath) |
| 162 |
if err != nil { |
| 163 |
err2 := file.Close() |
| 164 |
if err2 != nil { |
| 165 |
slog.Warn("closing file failed", "error", err2) |
| 166 |
} |
| 167 |
return nil, fmt.Errorf("http-upload: %w", err) |
| 168 |
} |
| 169 |
buffer, err = legacyPGPEncryptFile(oxPrivKey, keyRing, buf) |
| 170 |
if err != nil { |
| 171 |
err2 := file.Close() |
| 172 |
if err2 != nil { |
| 173 |
slog.Warn("closing file failed", "error", err2) |
| 174 |
} |
| 175 |
return urls, err |
| 176 |
} |
| 177 |
fileSize = int64(buffer.Len()) |
| 178 |
slog.Info("http-upload: updated file size after encrypting:", "file", filePath, "size", fileSize) |
| 179 |
} |
| 180 |
// Get file name |
| 181 |
fileName := filepath.Base(filePath) |
| 182 |
if oxPrivKey != nil { |
| 183 |
fileName = getID() + ".pgp" |
| 184 |
} |
| 185 |
// Just use alphanumerical and some special characters for now |
| 186 |
// to work around https://github.com/xmppo/go-xmpp/issues/132 |
| 187 |
reg := regexp.MustCompile(`[^a-zA-Z0-9\+\-\_\.]+`) |
| 188 |
fileNameEscaped := reg.ReplaceAllString(fileName, "_") |
| 189 |
slog.Info("http-upload: escape file", "name", fileNameEscaped) |
| 190 |
|
| 191 |
// Check if the file size doesn't exceed the maximum file size of the http upload |
| 192 |
// component if a maximum file size is reported, if not just continue and hope for |
| 193 |
// the best. |
| 194 |
if maxFileSize != 0 { |
| 195 |
if int64(fileSize) > maxFileSize { |
| 196 |
err2 := file.Close() |
| 197 |
if err2 != nil { |
| 198 |
slog.Warn("closing file failed", "error", err2) |
| 199 |
} |
| 200 |
return urls, fmt.Errorf("http-upload: file size %s MiB is larger than the maximum file size allowed (%s MiB)", |
| 201 |
strconv.FormatInt(int64(fileSize)/1024/1024, 10), strconv.FormatInt(maxFileSize/1024/1024, 10)) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
request := etree.NewDocument() |
| 206 |
request.WriteSettings.AttrSingleQuote = true |
| 207 |
requestReq := request.CreateElement("request") |
| 208 |
requestReq.CreateAttr("xmlns", nsHTTPUpload) |
| 209 |
requestReq.CreateAttr("filename", fileNameEscaped) |
| 210 |
requestReq.CreateAttr("size", fmt.Sprint(fileSize)) |
| 211 |
requestReq.CreateAttr("content-type", mimeType) |
| 212 |
r, err := request.WriteToString() |
| 213 |
if err != nil { |
| 214 |
err2 := file.Close() |
| 215 |
if err2 != nil { |
| 216 |
slog.Warn("closing file failed", "error", err2) |
| 217 |
} |
| 218 |
return urls, err |
| 219 |
} |
| 220 |
|
| 221 |
// Request http upload slot |
| 222 |
slog.Info("http-upload: requesting upload slot") |
| 223 |
uploadSlot, err := getSlot(client, slc, uploadComponent, r) |
| 224 |
if err != nil { |
| 225 |
err2 := file.Close() |
| 226 |
if err2 != nil { |
| 227 |
slog.Warn("closing file failed", "error", err2) |
| 228 |
} |
| 229 |
return urls, err |
| 230 |
} |
| 231 |
if !strings.HasPrefix(uploadSlot.Put.Url, "https://") { |
| 232 |
err2 := file.Close() |
| 233 |
if err2 != nil { |
| 234 |
slog.Warn("closing file failed", "error", err2) |
| 235 |
} |
| 236 |
return urls, fmt.Errorf("http-upload: upload slot does not provide https") |
| 237 |
} |
| 238 |
// Upload file |
| 239 |
slog.Info("http-upload: uploading file to", "slot", uploadSlot.Put.Url) |
| 240 |
httpTransport := &http.Transport{ |
| 241 |
IdleConnTimeout: timeout, |
| 242 |
TLSHandshakeTimeout: timeout, |
| 243 |
} |
| 244 |
proxyEnv := os.Getenv("HTTP_PROXY") |
| 245 |
slog.Info("http-upload: getting environment variable:", "HTTP_PROXY", proxyEnv) |
| 246 |
if proxyEnv != "" { |
| 247 |
proxyURL, err := url.Parse(proxyEnv) |
| 248 |
if err != nil { |
| 249 |
err2 := file.Close() |
| 250 |
if err2 != nil { |
| 251 |
slog.Warn("closing file failed", "error", err2) |
| 252 |
} |
| 253 |
return urls, err |
| 254 |
} |
| 255 |
slog.Info("http-upload: using http transport", "proxy", proxyURL) |
| 256 |
httpTransport.Proxy = http.ProxyURL(proxyURL) |
| 257 |
} |
| 258 |
httpClient := &http.Client{Transport: httpTransport} |
| 259 |
if oxEnc || oxPrivKey != nil { |
| 260 |
req, err = http.NewRequest(http.MethodPut, uploadSlot.Put.Url, buffer) |
| 261 |
} else { |
| 262 |
req, err = http.NewRequest(http.MethodPut, uploadSlot.Put.Url, file) |
| 263 |
} |
| 264 |
if err != nil { |
| 265 |
err2 := file.Close() |
| 266 |
if err2 != nil { |
| 267 |
slog.Warn("closing file failed", "error", err2) |
| 268 |
} |
| 269 |
return urls, err |
| 270 |
} |
| 271 |
req.ContentLength = fileSize |
| 272 |
req.Header.Set("Content-Type", mimeTypeEscaped.String()) |
| 273 |
for _, h := range uploadSlot.Put.Headers { |
| 274 |
if h.Name == strEmpty { |
| 275 |
continue |
| 276 |
} |
| 277 |
switch h.Name { |
| 278 |
case "Authorization", "Cookie", "Expires": |
| 279 |
req.Header.Set(h.Name, h.Value) |
| 280 |
} |
| 281 |
} |
| 282 |
resp, err := httpClient.Do(req) |
| 283 |
if err != nil { |
| 284 |
err2 := file.Close() |
| 285 |
if err2 != nil { |
| 286 |
slog.Warn("closing file failed", "error", err2) |
| 287 |
} |
| 288 |
return urls, err |
| 289 |
} |
| 290 |
slog.Info("http-upload: received http", "status", resp.StatusCode) |
| 291 |
// Test for http status code "200 OK" or "201 Created" |
| 292 |
if resp.StatusCode != 200 && resp.StatusCode != 201 { |
| 293 |
err2 := file.Close() |
| 294 |
if err2 != nil { |
| 295 |
slog.Warn("closing file failed", "error", err2) |
| 296 |
} |
| 297 |
return urls, fmt.Errorf("http-upload: upload failed") |
| 298 |
} |
| 299 |
|
| 300 |
// Return http link |
| 301 |
if uploadSlot.Get.Url == strEmpty { |
| 302 |
err2 := file.Close() |
| 303 |
if err2 != nil { |
| 304 |
slog.Warn("closing file failed", "error", err2) |
| 305 |
} |
| 306 |
return urls, fmt.Errorf("http-upload: no url attribute") |
| 307 |
} |
| 308 |
if !strings.HasPrefix(uploadSlot.Get.Url, "https://") { |
| 309 |
err2 := file.Close() |
| 310 |
if err2 != nil { |
| 311 |
slog.Warn("closing file failed", "error", err2) |
| 312 |
} |
| 313 |
return urls, fmt.Errorf("http-upload: get url does not provide https") |
| 314 |
} |
| 315 |
slog.Info("http-upload: received get", "URL", uploadSlot.Get.Url) |
| 316 |
err = resp.Body.Close() |
| 317 |
if err != nil { |
| 318 |
fmt.Println("http-upload: error while closing http request body:", err) |
| 319 |
} |
| 320 |
var url string |
| 321 |
if oxEnc { |
| 322 |
url = strings.TrimPrefix(uploadSlot.Get.Url, "https://") |
| 323 |
url = fmt.Sprintf("aesgcm://%s#%x%x", url, nonce, aesKey) |
| 324 |
} else { |
| 325 |
url = uploadSlot.Get.Url |
| 326 |
} |
| 327 |
urls = append(urls, url) |
| 328 |
} |
| 329 |
return urls, nil |
| 330 |
} |