66package com .github .jknack .handlebars .springmvc ;
77
88import static org .junit .jupiter .api .Assertions .*;
9- import static org .mockito .ArgumentMatchers .anyString ;
109import static org .mockito .Mockito .mock ;
1110import static org .mockito .Mockito .when ;
1211
@@ -75,14 +74,15 @@ public void shouldBlockUrlFragmentInjection() throws IOException {
7574 URL maliciousUrl = new URL ("file:///etc/passwd#.hbs" );
7675 when (mockResource .getURL ()).thenReturn (maliciousUrl );
7776
78- // We must mock the exact string that the template loader will ask Spring for
79- when (resourceLoader .getResource (anyString () )).thenReturn (mockResource );
77+ // Mock the exact valid string Handlebars will request after prefix/suffix append
78+ when (resourceLoader .getResource ("classpath:/templates/hack.hbs" )).thenReturn (mockResource );
8079
8180 IllegalArgumentException exception =
8281 assertThrows (
8382 IllegalArgumentException .class ,
8483 () -> {
85- templateLoader .sourceAt ("classpath:/templates/hack#.hbs" );
84+ // Pass a clean string to bypass OS-level Paths.get() restrictions
85+ templateLoader .sourceAt ("hack" );
8686 });
8787
8888 assertTrue (exception .getMessage ().contains ("Template URL must not contain a fragment" ));
@@ -97,13 +97,15 @@ public void shouldBlockUrlQueryInjection() throws IOException {
9797 URL maliciousUrl = new URL ("file:///etc/passwd?.hbs" );
9898 when (mockResource .getURL ()).thenReturn (maliciousUrl );
9999
100- when (resourceLoader .getResource (anyString ())).thenReturn (mockResource );
100+ // Mock the exact valid string Handlebars will request after prefix/suffix append
101+ when (resourceLoader .getResource ("classpath:/templates/hack.hbs" )).thenReturn (mockResource );
101102
102103 IllegalArgumentException exception =
103104 assertThrows (
104105 IllegalArgumentException .class ,
105106 () -> {
106- templateLoader .sourceAt ("classpath:/templates/hack?.hbs" );
107+ // Pass a clean string to bypass OS-level Paths.get() restrictions
108+ templateLoader .sourceAt ("hack" );
107109 });
108110
109111 assertTrue (exception .getMessage ().contains ("Template URL must not contain a query" ));
0 commit comments