@@ -190,6 +190,24 @@ pub fn extract(
190190 Ok ( Program ( visitor. trap_writer . trap_output ) )
191191}
192192
193+ /// Escapes a string for use in a TRAP key, by replacing special characters with
194+ /// HTML entities.
195+ fn escape_key ( s : & str ) -> String {
196+ let mut escaped = String :: new ( ) ;
197+ for c in s. chars ( ) {
198+ match c {
199+ '&' => escaped. push_str ( "&" ) ,
200+ '{' => escaped. push_str ( "{" ) ,
201+ '}' => escaped. push_str ( "}" ) ,
202+ '"' => escaped. push_str ( """ ) ,
203+ '@' => escaped. push_str ( "@" ) ,
204+ '#' => escaped. push_str ( "#" ) ,
205+ _ => escaped. push ( c) ,
206+ }
207+ }
208+ escaped
209+ }
210+
193211/// Normalizes the path according the common CodeQL specification. Assumes that
194212/// `path` has already been canonicalized using `std::fs::canonicalize`.
195213fn normalize_path ( path : & Path ) -> String {
@@ -230,11 +248,11 @@ fn normalize_path(path: &Path) -> String {
230248}
231249
232250fn full_id_for_file ( path : & Path ) -> String {
233- format ! ( "{};sourcefile" , normalize_path( path) )
251+ format ! ( "{};sourcefile" , escape_key ( & normalize_path( path) ) )
234252}
235253
236254fn full_id_for_folder ( path : & Path ) -> String {
237- format ! ( "{};folder" , normalize_path( path) )
255+ format ! ( "{};folder" , escape_key ( & normalize_path( path) ) )
238256}
239257
240258struct ChildNode {
@@ -731,3 +749,16 @@ fn limit_string_test() {
731749 assert_eq ! ( "hi ☹" , limit_string( & "hi ☹☹" . to_owned( ) , 6 ) ) ;
732750 assert_eq ! ( "hi " , limit_string( & "hi ☹☹" . to_owned( ) , 5 ) ) ;
733751}
752+
753+ #[ test]
754+ fn escape_key_test ( ) {
755+ assert_eq ! ( "foo!" , escape_key( "foo!" ) ) ;
756+ assert_eq ! ( "foo{}" , escape_key( "foo{}" ) ) ;
757+ assert_eq ! ( "{}" , escape_key( "{}" ) ) ;
758+ assert_eq ! ( "" , escape_key( "" ) ) ;
759+ assert_eq ! ( "/path/to/foo.rb" , escape_key( "/path/to/foo.rb" ) ) ;
760+ assert_eq ! (
761+ "/path/to/foo&{}"@#.rb" ,
762+ escape_key( "/path/to/foo&{}\" @#.rb" )
763+ ) ;
764+ }
0 commit comments