require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
module Owasp
module Esapi
describe Encoder do
# Setup some encoders
let (:encoder) { Owasp::Esapi.encoder }
let (:jsencoder) {Owasp::Esapi::Encoder.new([Owasp::Esapi::Codec::JavascriptCodec.new])}
let (:cssencoder) {Owasp::Esapi::Encoder.new([Owasp::Esapi::Codec::CssCodec.new])}
# HTML and Percent Codec tests
# Generate dynamic canonicalization tests
{
"%25F"=> "%F",
"%3c"=> "<",
"%3C"=> "<",
"%X1"=> "%X1",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=>"<",
"<"=>"<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"<"=> "<",
"&Lt"=> "<",
"&lT"=> "<",
"<"=> "<",
"<"=> "<",
"≪"=> "<",
"&lT;"=> "<",
"%"=> "%",
"%"=> "%",
"%b"=> "%b",
"%3Cscript%3Ealert%28%22hello%22%29%3B%3C%2Fscript%3E"=> "",
"%3Cscript>alert%28%22hello"%29%3B%3C%2Fscript%3E"=> "",
}.each_pair do |k,v|
it "should canonicalize #{k} to #{v}" do
begin
encoder.canonicalize(k.dup).should == v
rescue IntrustionException =>e
# if IDSis on we would throw an intrustion exception, other exceptions are real errors
end
end
end
# Javascript dynamic canonicilzation tests
{
"\\0"=> "\0",
"\\b"=> "\b",
"\\t"=> "\t",
"\\n"=> "\n",
"\\v"=> "\v",
"\\f"=> "\f",
"\\r"=> "\r",
"\\'"=> "\'",
"\\\""=> "\"",
"\\\\"=> "\\",
"\\<"=> "<",
}.each_pair do |k,v|
it "should canonicalize javascript #{k} to #{v}" do
begin
jsencoder.canonicalize(k.dup).should == v
rescue IntrustionException =>e
# if IDSis on we would throw an intrustion exception, other exceptions are real errors
end
end
end
# CSS dynamic canonicalization tests
{
"\\3c"=> "<",
"\\03c"=> "<",
"\\003c"=> "<",
"\\0003c"=> "<",
"\\00003c"=> "<",
"\\3C"=> "<",
"\\03C"=> "<",
"\\003C"=> "<",
"\\0003C"=> "<",
"\\00003C"=> "<",
}.each_pair do |k,v|
it "should canonicalize CSS #{k} to #{v}" do
begin
cssencoder.canonicalize(k.dup).should == v
rescue IntrustionException =>e
# if IDSis on we would throw an intrustion exception, other exceptions are real errors
end
end
end
# Sanitize
it "should sanitize input exceptions" do
# test null value
encoder.canonicalize(nil).should == nil
# test exception paths
encoder.sanitize("%25",true).should == '%'
encoder.sanitize("%25",false).should == '%'
end
# Dynamic double canonicalization tests
{
"<"=> "<",# double entity
"%255c"=> "\\", # double percent
"%2525"=> "%" , #double percent
"%26lt%3b"=> "<", #double percent
"%253c"=> "<",
"%26lt%3b"=> "<",
"%26"=> "&",
"%%33%63"=> "<",
"%%33c"=> "<",
"%3%63"=> "<",
"<"=> "<",
"&%6ct;"=> "<",
"%3c"=> "<",
"%25 %2526 %26#X3c;script> %3Cscript%25252525253e"=> "% &