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

feat: use MkdirAll by OS(#19301) #19323

Merged
merged 8 commits into from
Aug 4, 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
22 changes: 22 additions & 0 deletions commitserver/commit/default_mkdir_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build !linux

package commit

import (
"fmt"
"os"

securejoin "github.com/cyphar/filepath-securejoin"
)

func SecureMkdirAll(root, unsafePath string, mode os.FileMode) (string, error) {
fullPath, err := securejoin.SecureJoin(root, unsafePath)
if err != nil {
return "", fmt.Errorf("failed to construct secure path: %w", err)
}
err = os.MkdirAll(fullPath, mode)
if err != nil {
return "", fmt.Errorf("failed to create directory: %w", err)
}
return fullPath, nil
}
23 changes: 23 additions & 0 deletions commitserver/commit/default_mkdir_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build !linux

package commit

import (
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSecureMkdirAllDefault(t *testing.T) {
root := t.TempDir()

unsafePath := "test/dir"
fullPath, err := SecureMkdirAll(root, unsafePath, os.ModePerm)
require.NoError(t, err)

expectedPath := path.Join(root, unsafePath)
assert.Equal(t, expectedPath, fullPath)
}
9 changes: 2 additions & 7 deletions commitserver/commit/hydratorhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path"
"text/template"

securejoin "github.com/cyphar/filepath-securejoin"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -30,13 +29,9 @@ func WriteForPaths(rootPath string, repoUrl string, drySha string, paths []*apic
if hydratePath == "." {
hydratePath = ""
}

var fullHydratePath string
fullHydratePath, err = securejoin.SecureJoin(rootPath, hydratePath)
if err != nil {
return fmt.Errorf("failed to construct hydrate path: %w", err)
}
// TODO: consider switching to securejoin.MkdirAll: https://github.com/cyphar/filepath-securejoin?tab=readme-ov-file#mkdirall
err = os.MkdirAll(fullHydratePath, os.ModePerm)
fullHydratePath, err = SecureMkdirAll(rootPath, hydratePath, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create path: %w", err)
}
Expand Down
21 changes: 21 additions & 0 deletions commitserver/commit/linux_mkdir_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build linux

package commit

import (
"fmt"

"github.com/cyphar/filepath-securejoin"
)

func SecureMkdirAll(root, unsafePath string, mode os.FileMode) (string, error) {
err := securejoin.MkdirAll(root, unsafePath, mode)
if err != nil {
return "", fmt.Errorf("failed to make directory: %w", err)
}
fullPath, err := securejoin.SecureJoin(root, unsafePath)
if err != nil {
return "", fmt.Errorf("failed to construct secure path: %w", err)
}
return fullPath, nil
}
21 changes: 21 additions & 0 deletions commitserver/commit/linux_mkdir_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build linux

package commit

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestSecureMkdirAllLinux(t *testing.T) {
root := t.TempDir()

unsafePath := "test/dir"
fullPath, err := SecureMkdirAll(root, unsafePath, os.ModePerm)
require.NoError(t, err)

expectedPath := path.Join(root, unsafePath)
require.Equal(t, expectedPath, fullPath)
}
Loading