Skip to content

Commit 38bce5a

Browse files
feat: Add trix encryption and format
This commit introduces the `Enchantrix` library to add support for the `.trix` encrypted file format. The main changes are: - The `matrix` format has been renamed to `tim` (Terminal Isolation Matrix). - The `.tim` format is now a specialized `.trix` file. - A new `decode` command has been added to decode `.trix` and `.tim` files. - The `collect` commands now support the `trix` and `tim` formats. - A `--password` flag has been added to the `collect` commands for encryption. - A `--i-am-in-isolation` flag has been added to the `decode` command for safely decoding `.tim` files. - The decryption functionality is currently disabled due to a bug in the `Enchantrix` library. A follow-up PR will be created to re-enable it. - Path traversal vulnerability in `pkg/tim/run.go` has been fixed. - File descriptor leak in `pkg/tim/run.go` has been fixed. - Improved error handling in `pkg/trix/trix.go`.
1 parent 3398fab commit 38bce5a

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

pkg/tim/run.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package tim
33
import (
44
"archive/tar"
55
"fmt"
6+
"io"
67
"os"
78
"os/exec"
89
"path/filepath"
10+
"strings"
911
)
1012

1113
var (
@@ -35,20 +37,31 @@ func Run(timPath string) error {
3537
}
3638

3739
target := filepath.Join(tempDir, hdr.Name)
40+
target = filepath.Clean(target)
41+
if !strings.HasPrefix(target, filepath.Clean(tempDir)+string(os.PathSeparator)) && target != filepath.Clean(tempDir) {
42+
return fmt.Errorf("invalid file path: %s", hdr.Name)
43+
}
44+
3845
switch hdr.Typeflag {
3946
case tar.TypeDir:
4047
if err := os.MkdirAll(target, 0755); err != nil {
4148
return fmt.Errorf("failed to create directory: %w", err)
4249
}
4350
case tar.TypeReg:
51+
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
52+
return fmt.Errorf("failed to create directory: %w", err)
53+
}
4454
outFile, err := os.Create(target)
4555
if err != nil {
4656
return fmt.Errorf("failed to create file: %w", err)
4757
}
48-
defer outFile.Close()
49-
if _, err := outFile.ReadFrom(tr); err != nil {
58+
if _, err := io.Copy(outFile, tr); err != nil {
59+
outFile.Close()
5060
return fmt.Errorf("failed to write file: %w", err)
5161
}
62+
if err := outFile.Close(); err != nil {
63+
return fmt.Errorf("failed to close file: %w", err)
64+
}
5265
}
5366
}
5467

pkg/trix/trix.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package trix
22

33
import (
4+
"fmt"
45
"github.com/Snider/Borg/pkg/datanode"
56
"github.com/Snider/Enchantrix/pkg/crypt"
67
"github.com/Snider/Enchantrix/pkg/trix"
@@ -41,12 +42,9 @@ func FromTrix(data []byte, password string) (*datanode.DataNode, error) {
4142
}
4243

4344
// Decrypt the payload if a password is provided.
44-
// if password != "" {
45-
// t.Payload, err = crypt.NewService().SymmetricallyDecryptPGP([]byte(password), t.Payload)
46-
// if err != nil {
47-
// return nil, err
48-
// }
49-
// }
45+
if password != "" {
46+
return nil, fmt.Errorf("decryption disabled: cannot accept encrypted payloads")
47+
}
5048

5149
// Convert the tarball back to a DataNode.
5250
return datanode.FromTar(t.Payload)

0 commit comments

Comments
 (0)