Files
deploy/util/io/files/secure_mkdir_linux.go
dependabot[bot]GitHubdependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>Michael Crenshaw
d8d1e744a9 chore(deps): bump github.com/cyphar/filepath-securejoin from 0.3.6 to 0.4.0 (#21484)
* chore(deps): bump github.com/cyphar/filepath-securejoin

Bumps [github.com/cyphar/filepath-securejoin](https://github.com/cyphar/filepath-securejoin) from 0.3.6 to 0.4.0.
- [Release notes](https://github.com/cyphar/filepath-securejoin/releases)
- [Changelog](https://github.com/cyphar/filepath-securejoin/blob/main/CHANGELOG.md)
- [Commits](https://github.com/cyphar/filepath-securejoin/compare/v0.3.6...v0.4.0)

---
updated-dependencies:
- dependency-name: github.com/cyphar/filepath-securejoin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* match new signature

Signed-off-by: Michael Crenshaw <[email protected]>

---------

Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: Michael Crenshaw <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Michael Crenshaw <[email protected]>
2025-01-14 10:41:32 -05:00

26 lines
843 B
Go

//go:build linux
package files
import (
"fmt"
"os"
securejoin "github.com/cyphar/filepath-securejoin"
)
// SecureMkdirAll creates a directory with the given mode and returns the full path to the directory. It prevents
// directory traversal attacks by ensuring the path is within the root directory. The path is constructed as if the
// given root is the root of the filesystem. So anything traversing outside the root is simply removed from the path.
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
}