Skip to content

Commit 1250b94

Browse files
authored
Merge pull request #75 from matrix-org/dbkr/allow_gzipped_json
Allow gzipped json & txt within `files`
2 parents 100a1cb + eba4d0e commit 1250b94

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ logs.)
8484
* `file`: an arbitrary file to attach to the report. Saved as-is to disk, and
8585
a link is added to the github issue. The filename must be in the format
8686
`name.ext`, where `name` contains only alphanumerics, `-` or `_`, and `ext`
87-
is one of `jpg`, `png`, `txt` or `json`.
87+
is one of `jpg`, `png`, `txt`, `json`, `txt.gz` or `json.gz`.
8888

8989
Not supported for the JSON upload encoding.
9090

changelog.d/75.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow gzipped json & txt files to be uploaded as attachments to rageshakes.

logserver.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ func serveFile(w http.ResponseWriter, r *http.Request, path string) {
104104
//
105105
// Unlike mime.TypeByExtension, the results are limited to a set of types which
106106
// should be safe to serve to a browser without introducing XSS vulnerabilities.
107+
//
108+
// We handle all of the extensions we allow on files uploaded as attachments to a rageshake,
109+
// plus 'log' which we do not allow as an attachment, but is used as the extension when serving
110+
// the logs submitted as `logs` or `compressed-log`.
107111
func extensionToMimeType(path string) string {
108-
if strings.HasSuffix(path, ".txt") {
112+
if strings.HasSuffix(path, ".txt") || strings.HasSuffix(path, ".log") {
109113
// anyone uploading text in anything other than utf-8 needs to be
110114
// re-educated.
111115
return "text/plain; charset=utf-8"
@@ -236,7 +240,13 @@ func addToArchive(targz *tar.Writer, dfilename string, filename string) error {
236240
}
237241

238242
func serveGzippedFile(w http.ResponseWriter, r *http.Request, path string, size int64) {
239-
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
243+
cType := "text/plain; charset=utf-8"
244+
if strings.HasSuffix(path, ".gz") {
245+
// Guess the mime type from the extension as we do in serveFile, but without
246+
// the .gz header (in practice, either plain text or application/json).
247+
cType = extensionToMimeType(path[:len(path)-len(".gz")])
248+
}
249+
w.Header().Set("Content-Type", cType)
240250

241251
acceptsGzip := false
242252
splitRune := func(s rune) bool { return s == ' ' || s == '\t' || s == '\n' || s == ',' }

submit.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,13 @@ func formPartToPayload(field, data string, p *payload) {
405405
// * a limited set of extensions. We are careful to limit the content-types
406406
// we will serve the files with, but somebody might accidentally point an
407407
// Apache or nginx at the upload directory, which would serve js files as
408-
// application/javascript and open XSS vulnerabilities.
408+
// application/javascript and open XSS vulnerabilities. We also allow gzipped
409+
// text and json on the same basis (there's really no sense allowing gzipped images).
409410
//
410411
// * no silly characters (/, ctrl chars, etc)
411412
//
412413
// * nothing starting with '.'
413-
var filenameRegexp = regexp.MustCompile(`^[a-zA-Z0-9_-]+\.(jpg|png|txt|json)$`)
414+
var filenameRegexp = regexp.MustCompile(`^[a-zA-Z0-9_-]+\.(jpg|png|txt|json|txt\.gz|json\.gz)$`)
414415

415416
// saveFormPart saves a file upload to the report directory.
416417
//

0 commit comments

Comments
 (0)