public override void Bad(HttpRequest req, HttpResponse resp) { string data; data = ""; /* Initialize data */ /* Read data from a database */ { try { /* setup the connection */ using (SqlConnection connection = IO.GetDBConnection()) { connection.Open(); /* prepare and execute a (hardcoded) query */ using (SqlCommand command = new SqlCommand(null, connection)) { command.CommandText = "select name from users where id=0"; command.Prepare(); using (SqlDataReader dr = command.ExecuteReader()) { /* POTENTIAL FLAW: Read data from a database query SqlDataReader */ data = dr.GetString(1); } } } } catch (SqlException exceptSql) { IO.Logger.Log(NLog.LogLevel.Warn, exceptSql, "Error with SQL statement"); } } /* serialize data to a byte array */ byte[] dataSerialized = null; try { BinaryFormatter bf = new BinaryFormatter(); using (var ms = new MemoryStream()) { bf.Serialize(ms, data); dataSerialized = ms.ToArray(); } CWE80_XSS__Web_Database_75b.BadSink(dataSerialized, req, resp); } catch (SerializationException exceptSerialize) { IO.Logger.Log(NLog.LogLevel.Warn, "Serialization exception in serialization", exceptSerialize); } }
/* goodG2B() - use goodsource and badsink */ private static void GoodG2B(HttpRequest req, HttpResponse resp) { string data; /* FIX: Use a hardcoded string */ data = "foo"; /* serialize data to a byte array */ byte[] dataSerialized = null; try { BinaryFormatter bf = new BinaryFormatter(); using (var ms = new MemoryStream()) { bf.Serialize(ms, data); dataSerialized = ms.ToArray(); } CWE80_XSS__Web_Database_75b.GoodG2BSink(dataSerialized, req, resp); } catch (SerializationException exceptSerialize) { IO.Logger.Log(NLog.LogLevel.Warn, "Serialization exception in serialization", exceptSerialize); } }