001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.web.filter.authz;
020
021import javax.servlet.ServletRequest;
022import javax.servlet.ServletResponse;
023import javax.servlet.http.HttpServletResponse;
024
025/**
026 * Filter which requires a request to be over SSL.  Access is allowed if the request is received on the configured
027 * server {@link #setPort(int) port} <em>and</em> the
028 * {@code request.}{@link javax.servlet.ServletRequest#isSecure() isSecure()}.  If either condition is {@code false},
029 * the filter chain will not continue.
030 * <p/>
031 * The {@link #getPort() port} property defaults to {@code 443} and also additionally guarantees that the
032 * request scheme is always 'https' (except for port 80, which retains the 'http' scheme).
033 * <p/>
034 * In addition, the filter allows enabling HTTP Strict Transport Security (HSTS).
035 * This feature is opt-in and disabled by default. If enabled HSTS
036 * will prevent <b>any</b> communications from being sent over HTTP to the
037 * specified domain and will instead send all communications over HTTPS.
038 * </p>
039 * The {@link #getMaxAge() maxAge} property defaults {@code 31536000}, and
040 * {@link #isIncludeSubDomains includeSubDomains} is {@code false}.
041 * </p>
042 * <b>Warning:</b> Use this setting with care and only if you plan to enable
043 * SSL on every path.
044 * </p>
045 * Example configs:
046 * <pre>
047 * [urls]
048 * /secure/path/** = ssl
049 * </pre>
050 * with HSTS enabled
051 * <pre>
052 * [main]
053 * ssl.hsts.enabled = true
054 * [urls]
055 * /** = ssl
056 * </pre>
057 *
058 * @see <a href="https://tools.ietf.org/html/rfc6797">HTTP Strict Transport Security (HSTS)</a>
059 * @since 1.0
060 */
061@SuppressWarnings("checkstyle:JavadocVariable")
062public class SslFilter extends PortFilter {
063
064    public static final int DEFAULT_HTTPS_PORT = 443;
065    public static final String HTTPS_SCHEME = "https";
066
067    private HSTS hsts;
068
069    public SslFilter() {
070        setPort(DEFAULT_HTTPS_PORT);
071        this.hsts = new HSTS();
072    }
073
074    public HSTS getHsts() {
075        return hsts;
076    }
077
078    public void setHsts(HSTS hsts) {
079        this.hsts = hsts;
080    }
081
082    @Override
083    protected String getScheme(String requestScheme, int port) {
084        if (port == DEFAULT_HTTP_PORT) {
085            return PortFilter.HTTP_SCHEME;
086        } else {
087            return HTTPS_SCHEME;
088        }
089    }
090
091    /**
092     * Retains the parent method's port-matching behavior but additionally guarantees that the
093     * {@code ServletRequest.}{@link javax.servlet.ServletRequest#isSecure() isSecure()}.  If the port does not match or
094     * the request is not secure, access is denied.
095     *
096     * @param request     the incoming {@code ServletRequest}
097     * @param response    the outgoing {@code ServletResponse} - ignored in this implementation
098     * @param mappedValue the filter-specific config value mapped to this filter in the URL rules mappings
099     *                    - ignored by this implementation.
100     * @return {@code true} if the request is received on an expected SSL port and the
101     * {@code request.}{@link javax.servlet.ServletRequest#isSecure() isSecure()}, {@code false} otherwise.
102     * @throws Exception if the call to {@code super.isAccessAllowed} throws an exception.
103     * @since 1.2
104     */
105    @Override
106    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
107        return super.isAccessAllowed(request, response, mappedValue) && request.isSecure();
108    }
109
110    /**
111     * If HTTP Strict Transport Security (HSTS) is enabled the HTTP header
112     * will be written, otherwise this method does nothing.
113     *
114     * @param request  the incoming {@code ServletRequest}
115     * @param response the outgoing {@code ServletResponse}
116     */
117    @SuppressWarnings("checkstyle:MagicNumber")
118    @Override
119    protected void postHandle(ServletRequest request, ServletResponse response) {
120        if (hsts.isEnabled()) {
121            StringBuilder directives = new StringBuilder(64)
122                    .append("max-age=").append(hsts.getMaxAge());
123
124            if (hsts.isIncludeSubDomains()) {
125                directives.append("; includeSubDomains");
126            }
127
128            HttpServletResponse resp = (HttpServletResponse) response;
129            resp.addHeader(HSTS.HTTP_HEADER, directives.toString());
130        }
131    }
132
133    /**
134     * Helper class for HTTP Strict Transport Security (HSTS)
135     */
136    public class HSTS {
137
138        public static final String HTTP_HEADER = "Strict-Transport-Security";
139
140        public static final boolean DEFAULT_ENABLED = false;
141        // approx. one year in seconds
142        public static final int DEFAULT_MAX_AGE = 31536000;
143        public static final boolean DEFAULT_INCLUDE_SUB_DOMAINS = false;
144
145        private boolean enabled;
146        private int maxAge;
147        private boolean includeSubDomains;
148
149        public HSTS() {
150            this.enabled = DEFAULT_ENABLED;
151            this.maxAge = DEFAULT_MAX_AGE;
152            this.includeSubDomains = DEFAULT_INCLUDE_SUB_DOMAINS;
153        }
154
155        public boolean isEnabled() {
156            return enabled;
157        }
158
159        public void setEnabled(boolean enabled) {
160            this.enabled = enabled;
161        }
162
163        public int getMaxAge() {
164            return maxAge;
165        }
166
167        public void setMaxAge(int maxAge) {
168            this.maxAge = maxAge;
169        }
170
171        public boolean isIncludeSubDomains() {
172            return includeSubDomains;
173        }
174
175        public void setIncludeSubDomains(boolean includeSubDomains) {
176            this.includeSubDomains = includeSubDomains;
177        }
178    }
179}