/** Serialize a stylesheet to a String */ public String serialize(CssTree.StyleSheet styleSheet) { StringWriter writer = new StringWriter(); serialize(styleSheet, writer); return writer.toString(); }
/** Serialize a stylesheet to a String */ public String serialize(CssTree.StyleSheet styleSheet) { StringWriter writer = new StringWriter(); serialize(styleSheet, writer); return writer.toString(); }
/** Serialize a stylesheet to a String */ public String serialize(CssTree.StyleSheet styleSheet) { List<String> ip6Uris = new ArrayList<String>(); CajaCssUtils.saveIp6Uris(styleSheet, ip6Uris); StringWriter writer = new StringWriter(); serialize(styleSheet, writer); return CajaCssUtils.restoreIp6Uris(writer.toString(), ip6Uris); }
/** * Sanitize the CSS content of a style tag. * @param styleElem to sanitize * @param linkContext url of containing content * @param gadgetContext The gadget context. * @param importRewriter to rewrite @imports to sanitizing proxy * @param imageRewriter to rewrite images to sanitizing proxy */ public void sanitize(Element styleElem, Uri linkContext, GadgetContext gadgetContext, ProxyUriManager importRewriter, ProxyUriManager imageRewriter) { String content = null; try { CssTree.StyleSheet stylesheet = parser.parseDom(styleElem.getTextContent(), linkContext); sanitize(stylesheet, linkContext, gadgetContext, importRewriter, imageRewriter); // Write the rewritten CSS back into the element content = parser.serialize(stylesheet); } catch (GadgetException ge) { // Failed to parse stylesheet so log and continue LOG.log(Level.INFO, "Failed to parse stylesheet", ge); } if (StringUtils.isEmpty(content)) { // Remove the owning node styleElem.getParentNode().removeChild(styleElem); } else { styleElem.setTextContent(content); } }
/** * Sanitize the CSS content of a style tag. * @param styleElem to sanitize * @param linkContext url of containing content * @param gadgetContext The gadget context. * @param importRewriter to rewrite @imports to sanitizing proxy * @param imageRewriter to rewrite images to sanitizing proxy */ public void sanitize(Element styleElem, Uri linkContext, GadgetContext gadgetContext, ProxyUriManager importRewriter, ProxyUriManager imageRewriter) { String content = null; try { CssTree.StyleSheet stylesheet = parser.parseDom(styleElem.getTextContent(), linkContext); sanitize(stylesheet, linkContext, gadgetContext, importRewriter, imageRewriter); // Write the rewritten CSS back into the element content = parser.serialize(stylesheet); } catch (GadgetException ge) { // Failed to parse stylesheet so log and continue LOG.log(Level.INFO, "Failed to parse stylesheet", ge); } if (StringUtils.isEmpty(content)) { // Remove the owning node styleElem.getParentNode().removeChild(styleElem); } else { styleElem.setTextContent(content); } }
sanitize(stylesheet, linkContext, gadgetContext, importRewriter, imageRewriter); content = parser.serialize(stylesheet); } catch (GadgetException ge) {
/** * Sanitize the CSS content of a style tag. * @param content to sanitize * @param linkContext url of containing content * @param gadgetContext The gadget context. * @param importRewriter to rewrite @imports to sanitizing proxy * @param imageRewriter to rewrite images to sanitizing proxy * @return Sanitized css. */ public String sanitize(String content, Uri linkContext, GadgetContext gadgetContext, ProxyUriManager importRewriter, ProxyUriManager imageRewriter) { try { CssTree.StyleSheet stylesheet = parser.parseDom(content, linkContext); sanitize(stylesheet, linkContext, gadgetContext, importRewriter, imageRewriter); // Write the rewritten CSS back into the element return parser.serialize(stylesheet); } catch (GadgetException ge) { // Failed to parse stylesheet so log and continue LOG.log(Level.INFO, "Failed to parse stylesheet", ge); return ""; } }
/** * Sanitize the CSS content of a style tag. * @param content to sanitize * @param linkContext url of containing content * @param gadgetContext The gadget context. * @param importRewriter to rewrite @imports to sanitizing proxy * @param imageRewriter to rewrite images to sanitizing proxy * @return Sanitized css. */ public String sanitize(String content, Uri linkContext, GadgetContext gadgetContext, ProxyUriManager importRewriter, ProxyUriManager imageRewriter) { try { CssTree.StyleSheet stylesheet = parser.parseDom(content, linkContext); sanitize(stylesheet, linkContext, gadgetContext, importRewriter, imageRewriter); // Write the rewritten CSS back into the element return parser.serialize(stylesheet); } catch (GadgetException ge) { // Failed to parse stylesheet so log and continue LOG.log(Level.INFO, "Failed to parse stylesheet", ge); return ""; } }
/** * Sanitize the CSS content of a style tag. * @param content to sanitize * @param linkContext url of containing content * @param gadgetContext The gadget context. * @param importRewriter to rewrite @imports to sanitizing proxy * @param imageRewriter to rewrite images to sanitizing proxy * @return Sanitized css. */ public String sanitize(String content, Uri linkContext, GadgetContext gadgetContext, ProxyUriManager importRewriter, ProxyUriManager imageRewriter) { try { CssTree.StyleSheet stylesheet = parser.parseDom(content, linkContext); sanitize(stylesheet, linkContext, gadgetContext, importRewriter, imageRewriter); // Write the rewritten CSS back into the element return parser.serialize(stylesheet); } catch (GadgetException ge) { // Failed to parse stylesheet so log and continue if (LOG.isLoggable(Level.INFO)) { LOG.logp(Level.INFO, classname, "sanitize", MessageKeys.FAILED_TO_PARSE); LOG.log(Level.INFO, ge.getMessage(), ge); } return ""; } }
List<String> imports = rewrite(stylesheet, source, uriMaker, extractImports, gadgetContext); String content = cssParser.serialize(stylesheet); if (StringUtils.isEmpty(content) || StringUtils.isWhitespace(content)) {
gadgetContext); cssParser.serialize(stylesheet, writer); return stringList; } catch (GadgetException ge) {
@Test public void testUrlEscapingMockContainer() throws Exception { String css = ".xyz { background: url('http://www.example.org/img.gif');}"; CssTree.StyleSheet styleSheet = parser.parseDom(css); sanitizer.sanitize(styleSheet, DUMMY, gadgetContext, importRewriter, imageRewriter); assertEquals(".xyz{" + "background:url('//www.mock.com/dir/proxy?container=mockContainer&gadget=http%3A%2F%2Fwww.example.org%2Fbase" + "&debug=0&nocache=0&url=http%3A%2F%2Fwww.example.org%2Fimg.gif" + "&sanitize=1&rewriteMime=image%2F%2a');}", parser.serialize(styleSheet).replaceAll("\\s", "")); }
@Test public void testUrlEscapingMockContainer() throws Exception { String css = ".xyz { background: url('http://www.example.org/img.gif');}"; CssTree.StyleSheet styleSheet = parser.parseDom(css); sanitizer.sanitize(styleSheet, DUMMY, gadgetContext, importRewriter, imageRewriter); assertEquals(".xyz{" + "background:url('//www.mock.com/dir/proxy?container=mockContainer&gadget=http%3A%2F%2Fwww.example.org%2Fbase" + "&debug=0&nocache=0&rewriteMime=image%2F%2a&sanitize=1" + "&url=http%3A%2F%2Fwww.example.org%2Fimg.gif');}", parser.serialize(styleSheet).replaceAll("\\s", "")); }
@Test public void testUrlEscapingMockContainer() throws Exception { String css = ".xyz { background: url('http://www.example.org/img.gif');}"; CssTree.StyleSheet styleSheet = parser.parseDom(css); sanitizer.sanitize(styleSheet, DUMMY, gadgetContext, importRewriter, imageRewriter); assertEquals(".xyz{" + "background:url('//www.mock.com/dir/proxy?container=mockContainer&gadget=http%3A%2F%2Fwww.example.org%2Fbase" + "&debug=0&nocache=0&rewriteMime=image%2F%2a&sanitize=1" + "&url=http%3A%2F%2Fwww.example.org%2Fimg.gif');}", parser.serialize(styleSheet).replaceAll("\\s", "")); }
@Test public void testUrlEscapingDefaultContainer() throws Exception { String css = ".xyz { background: url('http://www.example.org/img.gif');}"; CssTree.StyleSheet styleSheet = parser.parseDom(css); GadgetContext gadgetContext = new GadgetContext() { @Override public String getContainer() { return ContainerConfig.DEFAULT_CONTAINER; } }; sanitizer.sanitize(styleSheet, DUMMY, gadgetContext, importRewriter, imageRewriter); assertEquals(".xyz{" + "background:url('//www.test.com/dir/proxy?container=default&gadget=http%3A%2F%2Fwww.example.org%2Fbase" + "&debug=0&nocache=0&rewriteMime=image%2F%2a&sanitize=1" + "&url=http%3A%2F%2Fwww.example.org%2Fimg.gif');}", parser.serialize(styleSheet).replaceAll("\\s", "")); }
@Test public void testUrlEscapingDefaultContainer() throws Exception { String css = ".xyz { background: url('http://www.example.org/img.gif');}"; CssTree.StyleSheet styleSheet = parser.parseDom(css); GadgetContext gadgetContext = new GadgetContext() { @Override public String getContainer() { return ContainerConfig.DEFAULT_CONTAINER; } }; sanitizer.sanitize(styleSheet, DUMMY, gadgetContext, importRewriter, imageRewriter); assertEquals(".xyz{" + "background:url('//www.test.com/dir/proxy?container=default&gadget=http%3A%2F%2Fwww.example.org%2Fbase" + "&debug=0&nocache=0&url=http%3A%2F%2Fwww.example.org%2Fimg.gif" + "&sanitize=1&rewriteMime=image%2F%2a');}", parser.serialize(styleSheet).replaceAll("\\s", "")); }
@Test public void testUrlEscapingDefaultContainer() throws Exception { String css = ".xyz { background: url('http://www.example.org/img.gif');}"; CssTree.StyleSheet styleSheet = parser.parseDom(css); GadgetContext gadgetContext = new GadgetContext() { @Override public String getContainer() { return ContainerConfig.DEFAULT_CONTAINER; } }; sanitizer.sanitize(styleSheet, DUMMY, gadgetContext, importRewriter, imageRewriter); assertEquals(".xyz{" + "background:url('//www.test.com/dir/proxy?container=default&gadget=http%3A%2F%2Fwww.example.org%2Fbase" + "&debug=0&nocache=0&rewriteMime=image%2F%2a&sanitize=1" + "&url=http%3A%2F%2Fwww.example.org%2Fimg.gif');}", parser.serialize(styleSheet).replaceAll("\\s", "")); }