|
| 1 | +package org.ocpsoft.rewrite.showcase.access; |
| 2 | + |
| 3 | +import javax.servlet.ServletContext; |
| 4 | + |
| 5 | +import org.ocpsoft.rewrite.config.Configuration; |
| 6 | +import org.ocpsoft.rewrite.config.ConfigurationBuilder; |
| 7 | +import org.ocpsoft.rewrite.config.Direction; |
| 8 | +import org.ocpsoft.rewrite.context.EvaluationContext; |
| 9 | +import org.ocpsoft.rewrite.event.Rewrite; |
| 10 | +import org.ocpsoft.rewrite.param.Constraint; |
| 11 | +import org.ocpsoft.rewrite.servlet.config.Header; |
| 12 | +import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider; |
| 13 | +import org.ocpsoft.rewrite.servlet.config.SendStatus; |
| 14 | +import org.ocpsoft.rewrite.servlet.config.URL; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
| 18 | + * |
| 19 | + */ |
| 20 | +public class InputValidationRewriteConfiguration extends HttpConfigurationProvider |
| 21 | +{ |
| 22 | + @Override |
| 23 | + public Configuration getConfiguration(final ServletContext context) |
| 24 | + { |
| 25 | + return ConfigurationBuilder.begin() |
| 26 | + |
| 27 | + .addRule() |
| 28 | + .when(Direction.isInbound() |
| 29 | + .and(URL.matches("{badthings}") |
| 30 | + .or(Header.exists("{badthings}")) |
| 31 | + .or(Header.valueExists("{badthings}") |
| 32 | + ) |
| 33 | + ) |
| 34 | + ) |
| 35 | + .perform(SendStatus.error(404)) // or take some protective action |
| 36 | + .where("badthings").constrainedBy(selectedCharacters); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + private Constraint<String> selectedCharacters = new Constraint<String>() { |
| 41 | + @Override |
| 42 | + public boolean isSatisfiedBy(Rewrite event, EvaluationContext context, String value) |
| 43 | + { |
| 44 | + return value.matches(".*[+%'\"^$#\\\\(\\\\)*<>].*"); |
| 45 | + // Don't forget unicode! |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + @Override |
| 50 | + public int priority() |
| 51 | + { |
| 52 | + return Integer.MIN_VALUE; |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments