22
33import static org .assertj .core .api .Assertions .assertThat ;
44
5- import com .google .common .io .Files ;
65import com .hubspot .jinjava .BaseInterpretingTest ;
7- import com .hubspot .jinjava .Jinjava ;
8- import com .hubspot .jinjava .interpret .JinjavaInterpreter ;
96import java .io .File ;
107import java .io .FileNotFoundException ;
118import java .nio .charset .StandardCharsets ;
9+ import java .nio .file .Files ;
1210import org .junit .Before ;
1311import org .junit .Test ;
1412
1513public class FileLocatorTest extends BaseInterpretingTest {
1614
1715 FileLocator locatorWorkingDir ;
1816 FileLocator locatorTmpDir ;
17+ FileLocator locatorConfined ;
1918
2019 File first ;
2120 File second ;
21+ File inside ;
22+ File secretOutside ;
2223
2324 @ Before
2425 public void setUp () throws Exception {
2526 locatorWorkingDir = new FileLocator ();
2627
27- File tmpDir = java .nio .file .Files
28- .createTempDirectory (getClass ().getSimpleName ())
29- .toFile ();
28+ File tmpDir = Files .createTempDirectory (getClass ().getSimpleName ()).toFile ();
3029 locatorTmpDir = new FileLocator (tmpDir );
3130
3231 first = new File (tmpDir , "foo/first.jinja" );
@@ -35,8 +34,21 @@ public void setUp() throws Exception {
3534 first .getParentFile ().mkdirs ();
3635 second .getParentFile ().mkdirs ();
3736
38- Files .write ("first" , first , StandardCharsets .UTF_8 );
39- Files .write ("second" , second , StandardCharsets .UTF_8 );
37+ Files .writeString (first .toPath (), "first" );
38+ Files .writeString (second .toPath (), "second" );
39+
40+ File parent = Files
41+ .createTempDirectory (getClass ().getSimpleName () + "-confined" )
42+ .toFile ();
43+ File confinedDir = new File (parent , "base" );
44+ confinedDir .mkdirs ();
45+ locatorConfined = new FileLocator (confinedDir );
46+
47+ inside = new File (confinedDir , "inside.jinja" );
48+ Files .writeString (inside .toPath (), "inside" );
49+
50+ secretOutside = new File (parent , "secret.txt" );
51+ Files .writeString (secretOutside .toPath (), "TOP_SECRET_FROM_OUTSIDE_DIR" );
4052 }
4153
4254 @ Test
@@ -102,4 +114,38 @@ public void testNotFoundRel() throws Exception {
102114 public void testNotFoundAbs () throws Exception {
103115 locatorWorkingDir .getString ("/blargh" , StandardCharsets .UTF_8 , interpreter );
104116 }
117+
118+ @ Test
119+ public void itAllowsInRootRelativePath () throws Exception {
120+ assertThat (
121+ locatorConfined .getString ("inside.jinja" , StandardCharsets .UTF_8 , interpreter )
122+ )
123+ .isEqualTo ("inside" );
124+ }
125+
126+ @ Test
127+ public void itAllowsInRootAbsolutePath () throws Exception {
128+ assertThat (
129+ locatorConfined .getString (
130+ inside .getAbsolutePath (),
131+ StandardCharsets .UTF_8 ,
132+ interpreter
133+ )
134+ )
135+ .isEqualTo ("inside" );
136+ }
137+
138+ @ Test (expected = ResourceNotFoundException .class )
139+ public void itBlocksDotDotTraversalOutsideRoot () throws Exception {
140+ locatorConfined .getString ("../secret.txt" , StandardCharsets .UTF_8 , interpreter );
141+ }
142+
143+ @ Test (expected = ResourceNotFoundException .class )
144+ public void itBlocksAbsolutePathOutsideRoot () throws Exception {
145+ locatorConfined .getString (
146+ secretOutside .getAbsolutePath (),
147+ StandardCharsets .UTF_8 ,
148+ interpreter
149+ );
150+ }
105151}
0 commit comments