|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.owasp.esapi.reference.DefaultHTTPUtilities
public class DefaultHTTPUtilities
Reference implementation of the HTTPUtilities interface. This implementation uses the Apache Commons FileUploader library, which in turn uses the Apache Commons IO library.
To simplify the interface, some methods use the current request and response that are tracked by ThreadLocal variables in the Authenticator. This means that you must have called ESAPI.authenticator().setCurrentHTTP(request, response) before calling these methods.
Typically, this is done by calling the Authenticator.login() method, which calls setCurrentHTTP() automatically. However if you want to use these methods in another application, you should explicitly call setCurrentHTTP() in your own code. In either case, you *must* call ESAPI.clearCurrent() to clear threadlocal variables before the thread is reused. The advantages of having identity everywhere outweigh the disadvantages of this approach.
HTTPUtilities
Field Summary |
---|
Fields inherited from interface org.owasp.esapi.HTTPUtilities |
---|
COOKIE, CSRF_TOKEN_NAME, ESAPI_STATE, HEADER, MAX_COOKIE_LEN, MAX_COOKIE_PAIRS, PARAMETER, REMEMBER_TOKEN_COOKIE_NAME |
Constructor Summary | |
---|---|
DefaultHTTPUtilities()
No arg constructor. |
Method Summary | ||
---|---|---|
void |
addCookie(javax.servlet.http.Cookie cookie)
Calls addCookie with the *current* request. This implementation uses a custom "set-cookie" header rather than Java's cookie interface which doesn't allow the use of HttpOnly. |
|
void |
addCookie(javax.servlet.http.HttpServletResponse response,
javax.servlet.http.Cookie cookie)
Add a cookie to the response after ensuring that there are no encoded or illegal characters in the name and name and value. This implementation uses a custom "set-cookie" header rather than Java's cookie interface which doesn't allow the use of HttpOnly. |
|
java.lang.String |
addCSRFToken(java.lang.String href)
Adds the current user's CSRF token (see User.getCSRFToken()) to the URL for purposes of preventing CSRF attacks. |
|
void |
addHeader(javax.servlet.http.HttpServletResponse response,
java.lang.String name,
java.lang.String value)
Add a header to the response after ensuring that there are no encoded or illegal characters in the name and name and value. |
|
void |
addHeader(java.lang.String name,
java.lang.String value)
Calls addHeader with the *current* request. |
|
void |
assertSecureChannel()
Calls assertSecureChannel with the *current* request. |
|
void |
assertSecureChannel(javax.servlet.http.HttpServletRequest request)
Ensures the use of SSL to protect any sensitive parameters in the request and any sensitive data in the response. This implementation ignores the built-in isSecure() method and uses the URL to determine if the request was transmitted over SSL. |
|
void |
assertSecureRequest()
Calls assertSecureRequest with the *current* request. |
|
void |
assertSecureRequest(javax.servlet.http.HttpServletRequest request)
Ensures that the request uses both SSL and POST to protect any sensitive parameters in the querystring from being sniffed, logged, bookmarked, included in referer header, etc... |
|
javax.servlet.http.HttpSession |
changeSessionIdentifier()
Calls changeSessionIdentifier with the *current* request. |
|
javax.servlet.http.HttpSession |
changeSessionIdentifier(javax.servlet.http.HttpServletRequest request)
Invalidate the existing session after copying all of its contents to a newly created session with a new session id. |
|
void |
clearCurrent()
Clears the current HttpRequest and HttpResponse associated with the current thread. |
|
java.lang.String |
decryptHiddenField(java.lang.String encrypted)
Decrypts an encrypted hidden field value and returns the cleartext. |
|
java.util.Map<java.lang.String,java.lang.String> |
decryptQueryString(java.lang.String encrypted)
Takes an encrypted querystring and returns a Map containing the original parameters. |
|
java.util.Map<java.lang.String,java.lang.String> |
decryptStateFromCookie()
Calls decryptStateFromCookie with the *current* request. |
|
java.util.Map<java.lang.String,java.lang.String> |
decryptStateFromCookie(javax.servlet.http.HttpServletRequest request)
Retrieves a map of data from a cookie encrypted with encryptStateInCookie(). |
|
java.lang.String |
encryptHiddenField(java.lang.String value)
Encrypts a hidden field value for use in HTML. |
|
java.lang.String |
encryptQueryString(java.lang.String query)
Takes a querystring (everything after the question mark in the URL) and returns an encrypted string containing the parameters. |
|
void |
encryptStateInCookie(javax.servlet.http.HttpServletResponse response,
java.util.Map<java.lang.String,java.lang.String> cleartext)
Stores a Map of data in an encrypted cookie. |
|
void |
encryptStateInCookie(java.util.Map<java.lang.String,java.lang.String> cleartext)
Calls encryptStateInCookie with the *current* response. |
|
java.lang.String |
getCookie(javax.servlet.http.HttpServletRequest request,
java.lang.String name)
A safer replacement for getCookies() in HttpServletRequest that returns the canonicalized value of the named cookie after "global" validation against the general type defined in ESAPI.properties. |
|
java.lang.String |
getCookie(java.lang.String name)
Calls getCookie with the *current* response. |
|
java.lang.String |
getCSRFToken()
Returns the current user's CSRF token. |
|
javax.servlet.http.HttpServletRequest |
getCurrentRequest()
Retrieves the current HttpServletRequest |
|
javax.servlet.http.HttpServletResponse |
getCurrentResponse()
Retrieves the current HttpServletResponse |
|
java.util.List<java.io.File> |
getFileUploads()
Calls getFileUploads with the *current* request, default upload directory, and default allowed file extensions |
|
java.util.List<java.io.File> |
getFileUploads(javax.servlet.http.HttpServletRequest request)
Call getFileUploads with the specified request, default upload directory, and default allowed file extensions |
|
java.util.List<java.io.File> |
getFileUploads(javax.servlet.http.HttpServletRequest request,
java.io.File finalDir)
Call getFileUploads with the specified request, specified upload directory, and default allowed file extensions |
|
java.util.List<java.io.File> |
getFileUploads(javax.servlet.http.HttpServletRequest request,
java.io.File finalDir,
java.util.List allowedExtensions)
Extract uploaded files from a multipart HTTP requests. |
|
java.lang.String |
getHeader(javax.servlet.http.HttpServletRequest request,
java.lang.String name)
A safer replacement for getHeader() in HttpServletRequest that returns the canonicalized value of the named header after "global" validation against the general type defined in ESAPI.properties. |
|
java.lang.String |
getHeader(java.lang.String name)
Calls getHeader with the *current* request. |
|
static HTTPUtilities |
getInstance()
|
|
java.lang.String |
getParameter(javax.servlet.http.HttpServletRequest request,
java.lang.String name)
A safer replacement for getParameter() in HttpServletRequest that returns the canonicalized value of the named parameter after "global" validation against the general type defined in ESAPI.properties. |
|
java.lang.String |
getParameter(java.lang.String name)
Calls getParameter with the *current* request. |
|
|
getRequestAttribute(javax.servlet.http.HttpServletRequest request,
java.lang.String key)
Gets a typed attribute from the HttpServletRequest associated
with the passed in request. |
|
|
getRequestAttribute(java.lang.String key)
Gets a typed attribute from the HttpServletRequest associated
with the caller thread. |
|
|
getSessionAttribute(javax.servlet.http.HttpSession session,
java.lang.String key)
Gets a typed attribute from the passed in session. |
|
|
getSessionAttribute(java.lang.String key)
Gets a typed attribute from the session associated with the calling thread. |
|
void |
killAllCookies()
Calls killAllCookies with the *current* request and response. |
|
void |
killAllCookies(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Kill all cookies received in the last request from the browser. |
|
void |
killCookie(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
java.lang.String name)
Kills the specified cookie by setting a new cookie that expires immediately. |
|
void |
killCookie(java.lang.String name)
Calls killCookie with the *current* request and response. |
|
void |
logHTTPRequest()
Calls logHTTPRequest with the *current* request and logger. |
|
void |
logHTTPRequest(javax.servlet.http.HttpServletRequest request,
Logger logger)
Format the Source IP address, URL, URL parameters, and all form parameters into a string suitable for the log file. |
|
void |
logHTTPRequest(javax.servlet.http.HttpServletRequest request,
Logger logger,
java.util.List parameterNamesToObfuscate)
Formats an HTTP request into a log suitable string. |
|
void |
sendForward(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
java.lang.String location)
This method performs a forward to any resource located inside the WEB-INF directory. This implementation simply checks to make sure that the forward location starts with "WEB-INF" and is intended for use in frameworks that forward to JSP files inside the WEB-INF folder. |
|
void |
sendForward(java.lang.String location)
Calls sendForward with the *current* request and response. |
|
void |
sendRedirect(javax.servlet.http.HttpServletResponse response,
java.lang.String location)
This method performs a forward to any resource located inside the WEB-INF directory. This implementation checks against the list of safe redirect locations defined in ESAPI.properties. |
|
void |
sendRedirect(java.lang.String location)
Calls sendRedirect with the *current* response. |
|
void |
setContentType()
Calls setContentType with the *current* request and response. |
|
void |
setContentType(javax.servlet.http.HttpServletResponse response)
Set the content type character encoding header on every HttpServletResponse in order to limit the ways in which the input data can be represented. |
|
void |
setCurrentHTTP(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Stores the current HttpRequest and HttpResponse so that they may be readily accessed throughout ESAPI (and elsewhere) |
|
void |
setHeader(javax.servlet.http.HttpServletResponse response,
java.lang.String name,
java.lang.String value)
Add a header to the response after ensuring that there are no encoded or illegal characters in the name and value. |
|
void |
setHeader(java.lang.String name,
java.lang.String value)
Calls setHeader with the *current* response. |
|
void |
setNoCacheHeaders()
Calls setNoCacheHeaders with the *current* response. |
|
void |
setNoCacheHeaders(javax.servlet.http.HttpServletResponse response)
Set headers to protect sensitive information against being cached in the browser. |
|
java.lang.String |
setRememberToken(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
java.lang.String password,
int maxAge,
java.lang.String domain,
java.lang.String path)
Set a cookie containing the current User's remember me token for automatic authentication. Save the user's remember me data in an encrypted cookie and send it to the user. |
|
java.lang.String |
setRememberToken(java.lang.String password,
int maxAge,
java.lang.String domain,
java.lang.String path)
Calls setNoCacheHeaders with the *current* response. |
|
void |
verifyCSRFToken()
Calls verifyCSRFToken with the *current* request. |
|
void |
verifyCSRFToken(javax.servlet.http.HttpServletRequest request)
Checks the CSRF token in the URL (see User.getCSRFToken()) against the user's CSRF token and throws an IntrusionException if it is missing. This implementation uses the CSRF_TOKEN_NAME parameter for the token. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DefaultHTTPUtilities()
Method Detail |
---|
public static HTTPUtilities getInstance()
public void addCookie(javax.servlet.http.Cookie cookie)
addCookie
in interface HTTPUtilities
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void addCookie(javax.servlet.http.HttpServletResponse response, javax.servlet.http.Cookie cookie)
addCookie
in interface HTTPUtilities
public java.lang.String addCSRFToken(java.lang.String href)
addCSRFToken
in interface HTTPUtilities
href
- the URL to which the CSRF token will be appended
public void addHeader(java.lang.String name, java.lang.String value)
addHeader
in interface HTTPUtilities
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void addHeader(javax.servlet.http.HttpServletResponse response, java.lang.String name, java.lang.String value)
addHeader
in interface HTTPUtilities
public void assertSecureChannel() throws AccessControlException
assertSecureChannel
in interface HTTPUtilities
AccessControlException
HTTPUtilities#assertSecureChannel(HttpServletRequest)}
,
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void assertSecureChannel(javax.servlet.http.HttpServletRequest request) throws AccessControlException
assertSecureChannel
in interface HTTPUtilities
AccessControlException
- if security constraints are not metpublic void assertSecureRequest() throws AccessControlException
assertSecureRequest
in interface HTTPUtilities
AccessControlException
HTTPUtilities#assertSecureRequest(HttpServletRequest)}
,
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void assertSecureRequest(javax.servlet.http.HttpServletRequest request) throws AccessControlException
assertSecureRequest
in interface HTTPUtilities
AccessControlException
- if security constraints are not metpublic javax.servlet.http.HttpSession changeSessionIdentifier() throws AuthenticationException
changeSessionIdentifier
in interface HTTPUtilities
AuthenticationException
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public javax.servlet.http.HttpSession changeSessionIdentifier(javax.servlet.http.HttpServletRequest request) throws AuthenticationException
changeSessionIdentifier
in interface HTTPUtilities
AuthenticationException
- the exceptionpublic void clearCurrent()
clearCurrent
in interface HTTPUtilities
ESAPI.clearCurrent()
public java.lang.String decryptHiddenField(java.lang.String encrypted)
decryptHiddenField
in interface HTTPUtilities
encrypted
- hidden field value to decrypt
public java.util.Map<java.lang.String,java.lang.String> decryptQueryString(java.lang.String encrypted) throws EncryptionException
decryptQueryString
in interface HTTPUtilities
encrypted
- the encrypted querystring to decrypt
EncryptionException
public java.util.Map<java.lang.String,java.lang.String> decryptStateFromCookie() throws EncryptionException
decryptStateFromCookie
in interface HTTPUtilities
EncryptionException
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public java.util.Map<java.lang.String,java.lang.String> decryptStateFromCookie(javax.servlet.http.HttpServletRequest request) throws EncryptionException
decryptStateFromCookie
in interface HTTPUtilities
request
-
EncryptionException
public java.lang.String encryptHiddenField(java.lang.String value) throws EncryptionException
encryptHiddenField
in interface HTTPUtilities
value
- the cleartext value of the hidden field
EncryptionException
public java.lang.String encryptQueryString(java.lang.String query) throws EncryptionException
encryptQueryString
in interface HTTPUtilities
query
- the querystring to encrypt
EncryptionException
public void encryptStateInCookie(javax.servlet.http.HttpServletResponse response, java.util.Map<java.lang.String,java.lang.String> cleartext) throws EncryptionException
encryptStateInCookie
in interface HTTPUtilities
EncryptionException
public void encryptStateInCookie(java.util.Map<java.lang.String,java.lang.String> cleartext) throws EncryptionException
encryptStateInCookie
in interface HTTPUtilities
EncryptionException
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public java.lang.String getCookie(javax.servlet.http.HttpServletRequest request, java.lang.String name) throws ValidationException
getCookie
in interface HTTPUtilities
ValidationException
public java.lang.String getCookie(java.lang.String name) throws ValidationException
getCookie
in interface HTTPUtilities
ValidationException
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public java.lang.String getCSRFToken()
getCSRFToken
in interface HTTPUtilities
public javax.servlet.http.HttpServletRequest getCurrentRequest()
getCurrentRequest
in interface HTTPUtilities
public javax.servlet.http.HttpServletResponse getCurrentResponse()
getCurrentResponse
in interface HTTPUtilities
public java.util.List<java.io.File> getFileUploads() throws ValidationException
getFileUploads
in interface HTTPUtilities
ValidationException
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public java.util.List<java.io.File> getFileUploads(javax.servlet.http.HttpServletRequest request) throws ValidationException
getFileUploads
in interface HTTPUtilities
ValidationException
public java.util.List<java.io.File> getFileUploads(javax.servlet.http.HttpServletRequest request, java.io.File finalDir) throws ValidationException
getFileUploads
in interface HTTPUtilities
ValidationException
public java.util.List<java.io.File> getFileUploads(javax.servlet.http.HttpServletRequest request, java.io.File finalDir, java.util.List allowedExtensions) throws ValidationException
HTTPUtilities.getCurrentRequest()
to obtain the HttpServletRequest
object
getFileUploads
in interface HTTPUtilities
ValidationException
- if the file fails validationpublic java.lang.String getHeader(javax.servlet.http.HttpServletRequest request, java.lang.String name) throws ValidationException
getHeader
in interface HTTPUtilities
ValidationException
public java.lang.String getHeader(java.lang.String name) throws ValidationException
getHeader
in interface HTTPUtilities
ValidationException
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public java.lang.String getParameter(javax.servlet.http.HttpServletRequest request, java.lang.String name) throws ValidationException
getParameter
in interface HTTPUtilities
ValidationException
public java.lang.String getParameter(java.lang.String name) throws ValidationException
getParameter
in interface HTTPUtilities
ValidationException
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void killAllCookies()
killAllCookies
in interface HTTPUtilities
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void killAllCookies(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
killAllCookies
in interface HTTPUtilities
request
- response
- public void killCookie(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String name)
killCookie
in interface HTTPUtilities
request
- response
- name
- public void killCookie(java.lang.String name)
killCookie
in interface HTTPUtilities
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void logHTTPRequest()
logHTTPRequest
in interface HTTPUtilities
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void logHTTPRequest(javax.servlet.http.HttpServletRequest request, Logger logger)
logHTTPRequest
in interface HTTPUtilities
logger
- the logger to write the request topublic void logHTTPRequest(javax.servlet.http.HttpServletRequest request, Logger logger, java.util.List parameterNamesToObfuscate)
logHTTPRequest
in interface HTTPUtilities
request
- logger
- the logger to write the request toparameterNamesToObfuscate
- the sensitive parameterspublic void sendForward(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String location) throws AccessControlException, javax.servlet.ServletException, java.io.IOException
sendForward
in interface HTTPUtilities
location
- the URL to forward to, including parameters
AccessControlException
javax.servlet.ServletException
java.io.IOException
public void sendForward(java.lang.String location) throws AccessControlException, javax.servlet.ServletException, java.io.IOException
sendForward
in interface HTTPUtilities
AccessControlException
javax.servlet.ServletException
java.io.IOException
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void sendRedirect(javax.servlet.http.HttpServletResponse response, java.lang.String location) throws AccessControlException, java.io.IOException
sendRedirect
in interface HTTPUtilities
response
- location
- the URL to forward to, including parameters
AccessControlException
java.io.IOException
public void sendRedirect(java.lang.String location) throws AccessControlException, java.io.IOException
sendRedirect
in interface HTTPUtilities
AccessControlException
java.io.IOException
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void setContentType()
setContentType
in interface HTTPUtilities
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void setContentType(javax.servlet.http.HttpServletResponse response)
setContentType
in interface HTTPUtilities
response
- The servlet response to set the content type for.public void setCurrentHTTP(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
setCurrentHTTP
in interface HTTPUtilities
request
- the current requestresponse
- the current responsepublic void setHeader(javax.servlet.http.HttpServletResponse response, java.lang.String name, java.lang.String value)
setHeader
in interface HTTPUtilities
public void setHeader(java.lang.String name, java.lang.String value)
setHeader
in interface HTTPUtilities
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void setNoCacheHeaders()
setNoCacheHeaders
in interface HTTPUtilities
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void setNoCacheHeaders(javax.servlet.http.HttpServletResponse response)
Cache-Control: no-storeNote that the header "pragma: no-cache" is intended only for use in HTTP requests, not HTTP responses. However, Microsoft has chosen to directly violate the standards, so we need to include that header here. For more information, please refer to the relevant standards:
Cache-Control: no-cache
Cache-Control: must-revalidate
Expires: -1
setNoCacheHeaders
in interface HTTPUtilities
response
- public java.lang.String setRememberToken(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String password, int maxAge, java.lang.String domain, java.lang.String path)
setRememberToken
in interface HTTPUtilities
request
- response
- password
- the user's passwordmaxAge
- the length of time that the token should be valid for in relative secondsdomain
- the domain to restrict the token to or nullpath
- the path to restrict the token to or null
public java.lang.String setRememberToken(java.lang.String password, int maxAge, java.lang.String domain, java.lang.String path)
setRememberToken
in interface HTTPUtilities
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void verifyCSRFToken() throws IntrusionException
verifyCSRFToken
in interface HTTPUtilities
IntrusionException
HTTPUtilities#setCurrentHTTP(HttpServletRequest, HttpServletResponse)}
public void verifyCSRFToken(javax.servlet.http.HttpServletRequest request) throws IntrusionException
verifyCSRFToken
in interface HTTPUtilities
request
-
IntrusionException
- if CSRF token is missing or incorrectpublic <T> T getSessionAttribute(java.lang.String key)
getSessionAttribute
in interface HTTPUtilities
T
- The implied type of object expected.key
- The key that references the session attribute
HTTPUtilities.getSessionAttribute(javax.servlet.http.HttpSession, String)
public <T> T getSessionAttribute(javax.servlet.http.HttpSession session, java.lang.String key)
HttpSession
associated with the current
thread.
getSessionAttribute
in interface HTTPUtilities
T
- The implied type of object expectedsession
- The session to retrieve the attribute fromkey
- The key that references the requested object
public <T> T getRequestAttribute(java.lang.String key)
HttpServletRequest
associated
with the caller thread. If the attribute on the request is not of the implied
type, a ClassCastException will be thrown back to the caller.
getRequestAttribute
in interface HTTPUtilities
T
- The implied type of the object expectedkey
- The key that references the request attribute.
public <T> T getRequestAttribute(javax.servlet.http.HttpServletRequest request, java.lang.String key)
HttpServletRequest
associated
with the passed in request. If the attribute on the request is not of the implied
type, a ClassCastException will be thrown back to the caller.
getRequestAttribute
in interface HTTPUtilities
T
- The implied type of the object expectedrequest
- The request to retrieve the attribute fromkey
- The key that references the request attribute.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |