HTML からすべての入力タグのコンテンツをフィルター処理したいと考えています。私は AntiSamy フィルターを使用しています。現在、フィルターは (入力値のみではなく) 完全な html コンテンツを除外しています。
ここで提供される実装を使用しています:
doFilter メソッド内で、このコードを使用してコンテンツをスキャンしています
HttpServletResponseInvocationHandler invocationHandler = httpResponseInvocationHandlerFactory.build((HttpServletResponse) response);
CleanResults cleanResults = antiSamy.scan(invocationHandler.getContents(), policy);
私が欲しいのは約です:
CleanResults cleanResults = antiSamy.scan(request.getParameter("input"), policy);
入力フィールドのコンテンツのみがフィルタリングされるようにします。
doFilter メソッド全体は次のとおりです。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (response instanceof HttpServletResponse) {
HttpServletResponseInvocationHandler invocationHandler = httpResponseInvocationHandlerFactory.build((HttpServletResponse) response);
HttpServletResponse proxiedResponse = httpResponseProxyFactory.build(invocationHandler);
chain.doFilter(request, proxiedResponse);
if ("text/html;charset=UTF-8".equals(proxiedResponse.getContentType())) {
try {
Policy policy = policyFileLoader.load(policyFile);
antiSamy.setInputEncoding(inputEncoding);
antiSamy.setOutputEncoding(outputEncoding);
CleanResults cleanResults = antiSamy.scan(invocationHandler.getContents(), policy);
log.info("Number of Errors: " + cleanResults.getNumberOfErrors());
if (log.isDebugEnabled()) {
log.debug("Errors found: ");
List errors = cleanResults.getErrorMessages();
for (int i = 0; i < errors.size(); i++) {
log.debug("\t" + (i + 1) + ". " + errors.get(i));
}
}
log.info("Scan time (in seconds): " + cleanResults.getScanTime());
response.getOutputStream().write(cleanResults.getCleanHTML().getBytes());
} catch (ScanException e) {
log.error(GENERIC_ERROR, e);
} catch (PolicyException e) {
log.error(GENERIC_ERROR, e);
}
} else {
response.getOutputStream().write(invocationHandler.getBytes());
}
} else {
chain.doFilter(request, response);
}
}
AntiSamy がデフォルトで提供するすべてのポリシー ファイルで試しました。
これについて何か助けを得ることができれば素晴らしいことです。