|
| 1 | +package io.jooby; |
| 2 | + |
| 3 | +import io.jooby.exception.InvalidCsrfToken; |
| 4 | + |
| 5 | +import javax.annotation.Nonnull; |
| 6 | +import java.util.Objects; |
| 7 | +import java.util.UUID; |
| 8 | +import java.util.function.Function; |
| 9 | +import java.util.function.Predicate; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +/** |
| 13 | + * <h1>Cross Site Request Forgery handler</h1> |
| 14 | + * |
| 15 | + * <pre> |
| 16 | + * { |
| 17 | + * before(new CsrfHandler()); |
| 18 | + * } |
| 19 | + * </pre> |
| 20 | + * |
| 21 | + * <p> |
| 22 | + * This filter require a token on <code>POST</code>, <code>PUT</code>, <code>PATCH</code> and |
| 23 | + * <code>DELETE</code> requests. A custom policy might be provided via: |
| 24 | + * {@link #setRequestFilter(Predicate)}. |
| 25 | + * </p> |
| 26 | + * |
| 27 | + * <p> |
| 28 | + * Default token generator, use a {@link UUID#randomUUID()}. A custom token generator might be |
| 29 | + * provided via: {@link #setTokenGenerator(Function)}. |
| 30 | + * </p> |
| 31 | + * |
| 32 | + * <p> |
| 33 | + * Default token name is: <code>csrf</code>. If you want to use a different name, just pass the name |
| 34 | + * to the {@link #CsrfHandler(String)} constructor. |
| 35 | + * </p> |
| 36 | + * |
| 37 | + * <h2>Token verification</h2> |
| 38 | + * <p> |
| 39 | + * The {@link CsrfHandler} handler will read an existing token from {@link Session} (or created a |
| 40 | + * new one is necessary) and make available as a request local variable via: |
| 41 | + * {@link Context#attribute(String, Object)}. |
| 42 | + * </p> |
| 43 | + * |
| 44 | + * <p> |
| 45 | + * If the incoming request require a token verification, it will extract the token from: |
| 46 | + * </p> |
| 47 | + * <ol> |
| 48 | + * <li>HTTP header</li> |
| 49 | + * <li>HTTP cookie</li> |
| 50 | + * <li>HTTP parameter (query or form)</li> |
| 51 | + * </ol> |
| 52 | + * |
| 53 | + * <p> |
| 54 | + * If the extracted token doesn't match the existing token (from {@link Session}) a <code>403</code> |
| 55 | + * will be thrown. |
| 56 | + * </p> |
| 57 | + * |
| 58 | + * @author edgar |
| 59 | + * @since 2.5.2 |
| 60 | + */ |
| 61 | +public class CsrfHandler implements Route.Before { |
| 62 | + |
| 63 | + /** |
| 64 | + * Default request filter. Requires an existing session and only check for POST, DELETE, PUT and |
| 65 | + * PATCH methods. |
| 66 | + */ |
| 67 | + public static final Predicate<Context> DEFAULT_FILTER = ctx -> { |
| 68 | + return Router.POST.equals(ctx.getMethod()) |
| 69 | + || Router.DELETE.equals(ctx.getMethod()) |
| 70 | + || Router.PATCH.equals(ctx.getMethod()) |
| 71 | + || Router.PUT.equals(ctx.getMethod()); |
| 72 | + }; |
| 73 | + |
| 74 | + /** |
| 75 | + * UUID token generator. |
| 76 | + */ |
| 77 | + public static final Function<Context, String> DEFAULT_GENERATOR = ctx -> UUID.randomUUID() |
| 78 | + .toString(); |
| 79 | + |
| 80 | + private String name; |
| 81 | + |
| 82 | + private Function<Context, String> generator = DEFAULT_GENERATOR; |
| 83 | + |
| 84 | + private Predicate<Context> filter = DEFAULT_FILTER; |
| 85 | + |
| 86 | + /** |
| 87 | + * Creates a new {@link CsrfHandler} handler and use the given name to save the token in the |
| 88 | + * {@link Session} and or extract the token from incoming requests. |
| 89 | + * |
| 90 | + * @param name Token's name. |
| 91 | + */ |
| 92 | + public CsrfHandler(String name) { |
| 93 | + this.name = name; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Creates a new {@link CsrfHandler} handler and use the given name to save the token in the |
| 98 | + * {@link Session} and or extract the token from incoming requests. |
| 99 | + */ |
| 100 | + public CsrfHandler() { |
| 101 | + this("csrf"); |
| 102 | + } |
| 103 | + |
| 104 | + @Override public void apply(@Nonnull Context ctx) throws Exception { |
| 105 | + |
| 106 | + Session session = ctx.session(); |
| 107 | + String token = session.get(name).toOptional().orElseGet(() -> { |
| 108 | + String newToken = generator.apply(ctx); |
| 109 | + session.put(name, newToken); |
| 110 | + return newToken; |
| 111 | + }); |
| 112 | + |
| 113 | + ctx.attribute(name, token); |
| 114 | + |
| 115 | + if (filter.test(ctx)) { |
| 116 | + String clientToken = Stream.of( |
| 117 | + ctx.header(name).valueOrNull(), |
| 118 | + ctx.cookie(name).valueOrNull(), |
| 119 | + ctx.form(name).valueOrNull(), |
| 120 | + ctx.query(name).valueOrNull() |
| 121 | + ).filter(Objects::nonNull) |
| 122 | + .findFirst() |
| 123 | + .orElse(null); |
| 124 | + if (!token.equals(clientToken)) { |
| 125 | + throw new InvalidCsrfToken(clientToken); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Set a custom token generator. Default generator use: {@link UUID#randomUUID()}. |
| 132 | + * |
| 133 | + * @param generator A custom token generator. |
| 134 | + * @return This filter. |
| 135 | + */ |
| 136 | + public @Nonnull CsrfHandler setTokenGenerator(@Nonnull Function<Context, String> generator) { |
| 137 | + this.generator = generator; |
| 138 | + return this; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Decided whenever or not an incoming request require token verification. Default predicate |
| 143 | + * requires verification on: <code>POST</code>, <code>PUT</code>, <code>PATCH</code> and |
| 144 | + * <code>DELETE</code> requests. |
| 145 | + * |
| 146 | + * @param filter Predicate to use. |
| 147 | + * @return This filter. |
| 148 | + */ |
| 149 | + public @Nonnull CsrfHandler setRequestFilter(@Nonnull Predicate<Context> filter) { |
| 150 | + this.filter = filter; |
| 151 | + return this; |
| 152 | + } |
| 153 | +} |
0 commit comments