Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

For example, an application's strategy for avoiding Cross Site Scripting cross-site scripting (XSS) vulnerabilities may include forbidding <script> tags in inputs. Such black-listing blacklisting mechanisms are a useful part of a security strategy, even though they are insufficient for complete input validation and sanitization. When implemented, this form of validation must be performed only after normalizing the input.

Wiki Markup
Character information in Java 1.6 is based on the Unicode Standard, version 4.0 \[[Unicode 2003|AA. Bibliography#Unicode 2003]\]. Character information in Java 1.6 is based on the Unicode Standard, version 6.0.0 \[[Unicode 2011|AA. Bibliography#Unicode 2011]\].  

Wiki Markup
According to the Unicode Standard \[[Davis 2008|AA. Bibliography#Davis 08]\], annex #15, Unicode Normalization Forms,

When implementations keep strings in a normalized form, they can be assured that equivalent strings have a unique binary representation.

Normalization Forms KC and KD must not be blindly applied to arbitrary text. Because they erase many formatting distinctions, they will prevent round-trip conversion to and from many legacy character sets, and unless supplanted by formatting markup, they may remove distinctions that are important to the semantics of the text. It is best to think of these Normalization Forms as being like uppercase or lowercase mappings: useful in certain contexts for identifying core meanings, but also performing modifications to the text that may not always be appropriate. They can be applied more freely to domains with restricted character sets ...

Frequently, the most suitable normalization form for performing input validation on arbitrarily - encoded strings is KC (NFKC) , because normalizing to KC transforms the input into an equivalent canonical form that can be safely compared with the required input form.

...

This noncompliant code example attempts to validate the String before performing normalization. Consequently, the validation logic fails to detect inputs that should be rejected , because the check for angle brackets fails to detect alternative Unicode representations.

Code Block
bgColor#FFcccc
// String s may be user controllable
// \uFE64 is normalized to < and \uFE65 is normalized to > using NFKC
String s = "\uFE64" + "script" + "\uFE65"; 

// Validate
Pattern pattern = Pattern.compile("[<>]"); // Check for angle brackets
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
  // Found black listed tag
  throw new IllegalStateException();
} else {
  // ... 
}

// Normalize
s = Normalizer.normalize(s, Form.NFKC); 

The normalize method transforms Unicode text into an equivalent composed or decomposed form, allowing for easier searching of text. The normalize method supports the standard normalization forms described in Unicode Standard Annex #15 — Unicode Normalization Forms.

...

Code Block
bgColor#ccccff
String s = "\uFE64" + "script" + "\uFE65";

// normalize
s = Normalizer.normalize(s, Form.NFKC); 

//validate
Pattern pattern = Pattern.compile("[<>]"); 
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
  // Found black listed tag
  throw new IllegalStateException();
} else {
  // ... 
}

Risk Assessment

Validating input before normalization affords attackers the opportunity to bypass filters and other security mechanisms. This can result in the execution of arbitrary code.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="29e04062a5dd3652-9b2ea0d1-424d4b9f-be319216-9c39701051dc4714d565ca28"><ac:plain-text-body><![CDATA[

[[MITRE 2009

AA. Bibliography#MITRE 09]]

[CWE ID -289

http://cwe.mitre.org/data/definitions/289.html] "Authentication Bypass by Alternate Name"

]]></ac:plain-text-body></ac:structured-macro>

 

CWE ID -180 "Incorrect Behavior Order: Validate Before Canonicalize"

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="473134bd74f460e8-3fe27f4f-47804f11-bc5ba403-f9c8f6d467110c020bc502f1"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6831464c773db05c-1081fcbd-4c30413d-b757b48b-ba9e2c64b9ef1733dc82c103"><ac:plain-text-body><![CDATA[

[[Davis 2008

AA. Bibliography#Davis 08]]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e6467946ad4f9e9c-e11e40d8-415b47f0-bec299db-2fa1e64d12af759a0ef008d2"><ac:plain-text-body><![CDATA[

[[Weber 2009

AA. Bibliography#Weber 09]]

]]></ac:plain-text-body></ac:structured-macro>

...