Merge pull request #41 from matrix-org/michaelk/permit_gz_extension

Allow files ending "txt.gz" or "log.gz" to be uploaded as a log file.
This commit is contained in:
Michael Kaye 2022-01-25 10:00:46 +00:00 committed by GitHub
commit 8beb68cd85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 5 deletions

1
changelog.d/40.feature Normal file
View file

@ -0,0 +1 @@
Support element-android submitting logs with .gz suffix.

View file

@ -424,7 +424,7 @@ func saveFormPart(leafName string, reader io.Reader, reportDir string) (string,
// we require a sensible extension, and don't allow the filename to start with // we require a sensible extension, and don't allow the filename to start with
// '.' // '.'
var logRegexp = regexp.MustCompile(`^[a-zA-Z0-9_-][a-zA-Z0-9_.-]*\.(log|txt)$`) var logRegexp = regexp.MustCompile(`^[a-zA-Z0-9_-][a-zA-Z0-9_.-]*\.(log|txt)(\.gz)?$`)
// saveLogPart saves a log upload to the report directory. // saveLogPart saves a log upload to the report directory.
// //
@ -435,10 +435,16 @@ func saveLogPart(logNum int, filename string, reader io.Reader, reportDir string
// some clients use sensible names (foo.N.log), which we preserve. For // some clients use sensible names (foo.N.log), which we preserve. For
// others, we just make up a filename. // others, we just make up a filename.
// //
// Either way, we need to append .gz, because we're compressing it. // We append a ".gz" extension if not already present, as the final file we store on
// disk will be gzipped. The original filename may or may not contain a '.gz' depending
// on the client that uploaded it, and if it was uploaded already compressed.
var leafName string var leafName string
if logRegexp.MatchString(filename) { if logRegexp.MatchString(filename) {
leafName = filename + ".gz" leafName = filename
if !strings.HasSuffix(filename, ".gz") {
leafName += ".gz"
}
} else { } else {
leafName = fmt.Sprintf("logs-%04d.log.gz", logNum) leafName = fmt.Sprintf("logs-%04d.log.gz", logNum)
} }

View file

@ -160,6 +160,7 @@ func TestMultipartUpload(t *testing.T) {
// check file uploaded correctly // check file uploaded correctly
checkUploadedFile(t, reportDir, "passwd.txt", false, "bibblybobbly") checkUploadedFile(t, reportDir, "passwd.txt", false, "bibblybobbly")
checkUploadedFile(t, reportDir, "crash.log.gz", true, "test\n")
} }
func multipartBody() (body string) { func multipartBody() (body string) {
@ -215,6 +216,18 @@ Content-Type: application/octet-stream
bibblybobbly bibblybobbly
` `
body += `------WebKitFormBoundarySsdgl8Nq9voFyhdO
Content-Disposition: form-data; name="compressed-log"; filename="crash.log.gz"
Content-Type: application/octet-stream
`
body += string([]byte{
0x1f, 0x8b, 0x08, 0x00, 0xbf, 0xd8, 0xf5, 0x58, 0x00, 0x03,
0x2b, 0x49, 0x2d, 0x2e, 0xe1, 0x02, 0x00,
0xc6, 0x35, 0xb9, 0x3b, 0x05, 0x00, 0x00, 0x00,
0x0a,
})
body += "------WebKitFormBoundarySsdgl8Nq9voFyhdO--\n" body += "------WebKitFormBoundarySsdgl8Nq9voFyhdO--\n"
return return
} }
@ -224,8 +237,8 @@ func checkParsedMultipartUpload(t *testing.T, p *parsedPayload) {
if p.UserText != wanted { if p.UserText != wanted {
t.Errorf("User text: got %s, want %s", p.UserText, wanted) t.Errorf("User text: got %s, want %s", p.UserText, wanted)
} }
if len(p.Logs) != 3 { if len(p.Logs) != 4 {
t.Errorf("Log length: got %d, want 3", len(p.Logs)) t.Errorf("Log length: got %d, want 4", len(p.Logs))
} }
if len(p.Data) != 3 { if len(p.Data) != 3 {
t.Errorf("Data length: got %d, want 3", len(p.Data)) t.Errorf("Data length: got %d, want 3", len(p.Data))
@ -249,6 +262,10 @@ func checkParsedMultipartUpload(t *testing.T, p *parsedPayload) {
if p.Logs[2] != wanted { if p.Logs[2] != wanted {
t.Errorf("Log 2: got %s, want %s", p.Logs[2], wanted) t.Errorf("Log 2: got %s, want %s", p.Logs[2], wanted)
} }
wanted = "crash.log.gz"
if p.Logs[3] != wanted {
t.Errorf("Log 3: got %s, want %s", p.Logs[3], wanted)
}
} }
func TestLabels(t *testing.T) { func TestLabels(t *testing.T) {