@@ -12,6 +12,7 @@ import (
12
12
"io"
13
13
"os"
14
14
"path/filepath"
15
+ "runtime"
15
16
"strings"
16
17
"testing"
17
18
@@ -33,7 +34,7 @@ func TestLinkUpdateChecksum(t *testing.T) {
33
34
require .NoError (t , copyDir (testDataSrc , filepath .Join (tempDir , "testdata" )))
34
35
35
36
// Set up paths within the temporary directory
36
- basePath := filepath .Join (tempDir , "testdata/ links" )
37
+ basePath := filepath .Join (tempDir , "testdata" , " links" )
37
38
38
39
// Create an os.Root for secure file operations within tempDir
39
40
root , err := os .OpenRoot (tempDir )
@@ -82,7 +83,7 @@ func TestListLinkedFiles(t *testing.T) {
82
83
// Get current working directory to locate test data
83
84
wd , err := os .Getwd ()
84
85
assert .NoError (t , err )
85
- basePath := filepath .Join (wd , filepath . FromSlash ( "testdata/ links" ) )
86
+ basePath := filepath .Join (wd , "testdata" , " links" )
86
87
87
88
// Find the repository root to create a secure os.Root context
88
89
root , err := FindRepositoryRoot ()
@@ -193,7 +194,7 @@ func TestUpdateLinkedFilesChecksums(t *testing.T) {
193
194
require .NoError (t , copyDir (testDataSrc , filepath .Join (tempDir , "testdata" )))
194
195
195
196
// Set up paths within the temporary directory
196
- basePath := filepath .Join (tempDir , "testdata/ links" )
197
+ basePath := filepath .Join (tempDir , "testdata" , " links" )
197
198
198
199
// Create an os.Root for secure file operations within tempDir
199
200
root , err := os .OpenRoot (tempDir )
@@ -227,7 +228,7 @@ func TestLinkedFilesByPackageFrom(t *testing.T) {
227
228
// Get current working directory to locate test data
228
229
wd , err := os .Getwd ()
229
230
assert .NoError (t , err )
230
- basePath := filepath .Join (wd , filepath . FromSlash ( "testdata/ links" ) )
231
+ basePath := filepath .Join (wd , "testdata" , " links" )
231
232
232
233
// Find the repository root to create a secure os.Root context
233
234
root , err := FindRepositoryRoot ()
@@ -269,14 +270,14 @@ func TestIncludeLinkedFiles(t *testing.T) {
269
270
// Get current working directory to locate test data
270
271
wd , err := os .Getwd ()
271
272
assert .NoError (t , err )
272
- testPkg := filepath .Join (wd , filepath . FromSlash ( "testdata" ) )
273
+ testPkg := filepath .Join (wd , "testdata" )
273
274
274
275
// Create a temporary directory and copy test data to avoid modifying originals
275
276
tempDir := t .TempDir ()
276
277
require .NoError (t , copyDir (testPkg , filepath .Join (tempDir , "testdata" )))
277
278
278
279
// Set up source and destination directories
279
- fromDir := filepath .Join (tempDir , "testdata/ testpackage" )
280
+ fromDir := filepath .Join (tempDir , "testdata" , " testpackage" )
280
281
toDir := filepath .Join (tempDir , "dest" )
281
282
282
283
// Create an os.Root for secure file operations within tempDir
@@ -769,6 +770,11 @@ func TestLinksFS_ErrorConditions(t *testing.T) {
769
770
err = os .WriteFile (invalidLinkFile , []byte ("" ), 0644 )
770
771
require .NoError (t , err )
771
772
773
+ // Create link that escapes root
774
+ outOfRootLinkFile := filepath .Join (workDir , "escapesroot.txt.link" )
775
+ err = os .WriteFile (outOfRootLinkFile , []byte ("../../etc/passwd" ), 0644 )
776
+ require .NoError (t , err )
777
+
772
778
// Setup LinksFS
773
779
root , err := os .OpenRoot (repoDir )
774
780
require .NoError (t , err )
@@ -777,6 +783,11 @@ func TestLinksFS_ErrorConditions(t *testing.T) {
777
783
lfs , err := NewLinksFS (root , workDir )
778
784
require .NoError (t , err )
779
785
786
+ notFoundErrorMsg := "no such file or directory"
787
+ if runtime .GOOS == "windows" {
788
+ notFoundErrorMsg = "The system cannot find the file specified"
789
+ }
790
+
780
791
tests := []struct {
781
792
name string
782
793
fileName string
@@ -785,13 +796,18 @@ func TestLinksFS_ErrorConditions(t *testing.T) {
785
796
{
786
797
name : "broken link to non-existent file" ,
787
798
fileName : "broken.txt.link" ,
788
- errorMsg : "escapes the repository root" ,
799
+ errorMsg : notFoundErrorMsg ,
789
800
},
790
801
{
791
802
name : "invalid link file format" ,
792
803
fileName : "invalid.txt.link" ,
793
804
errorMsg : "file is empty or first line is missing" ,
794
805
},
806
+ {
807
+ name : "escapes root" ,
808
+ fileName : "escapesroot.txt.link" ,
809
+ errorMsg : "path escapes from parent" ,
810
+ },
795
811
}
796
812
797
813
for _ , tc := range tests {
@@ -845,13 +861,13 @@ func TestLinksFS_WorkDirValidation(t *testing.T) {
845
861
name : "invalid absolute workDir outside repo" ,
846
862
workDir : outsideDir ,
847
863
expectError : true ,
848
- errorMsg : "is outside the repository root " ,
864
+ errorMsg : "path escapes from parent " ,
849
865
},
850
866
{
851
867
name : "invalid relative workDir escaping repo" ,
852
868
workDir : "../outside" ,
853
869
expectError : true ,
854
- errorMsg : "is outside the repository root " ,
870
+ errorMsg : "path escapes from parent " ,
855
871
},
856
872
}
857
873
0 commit comments