|
| 1 | +/** |
| 2 | + * This file is part of the Open Web Application Security Project (OWASP) Java File IO Security project. For details, please see |
| 3 | + * <a href="https://www.owasp.org/index.php/OWASP_Java_File_I_O_Security_Project">https://www.owasp.org/index.php/OWASP_Java_File_I_O_Security_Project</a>. |
| 4 | + * |
| 5 | + * Copyright (c) 2014 - The OWASP Foundation |
| 6 | + * |
| 7 | + * This API is published by OWASP under the Apache 2.0 license. You should read and accept the LICENSE before you use, modify, and/or redistribute this software. |
| 8 | + * |
| 9 | + * @author Jeff Williams <a href="http://www.aspectsecurity.com">Aspect Security</a> - Original ESAPI author |
| 10 | + * @author August Detlefsen <a href="http://www.codemagi.com">CodeMagi</a> - Java File IO Security Project lead |
| 11 | + * @created 2014 |
| 12 | + */ |
| 13 | +package org.owasp.fileio; |
| 14 | + |
| 15 | +import org.owasp.fileio.util.Utils; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Iterator; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Set; |
| 20 | +import org.owasp.fileio.codecs.Codec; |
| 21 | +import org.owasp.fileio.codecs.HTMLEntityCodec; |
| 22 | +import org.owasp.fileio.codecs.PercentCodec; |
| 23 | + |
| 24 | +/** |
| 25 | + * Reference implementation of the Encoder interface. This implementation takes a whitelist approach to encoding, meaning that everything not specifically identified in a list of "immune" characters |
| 26 | + * is encoded. |
| 27 | + */ |
| 28 | +public class Encoder { |
| 29 | + |
| 30 | + private static volatile Encoder singletonInstance; |
| 31 | + public final static char[] CHAR_ALPHANUMERICS = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
| 32 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 33 | + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; |
| 34 | + public static final Set<Character> ALPHANUMERICS; |
| 35 | + |
| 36 | + static { |
| 37 | + ALPHANUMERICS = Utils.arrayToSet(Encoder.CHAR_ALPHANUMERICS); |
| 38 | + } |
| 39 | + private boolean restrictMultiple = true; |
| 40 | + private boolean restrictMixed = true; |
| 41 | + |
| 42 | + public boolean isRestrictMultiple() { |
| 43 | + return restrictMultiple; |
| 44 | + } |
| 45 | + |
| 46 | + public void setRestrictMultiple(boolean restrictMultiple) { |
| 47 | + this.restrictMultiple = restrictMultiple; |
| 48 | + } |
| 49 | + |
| 50 | + public boolean isRestrictMixed() { |
| 51 | + return restrictMixed; |
| 52 | + } |
| 53 | + |
| 54 | + public void setRestrictMixed(boolean restrictMixed) { |
| 55 | + this.restrictMixed = restrictMixed; |
| 56 | + } |
| 57 | + |
| 58 | + public static Encoder getInstance() { |
| 59 | + if (singletonInstance == null) { |
| 60 | + synchronized (Encoder.class) { |
| 61 | + if (singletonInstance |
| 62 | + == null) { |
| 63 | + singletonInstance = new Encoder(); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + return singletonInstance; |
| 68 | + } |
| 69 | + // Codecs |
| 70 | + private List codecs = new ArrayList(); |
| 71 | + private HTMLEntityCodec htmlCodec = new HTMLEntityCodec(); |
| 72 | + private PercentCodec percentCodec = new PercentCodec(); |
| 73 | + |
| 74 | + /** |
| 75 | + * Instantiates a new DefaultEncoder with the default codecs |
| 76 | + */ |
| 77 | + public Encoder() { |
| 78 | + codecs.add(htmlCodec); |
| 79 | + codecs.add(percentCodec); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Instantiates a new DefaultEncoder with the default codecs |
| 84 | + */ |
| 85 | + public Encoder(List<Codec> codecs) { |
| 86 | + this.codecs = codecs; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * {@inheritDoc} |
| 91 | + */ |
| 92 | + public String canonicalize(String input) { |
| 93 | + if (input == null) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + // Issue 231 - These are reverse boolean logic in the Encoder interface, so we need to invert these values - CS |
| 98 | + return canonicalize(input, restrictMultiple, restrictMixed); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * {@inheritDoc} |
| 103 | + */ |
| 104 | + public String canonicalize(String input, boolean strict) { |
| 105 | + return canonicalize(input, strict, strict); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * {@inheritDoc} |
| 110 | + */ |
| 111 | + public String canonicalize(String input, boolean restrictMultiple, boolean restrictMixed) { |
| 112 | + if (input == null) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + String working = input; |
| 117 | + Codec codecFound = null; |
| 118 | + int mixedCount = 1; |
| 119 | + int foundCount = 0; |
| 120 | + boolean clean = false; |
| 121 | + while (!clean) { |
| 122 | + clean = true; |
| 123 | + |
| 124 | + // try each codec and keep track of which ones work |
| 125 | + Iterator i = codecs.iterator(); |
| 126 | + while (i.hasNext()) { |
| 127 | + Codec codec = (Codec) i.next(); |
| 128 | + String old = working; |
| 129 | + working = codec.decode(working); |
| 130 | + if (!old.equals(working)) { |
| 131 | + if (codecFound != null && codecFound != codec) { |
| 132 | + mixedCount++; |
| 133 | + } |
| 134 | + codecFound = codec; |
| 135 | + if (clean) { |
| 136 | + foundCount++; |
| 137 | + } |
| 138 | + clean = false; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // do strict tests and handle if any mixed, multiple, nested encoding were found |
| 144 | + if (foundCount >= 2 && mixedCount > 1) { |
| 145 | + if (restrictMultiple || restrictMixed) { |
| 146 | + //TODO: throw new ValidationException("Input validation failure", "Multiple (" + foundCount + "x) and mixed encoding (" + mixedCount + "x) detected in " + input); |
| 147 | + } else { |
| 148 | + //TODO: logger.warning(Logger.SECURITY_FAILURE, "Multiple (" + foundCount + "x) and mixed encoding (" + mixedCount + "x) detected in " + input); |
| 149 | + } |
| 150 | + } else if (foundCount >= 2) { |
| 151 | + if (restrictMultiple) { |
| 152 | + //TODO: throw new ValidationException("Input validation failure", "Multiple (" + foundCount + "x) encoding detected in " + input); |
| 153 | + } else { |
| 154 | + //TODO: logger.warning(Logger.SECURITY_FAILURE, "Multiple (" + foundCount + "x) encoding detected in " + input); |
| 155 | + } |
| 156 | + } else if (mixedCount > 1) { |
| 157 | + if (restrictMixed) { |
| 158 | + //TODO: throw new ValidationException("Input validation failure", "Mixed encoding (" + mixedCount + "x) detected in " + input); |
| 159 | + } else { |
| 160 | + //TODO: logger.warning(Logger.SECURITY_FAILURE, "Mixed encoding (" + mixedCount + "x) detected in " + input); |
| 161 | + } |
| 162 | + } |
| 163 | + return working; |
| 164 | + } |
| 165 | +} |
0 commit comments