Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] Fix potential resource leak in postman source #2606

Merged
merged 1 commit into from Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 41 additions & 28 deletions pkg/sources/postman/postman.go
Expand Up @@ -155,37 +155,11 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, _ .
// check if zip file
workspace := Workspace{}
if strings.HasSuffix(workspacePath, ".zip") {
r, err := zip.OpenReader(workspacePath)
var err error
workspace, err = unpackWorkspace(workspacePath)
if err != nil {
return err
}
for _, file := range r.File {
rc, err := file.Open()
if err != nil {
return err
}
contents, err := io.ReadAll(rc)
rc.Close()
if err != nil {
return err
}
if strings.Contains(file.Name, "collection") {
// read in the collection then scan it
c := Collection{}
if err = json.Unmarshal(contents, &c); err != nil {
return err
}
workspace.CollectionsRaw = append(workspace.CollectionsRaw, c)
}
if strings.Contains(file.Name, "environment") {
e := VariableData{}
if err = json.Unmarshal(contents, &e); err != nil {
return err
}
workspace.EnvironmentsRaw = append(workspace.EnvironmentsRaw, e)
}
}
r.Close()
}
basename := path.Base(workspacePath)
workspace.ID = strings.TrimSuffix(basename, filepath.Ext(basename))
Expand Down Expand Up @@ -662,6 +636,45 @@ func (s *Source) scanData(ctx context.Context, chunksChan chan *sources.Chunk, d
}
}

// unpackWorkspace unzips the provided zip file and scans the inflated files
// for collections and environments. It populates the CollectionsRaw and
// EnvironmentsRaw fields of the Workspace object.
func unpackWorkspace(workspacePath string) (Workspace, error) {
var workspace Workspace
r, err := zip.OpenReader(workspacePath)
if err != nil {
return workspace, err
}
defer r.Close()
for _, file := range r.File {
rc, err := file.Open()
if err != nil {
return workspace, err
}
contents, err := io.ReadAll(rc)
rc.Close()
if err != nil {
return workspace, err
}
if strings.Contains(file.Name, "collection") {
// read in the collection then scan it
var c Collection
if err = json.Unmarshal(contents, &c); err != nil {
return workspace, err
}
workspace.CollectionsRaw = append(workspace.CollectionsRaw, c)
}
if strings.Contains(file.Name, "environment") {
var e VariableData
if err = json.Unmarshal(contents, &e); err != nil {
return workspace, err
}
workspace.EnvironmentsRaw = append(workspace.EnvironmentsRaw, e)
}
}
return workspace, nil
}

func shouldSkip(uuid string, include []string, exclude []string) bool {
if slices.Contains(exclude, uuid) {
return true
Expand Down