/** * 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 ""; } }
@Before public void setUp() throws Exception { cajaCssParser = new CajaCssParser(); cajaCssParser.setCacheProvider(new LruCacheProvider(10)); }
/** * Parse CSS content into Caja's CSS DOM model * * @return A parsed stylesheet */ public CssTree.StyleSheet parseDom(String content) throws GadgetException { // Use a fake source if the real source is unknown return parseDom(content, FAKE_SOURCE); }
private GadgetRewriter createRewriter(Set<String> tags, Set<String> attributes) { Set<String> newTags = new HashSet<String>(tags); newTags.addAll(DEFAULT_TAGS); ContentRewriterFeature.Factory rewriterFeatureFactory = new ContentRewriterFeature.Factory(null, new ContentRewriterFeature.DefaultConfig( ".*", "", "HTTP", "embed,img,script,link,style", false, false)); return new SanitizingGadgetRewriter(newTags, attributes, rewriterFeatureFactory, new CajaCssSanitizer(new CajaCssParser()), new PassthruManager("host.com", "/proxy")); }
/** Serialize a stylesheet to a String */ public String serialize(CssTree.StyleSheet styleSheet) { StringWriter writer = new StringWriter(); serialize(styleSheet, writer); return writer.toString(); }
public CssTree.StyleSheet parseDom(String content, Uri source) throws GadgetException { CssTree.StyleSheet parsedCss = null; boolean shouldCache = shouldCache(); String key = null; if (shouldCache) { // TODO - Consider using the source if its under a certain size key = HashUtil.checksum(content.getBytes()); parsedCss = parsedCssCache.getElement(key); } if (parsedCss == null) { try { parsedCss = parseImpl(content, source); if (shouldCache) { parsedCssCache.addElement(key, parsedCss); } } catch (ParseException pe) { // Bad input; not server's fault. throw new GadgetException(GadgetException.Code.CSS_PARSE_ERROR, pe, HttpResponse.SC_BAD_REQUEST); } } if (shouldCache) { return (CssTree.StyleSheet)parsedCss.clone(); } return parsedCss; }
/** * Parse CSS content into Caja's CSS DOM model * * @return A parsed stylesheet */ public CssTree.StyleSheet parseDom(String content) throws GadgetException { // Use a fake source if the real source is unknown return parseDom(content, FAKE_SOURCE); }
private ResponseRewriter createRewriter(Set<String> tags, Set<String> attributes) { ContentRewriterFeature.Factory rewriterFeatureFactory = new ContentRewriterFeature.Factory(null, new ContentRewriterFeature.DefaultConfig( ".*", "", "HTTP", "embed,img,script,link,style", false, false)); return new SanitizingResponseRewriter(rewriterFeatureFactory, new CajaCssSanitizer(new CajaCssParser()), new PassthruManager()); }
/** Serialize a stylesheet to a String */ public String serialize(CssTree.StyleSheet styleSheet) { StringWriter writer = new StringWriter(); serialize(styleSheet, writer); return writer.toString(); }
public CssTree.StyleSheet parseDom(String content, Uri source) throws GadgetException { CssTree.StyleSheet parsedCss = null; boolean shouldCache = shouldCache(); String key = null; if (shouldCache) { // TODO - Consider using the source if its under a certain size key = HashUtil.checksum(content.getBytes()); parsedCss = parsedCssCache.getElement(key); } if (parsedCss == null) { try { parsedCss = parseImpl(content, source); if (shouldCache) { parsedCssCache.addElement(key, parsedCss); } } catch (ParseException pe) { // Bad input; not server's fault. throw new GadgetException(GadgetException.Code.CSS_PARSE_ERROR, pe, HttpResponse.SC_BAD_REQUEST); } } if (shouldCache) { return (CssTree.StyleSheet)parsedCss.clone(); } return parsedCss; }
/** * 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 ""; } }
/** * Parse CSS content into Caja's CSS DOM model * * @return A parsed stylesheet */ public CssTree.StyleSheet parseDom(String content) throws GadgetException { // Use a fake source if the real source is unknown return parseDom(content, FAKE_SOURCE); }
private GadgetRewriter createRewriter(Set<String> tags, Set<String> attributes) { Set<String> newTags = new HashSet<String>(tags); newTags.addAll(DEFAULT_TAGS); ContentRewriterFeature.Factory rewriterFeatureFactory = new ContentRewriterFeature.Factory(null, Providers.of(new ContentRewriterFeature.DefaultConfig( ".*", "", "HTTP", "embed,img,script,link,style", false, false, false))); return new SanitizingGadgetRewriter(Providers.of(newTags), Providers.of(attributes), rewriterFeatureFactory, new CajaCssSanitizer(new CajaCssParser()), new PassthruManager("host.com", "/proxy")); }
@Before public void setUp() throws Exception { cajaCssParser = new CajaCssParser(); cajaCssParser.setCacheProvider(new LruCacheProvider(10)); }
/** 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); }
public CssTree.StyleSheet parseDom(String content, Uri source) throws GadgetException { CssTree.StyleSheet parsedCss = null; boolean shouldCache = shouldCache(); String key = null; if (shouldCache) { // TODO - Consider using the source if its under a certain size key = HashUtil.checksum(content.getBytes()); parsedCss = parsedCssCache.getElement(key); } if (parsedCss == null) { try { parsedCss = parseImpl(content, source); if (shouldCache) { parsedCssCache.addElement(key, parsedCss); } } catch (ParseException pe) { // Bad input; not server's fault. throw new GadgetException(GadgetException.Code.CSS_PARSE_ERROR, pe, HttpResponse.SC_BAD_REQUEST); } } if (shouldCache) { return (CssTree.StyleSheet)parsedCss.clone(); } return parsedCss; }
/** * 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); } }
@Test public void testCajaParseDotInIdent() throws Exception { String original = "li{list-style:none;.padding-bottom:4px;}"; cajaCssParser.parseDom(original); CssTree.StyleSheet styleSheet = cajaCssParser.parseDom(original); List<CssTree.SimpleSelector> selectorList = CajaCssUtils.descendants( styleSheet, CssTree.SimpleSelector.class); assertEquals(1, selectorList.size()); assertSame(CssTree.SimpleSelector.class, selectorList.get(0).getClass()); }
private GadgetRewriter createRewriter(Set<String> tags, Set<String> attributes) { Set<String> newTags = new HashSet<String>(tags); newTags.addAll(DEFAULT_TAGS); ContentRewriterFeature.Factory rewriterFeatureFactory = new ContentRewriterFeature.Factory(null, Providers.of(new ContentRewriterFeature.DefaultConfig( ".*", "", "HTTP", "embed,img,script,link,style", false, false, false))); return new SanitizingGadgetRewriter(Providers.of(newTags), Providers.of(attributes), rewriterFeatureFactory, new CajaCssSanitizer(new CajaCssParser()), new PassthruManager("host.com", "/proxy")); }
@Before public void setUp() throws Exception { cajaCssParser = new CajaCssParser(); cajaCssParser.setCacheProvider(new LruCacheProvider(10)); }