From cf1ba6588a5f8639c7a6e0f3e39fcf5170e355ea Mon Sep 17 00:00:00 2001 From: Jakub Date: Tue, 1 Jun 2021 09:35:20 +0200 Subject: [PATCH] GODT-949: Fix section parsing issue --- .gitlab-ci.yml | 71 ------------------------------------- go.mod | 2 +- pkg/message/build_rfc822.go | 4 +-- pkg/message/section.go | 16 ++++++--- pkg/message/section_test.go | 11 ++---- pkg/pmapi/messages.go | 2 +- 6 files changed, 19 insertions(+), 87 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7d6846ac..ac11b450 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -187,77 +187,6 @@ build-ie-darwin-qa: paths: - ie_*.tgz -.build-windows-base: - extends: .build-base - services: - - docker:dind - variables: - DOCKER_HOST: tcp://docker:2375 - -build-windows: - extends: .build-windows-base - script: - # We need to install docker because qtdeploy builds for windows inside a docker container. - # Docker will connect to the dockerd daemon provided by the runner service docker:dind at tcp://docker:2375. - - curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh - - apt-get update && apt-get -y install binutils-mingw-w64 tar gzip - - ln -s /usr/bin/x86_64-w64-mingw32-windres /usr/bin/windres - - go mod download - - TARGET_OS=windows make build - artifacts: - name: "bridge-windows-$CI_COMMIT_SHORT_SHA" - paths: - - bridge_*.tgz - -build-windows-qa: - extends: .build-windows-base - only: - - web - script: - # We need to install docker because qtdeploy builds for windows inside a docker container. - # Docker will connect to the dockerd daemon provided by the runner service docker:dind at tcp://docker:2375. - - curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh - - apt-get update && apt-get -y install binutils-mingw-w64 tar gzip - - ln -s /usr/bin/x86_64-w64-mingw32-windres /usr/bin/windres - - go mod download - - TARGET_OS=windows BUILD_TAGS="build_qa" make build - artifacts: - name: "bridge-windows-qa-$CI_COMMIT_SHORT_SHA" - paths: - - bridge_*.tgz - -build-ie-windows: - extends: .build-windows-base - script: - # We need to install docker because qtdeploy builds for windows inside a docker container. - # Docker will connect to the dockerd daemon provided by the runner service docker:dind at tcp://docker:2375. - - curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh - - apt-get update && apt-get -y install binutils-mingw-w64 tar gzip - - ln -s /usr/bin/x86_64-w64-mingw32-windres /usr/bin/windres - - go mod download - - TARGET_OS=windows make build-ie - artifacts: - name: "ie-windows-$CI_COMMIT_SHORT_SHA" - paths: - - ie_*.tgz - -build-ie-windows-qa: - extends: .build-windows-base - only: - - web - script: - # We need to install docker because qtdeploy builds for windows inside a docker container. - # Docker will connect to the dockerd daemon provided by the runner service docker:dind at tcp://docker:2375. - - curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh - - apt-get update && apt-get -y install binutils-mingw-w64 tar gzip - - ln -s /usr/bin/x86_64-w64-mingw32-windres /usr/bin/windres - - go mod download - - TARGET_OS=windows BUILD_TAGS="build_qa" make build-ie - artifacts: - name: "ie-windows-qa-$CI_COMMIT_SHORT_SHA" - paths: - - ie_*.tgz - # Stage: MIRROR mirror-repo: diff --git a/go.mod b/go.mod index 0f042e17..fd0fb525 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/ProtonMail/proton-bridge -go 1.13 +go 1.15 // These dependencies are `replace`d below, so the version numbers should be ignored. // They are in a separate require block to highlight this. diff --git a/pkg/message/build_rfc822.go b/pkg/message/build_rfc822.go index 7c368129..36366ca9 100644 --- a/pkg/message/build_rfc822.go +++ b/pkg/message/build_rfc822.go @@ -226,7 +226,7 @@ func buildPGPRFC822(kr *crypto.KeyRing, msg *pmapi.Message, opts JobOptions) ([] sigs, err := msg.ExtractSignatures(kr) if err != nil { - return nil, err + log.WithError(err).WithField("id", msg.ID).Warn("Extract signature failed") } if len(sigs) > 0 { @@ -277,7 +277,7 @@ func buildPGPMIMEFallbackRFC822(msg *pmapi.Message, opts JobOptions) ([]byte, er return buf.Bytes(), nil } -func writeMultipartSignedRFC822(header message.Header, body []byte, sig pmapi.Signature) ([]byte, error) { +func writeMultipartSignedRFC822(header message.Header, body []byte, sig pmapi.Signature) ([]byte, error) { //nolint[funlen] buf := new(bytes.Buffer) header.SetContentType("multipart/signed", map[string]string{ diff --git a/pkg/message/section.go b/pkg/message/section.go index 2e99927b..23b592c5 100644 --- a/pkg/message/section.go +++ b/pkg/message/section.go @@ -104,7 +104,7 @@ func (bs *BodyStructure) parseAllChildSections(r io.Reader, currentPath []int, s // If multipart, call getAllParts, else read to count lines. if (strings.HasPrefix(mediaType, "multipart/") || mediaType == "message/rfc822") && params["boundary"] != "" { - newPath := append(currentPath, 1) + nextPath := getChildPath(currentPath) var br *boundaryReader br, err = newBoundaryReader(bodyReader, params["boundary"]) @@ -121,9 +121,9 @@ func (bs *BodyStructure) parseAllChildSections(r io.Reader, currentPath []int, s if err != nil { break } - err = bs.parseAllChildSections(part, newPath, start) + err = bs.parseAllChildSections(part, nextPath, start) part.Reset() - newPath[len(newPath)-1]++ + nextPath[len(nextPath)-1]++ } br.reader = nil @@ -152,7 +152,7 @@ func (bs *BodyStructure) parseAllChildSections(r io.Reader, currentPath []int, s (*bs)[path] = info // Fix start of subsections. - newPath := append(currentPath, 1) + newPath := getChildPath(currentPath) shift := info.Size - info.BSize subInfo, err := bs.getInfo(newPath) @@ -197,6 +197,14 @@ func (bs *BodyStructure) parseAllChildSections(r io.Reader, currentPath []int, s return nil } +// getChildPath will return the first child path of parent path. +// NOTE: Return value can be used to iterate over parts so it is necessary to +// copy parrent values in order to not rewrite values in parent. +func getChildPath(parent []int) []int { + // append alloc inline is the fasted way to copy + return append(append(make([]int, 0, len(parent)+1), parent...), 1) +} + func stringPathFromInts(ints []int) (ret string) { for i, n := range ints { if i != 0 { diff --git a/pkg/message/section_test.go b/pkg/message/section_test.go index 1a1e4f26..b9ba8a39 100644 --- a/pkg/message/section_test.go +++ b/pkg/message/section_test.go @@ -98,17 +98,12 @@ func TestParseBodyStructurePGP(t *testing.T) { bs, err := NewBodyStructure(bytes.NewReader(b)) require.NoError(t, err) - paths := []string{} + haveStructure := map[string]string{} for path := range *bs { - paths = append(paths, path) - } - sort.Strings(paths) - - for _, path := range paths { - require.Equal(t, expectedStructure[path], (*bs)[path].Header.Get("Content-Type")) + haveStructure[path] = (*bs)[path].Header.Get("Content-Type") } - require.True(t, len(*bs) == len(expectedStructure), "Wrong number of sections expected %d but have %d", len(expectedStructure), len(*bs)) + require.Equal(t, expectedStructure, haveStructure) } func TestGetSection(t *testing.T) { diff --git a/pkg/pmapi/messages.go b/pkg/pmapi/messages.go index 94776ad1..7f89959c 100644 --- a/pkg/pmapi/messages.go +++ b/pkg/pmapi/messages.go @@ -326,7 +326,7 @@ func (m *Message) ExtractSignatures(kr *crypto.KeyRing) ([]Signature, error) { return nil, nil } - var signatures []Signature + signatures := make([]Signature, 0, len(msg.UnverifiedSignatures)) for _, signature := range msg.UnverifiedSignatures { buf := new(bytes.Buffer)