Code

collection3: Filter out identifiers with control characters.
[collectd.git] / contrib / collection3 / bin / index.cgi
index be20928c029972fadff8285804865d9ae0b13b82..e28e98572238e04e6e1e9ea52a6a8d565e88b1e2 100755 (executable)
@@ -20,6 +20,7 @@ use warnings;
 use lib ('../lib');
 use utf8;
 
+use Carp (qw(cluck confess));
 use FindBin ('$RealBin');
 use CGI (':cgi');
 use CGI::Carp ('fatalsToBrowser');
@@ -145,7 +146,7 @@ HTML
     <title>collection.cgi, Version 3</title>
     <link rel="icon" href="../share/shortcut-icon.png" type="image/png" />
     <link rel="stylesheet" href="../share/style.css" type="text/css" />
-    <script type="text/javascript" src="../share/navigate.js" />
+    <script type="text/javascript" src="../share/navigate.js"></script>
   </head>
   <body onload="nav_init ($begin, $end);">
 HTML
@@ -160,6 +161,28 @@ sub end_html
 HTML
 }
 
+sub contains_invalid_chars
+{
+  my $str = shift;
+
+  for (split (m//, $str))
+  {
+    my $n = ord ($_);
+
+    # Whitespace is allowed.
+    if (($n >= 9) && ($n <= 13))
+    {
+      next;
+    }
+    elsif ($n < 32)
+    {
+      return (1);
+    }
+  }
+
+  return;
+}
+
 sub show_selector
 {
   my $timespan_selection = get_timespan_selection ();
@@ -174,6 +197,7 @@ sub show_selector
 HTML
   for (sort (keys %$host_selection))
   {
+    next if contains_invalid_chars ($_);
     my $host = encode_entities ($_);
     my $selected = $host_selection->{$_}
     ? ' selected="selected"'
@@ -186,6 +210,7 @@ HTML
 HTML
   for (sort (keys %$plugin_selection))
   {
+    next if contains_invalid_chars ($_);
     my $plugin = encode_entities ($_);
     my $selected = $plugin_selection->{$_}
     ? ' selected="selected"'
@@ -198,6 +223,7 @@ HTML
 HTML
   for (sort { $TimeSpans->{$a} <=> $TimeSpans->{$b} } (keys (%$TimeSpans)))
   {
+    next if contains_invalid_chars ($_);
     my $name = encode_entities ($_);
     my $value = $TimeSpans->{$_};
     my $selected = ($value == $timespan_selection)
@@ -224,6 +250,7 @@ sub action_list_hosts
   for (sort @hosts)
   {
     my $url = encode_entities (script_name () . "?action=show_selection;hostname=$_");
+    next if contains_invalid_chars ($_);
     my $name = encode_entities ($_);
     print qq#      <li><a href="$url">$name</a></li>\n#;
   }
@@ -298,8 +325,8 @@ sub action_show_selection
     my $file = $_;
     my $type = ucfirst (lc ($file->{'type'}));
 
-    $type =~ s/[^A-Za-z_]//g;
-    $type =~ s/_([A-Za-z])/\U$1\E/g;
+    $type =~ s/[^A-Za-z0-9_]//g;
+    $type =~ s/_([A-Za-z0-9])/\U$1\E/g;
 
     if (!defined ($types->{$type}))
     {