security(tools): use crypto/rand for temp file naming
Replace predictable PID+nanosecond temp file names with cryptographically random hex strings to prevent symlink pre-placement attacks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
ec870eaf8a
commit
b391e649d2
1 changed files with 7 additions and 2 deletions
|
|
@ -2,13 +2,14 @@ package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/sipeed/picoclaw/pkg/fileutil"
|
"github.com/sipeed/picoclaw/pkg/fileutil"
|
||||||
)
|
)
|
||||||
|
|
@ -340,7 +341,11 @@ func (r *sandboxFs) WriteFile(path string, data []byte) error {
|
||||||
|
|
||||||
// Use atomic write pattern with explicit sync for flash storage reliability.
|
// Use atomic write pattern with explicit sync for flash storage reliability.
|
||||||
// Using 0o600 (owner read/write only) for secure default permissions.
|
// Using 0o600 (owner read/write only) for secure default permissions.
|
||||||
tmpRelPath := fmt.Sprintf(".tmp-%d-%d", os.Getpid(), time.Now().UnixNano())
|
randBytes := make([]byte, 8)
|
||||||
|
if _, err := rand.Read(randBytes); err != nil {
|
||||||
|
return fmt.Errorf("failed to generate random bytes for temp file: %w", err)
|
||||||
|
}
|
||||||
|
tmpRelPath := fmt.Sprintf(".tmp-%s", hex.EncodeToString(randBytes))
|
||||||
|
|
||||||
tmpFile, err := root.OpenFile(tmpRelPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
tmpFile, err := root.OpenFile(tmpRelPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue