A practical example sanitizing user input by removing script tags and normalizing excess whitespace in a single pass.
ID: 1145
See: Transform(string)
using uCalcSoftware;
var uc = new uCalc();
using (var t = new uCalc.Transformer()) {
// Rule 1: Remove script tags and their content (case-insensitive, multi-line)
t.FromTo("", "");
t.DefaultRuleSet.SetCaseSensitive(false).SetStatementSensitive(false);
// Rule 2: Normalize one or more whitespace characters to a single space
t.FromTo("{@Whitespace:ws}", " ");
string userInput = " Welcome! Please enjoy. ";
// Transform the input in one go and print the result
Console.WriteLine($"Sanitized: '{t.Transform(userInput)}'");
}
Sanitized: ' Welcome! Please enjoy. ' using uCalcSoftware; var uc = new uCalc(); using (var t = new uCalc.Transformer()) { // Rule 1: Remove script tags and their content (case-insensitive, multi-line) t.FromTo("<script>{content}</script>", ""); t.DefaultRuleSet.SetCaseSensitive(false).SetStatementSensitive(false); // Rule 2: Normalize one or more whitespace characters to a single space t.FromTo("{@Whitespace:ws}", " "); string userInput = " Welcome! <SCRIPT>alert('bad');</SCRIPT>Please enjoy. "; // Transform the input in one go and print the result Console.WriteLine($"Sanitized: '{t.Transform(userInput)}'"); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
{
uCalc::Transformer t;
t.Owned(); // Causes t to be released when it goes out of scope
// Rule 1: Remove script tags and their content (case-insensitive, multi-line)
t.FromTo("", "");
t.DefaultRuleSet().SetCaseSensitive(false).SetStatementSensitive(false);
// Rule 2: Normalize one or more whitespace characters to a single space
t.FromTo("{@Whitespace:ws}", " ");
string userInput = " Welcome! Please enjoy. ";
// Transform the input in one go and print the result
cout << "Sanitized: '" << t.Transform(userInput) << "'" << endl;
}
}
Sanitized: ' Welcome! Please enjoy. ' #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; { uCalc::Transformer t; t.Owned(); // Causes t to be released when it goes out of scope // Rule 1: Remove script tags and their content (case-insensitive, multi-line) t.FromTo("<script>{content}</script>", ""); t.DefaultRuleSet().SetCaseSensitive(false).SetStatementSensitive(false); // Rule 2: Normalize one or more whitespace characters to a single space t.FromTo("{@Whitespace:ws}", " "); string userInput = " Welcome! <SCRIPT>alert('bad');</SCRIPT>Please enjoy. "; // Transform the input in one go and print the result cout << "Sanitized: '" << t.Transform(userInput) << "'" << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Using t As New uCalc.Transformer()
'// Rule 1: Remove script tags and their content (case-insensitive, multi-line)
t.FromTo("", "")
t.DefaultRuleSet.SetCaseSensitive(false).SetStatementSensitive(false)
'// Rule 2: Normalize one or more whitespace characters to a single space
t.FromTo("{@Whitespace:ws}", " ")
Dim userInput As String = " Welcome! Please enjoy. "
'// Transform the input in one go and print the result
Console.WriteLine($"Sanitized: '{t.Transform(userInput)}'")
End Using
End Sub
End Module
Sanitized: ' Welcome! Please enjoy. ' Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Using t As New uCalc.Transformer() '// Rule 1: Remove script tags and their content (case-insensitive, multi-line) t.FromTo("<script>{content}</script>", "") t.DefaultRuleSet.SetCaseSensitive(false).SetStatementSensitive(false) '// Rule 2: Normalize one or more whitespace characters to a single space t.FromTo("{@Whitespace:ws}", " ") Dim userInput As String = " Welcome! <SCRIPT>alert('bad');</SCRIPT>Please enjoy. " '// Transform the input in one go and print the result Console.WriteLine($"Sanitized: '{t.Transform(userInput)}'") End Using End Sub End Module