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

Fix "uncontrolled data use" #151

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions internal/objectstorage/filestore/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@
}

func (s *Store) Get(ctx context.Context, gitoid string) (io.ReadCloser, error) {
return os.Open(filepath.Join(s.prefix, gitoid+".json"))
if filepath.IsLocal(gitoid) {
return os.Open(filepath.Join(s.prefix, gitoid+".json"))
github-advanced-security[bot] marked this conversation as resolved.
Dismissed
Show resolved Hide resolved
} else {
return nil, filepath.ErrBadPattern
}
}

func (s *Store) Store(ctx context.Context, gitoid string, payload []byte) error {
return os.WriteFile(filepath.Join(s.prefix, gitoid+".json"), payload, 0644)
if filepath.IsLocal(gitoid) {
return os.WriteFile(filepath.Join(s.prefix, gitoid+".json"), payload, 0644)
} else {
return filepath.ErrBadPattern
}
}
95 changes: 95 additions & 0 deletions internal/objectstorage/filestore/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2024 The Archivista Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package filestore_test

import (
"context"
"io"
"os"
"path/filepath"
"testing"

"github.com/in-toto/archivista/internal/objectstorage/filestore"
"github.com/stretchr/testify/suite"
)

// Test Suite: UT FileStoreSuite
type UTFileStoreSuite struct {
suite.Suite
tempDir string
payload []byte
}

func TestUTFileStoreSuite(t *testing.T) {
suite.Run(t, new(UTFileStoreSuite))
}

func (ut *UTFileStoreSuite) SetupTest() {
// Create a temporary directory for testing
tempDir, err := os.MkdirTemp("", "filestore_test")
if err != nil {
ut.FailNow(err.Error())
}
ut.tempDir = tempDir
ut.payload = []byte("test payload")
}

func (ut *UTFileStoreSuite) TearDownTest() {
os.RemoveAll(ut.tempDir)
}
func (ut *UTFileStoreSuite) Test_Get() {

store, _, err := filestore.New(context.Background(), ut.tempDir, "")
if err != nil {
ut.FailNow(err.Error())
}

// Define a test payload
payload := []byte("test payload")

// Store the payload
err = store.Store(context.Background(), "test_gitoid", payload)
if err != nil {
ut.FailNow(err.Error())
}

// Attempt storing at malicious payload location
err = store.Store(context.Background(), "../../test_gitoid", payload)
if err != nil && err != filepath.ErrBadPattern {
ut.FailNowf("Failed to detect bad path: %v", err.Error())
}

// Retrieve the payload
reader, err := store.Get(context.Background(), "test_gitoid")
if err != nil {
ut.FailNowf("Failed to retrieve payload: %v", err.Error())
}
defer reader.Close()

// Read the payload from the reader
retrievedPayload, err := io.ReadAll(reader)
if err != nil {
ut.FailNowf("Failed to read payload: %v", err.Error())
}

// Compare the retrieved payload with the original payload
ut.Equal(string(retrievedPayload), string(payload))

// Attempt to retrieve non-local payload
_, err = store.Get(context.Background(), "/etc/passwd")
if err != nil && err != filepath.ErrBadPattern {
ut.FailNowf("Failed to detect bad path: %v", err.Error())
}

}
Loading