22import java .net .URI ;
33import java .net .http .HttpClient ;
44import java .net .http .HttpRequest ;
5- import java .util .regex .Pattern ;
65import java .util .regex .Matcher ;
6+ import java .util .regex .Pattern ;
7+ import java .util .List ;
78
89import javax .servlet .ServletException ;
910import javax .servlet .http .HttpServlet ;
@@ -147,6 +148,32 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
147148 HttpRequest r13 = HttpRequest .newBuilder (new URI (param13 )).build ();
148149 client .send (r13 , null );
149150 }
151+
152+ // GOOD: sanitisation by @Pattern annotation on a field
153+ AnnotatedFieldObject obj14 = new AnnotatedFieldObject (request .getParameter ("uri14" ));
154+ HttpRequest r14a = HttpRequest .newBuilder (new URI (obj14 .uri )).build ();
155+ client .send (r14a , null );
156+ HttpRequest r14b = HttpRequest .newBuilder (new URI (obj14 .getUri ())).build ();
157+ client .send (r14b , null );
158+
159+ // GOOD: sanitisation by @Pattern annotation on a parameter of a constructor
160+ AnnotatedParameterObject obj15 = new AnnotatedParameterObject (request .getParameter ("uri15" ));
161+ HttpRequest r15a = HttpRequest .newBuilder (new URI (obj15 .uri )).build ();
162+ client .send (r15a , null );
163+ HttpRequest r15b = HttpRequest .newBuilder (new URI (obj15 .getUri ())).build ();
164+ client .send (r15b , null );
165+
166+ // GOOD: sanitisation by @Pattern annotation on a parameter of a method
167+ HttpRequest r16 = HttpRequest .newBuilder (new URI (identity1 (request .getParameter ("uri16" )))).build ();
168+ client .send (r16 , null );
169+
170+ // GOOD: sanitisation by @Pattern annotation on a method (which constrains the return value)
171+ HttpRequest r17 = HttpRequest .newBuilder (new URI (identity2 (request .getParameter ("uri17" )))).build ();
172+ client .send (r17 , null );
173+
174+ // GOOD: sanitisation by @Pattern annotation on a type (we do not recognise this, so we get an FP)
175+ HttpRequest r18 = HttpRequest .newBuilder (new URI (getFromList (List .of (request .getParameter ("uri18" ))))).build (); // $ SPURIOUS: Source Alert
176+ client .send (r18 , null ); // $ SPURIOUS: Alert
150177 } catch (Exception e ) {
151178 // TODO: handle exception
152179 }
@@ -157,4 +184,44 @@ private void validate(String s) {
157184 throw new IllegalArgumentException ("Invalid ID" );
158185 }
159186 }
187+
188+ public String identity1 (@ javax .validation .constraints .Pattern (regexp = "[a-zA-Z0-9_-]+" ) String uri ) {
189+ return uri ;
190+ }
191+
192+ @ javax .validation .constraints .Pattern (regexp = "[a-zA-Z0-9_-]+" )
193+ public String identity2 (String uri ) {
194+ return uri ;
195+ }
196+
197+ public String getFromList (List <@ javax .validation .constraints .Pattern (regexp = "[a-zA-Z0-9_-]+" ) String > list ) {
198+ return list .get (0 );
199+ }
200+
201+ public class AnnotatedFieldObject {
202+ @ javax .validation .constraints .Pattern (regexp = "[a-zA-Z0-9_-]+" )
203+ String uri ;
204+
205+ String otherField ;
206+
207+ public AnnotatedFieldObject (String uri ) {
208+ this .uri = uri ;
209+ }
210+
211+ public String getUri () {
212+ return uri ;
213+ }
214+ }
215+
216+ public class AnnotatedParameterObject {
217+ String uri ;
218+
219+ public AnnotatedParameterObject (@ javax .validation .constraints .Pattern (regexp = "[a-zA-Z0-9_-]+" ) String uri ) {
220+ this .uri = uri ;
221+ }
222+
223+ public String getUri () {
224+ return uri ;
225+ }
226+ }
160227}
0 commit comments