Extractor: ignore data URIs when adding outlinks

This commit is contained in:
Alex Osborne
2021-07-30 15:57:09 +09:00
parent 91ef23eddf
commit e33e1ff398
6 changed files with 56 additions and 4 deletions
@@ -88,6 +88,13 @@ import org.archive.url.LaxURLCodec;
public class UriUtils {
private static final Logger LOGGER = Logger.getLogger(UriUtils.class.getName());
/**
* Returns true when when given a CharSequence that looks like a data URI.
*/
public static boolean isDataUri(CharSequence candidate) {
return TextUtils.matches("(?i)\\s*data:.*", candidate);
}
// naive likely-uri test:
// no '<' or '>'
// at least one '.' or '/';
@@ -66,6 +66,17 @@ public class UriUtilsTest extends TestCase {
"images/photo.jpg",
"../../images/photo.jpg" };
public void testIsDataUri() {
assertTrue(UriUtils.isDataUri("data:,hello"));
assertTrue(UriUtils.isDataUri("data:text/plain,hello"));
assertTrue(UriUtils.isDataUri(" data:,hello"));
assertTrue(UriUtils.isDataUri(" dAtA:,hello// "));
assertFalse(UriUtils.isDataUri(""));
assertFalse(UriUtils.isDataUri(" http://example.org/"));
assertFalse(UriUtils.isDataUri("http://example.org/"));
assertFalse(UriUtils.isDataUri("\0\1\2\3garbage"));
}
/** check that plausible relative image URIs return true with legacy tests */
public void xestLegacySimpleImageRelatives() {
legacyTryAll(urisRelativeImages, true);
@@ -29,6 +29,7 @@ import org.archive.modules.CrawlURI;
import org.archive.modules.Processor;
import org.archive.net.UURI;
import org.archive.net.UURIFactory;
import org.archive.util.UriUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
@@ -134,10 +135,13 @@ public abstract class Extractor extends Processor {
/**
* Create and add a 'Link' to the CrawlURI with given URI/context/hop-type
* @return the new outlink or null if it was not valid
* @return the new outlink or null if it was invalid or ignored
*/
protected CrawlURI addOutlink(CrawlURI curi, String uri, LinkContext context,
Hop hop) {
if (UriUtils.isDataUri(uri)) {
return null;
}
try {
UURI dest = UURIFactory.getInstance(curi.getUURI(), uri);
CrawlURI link = curi.createCrawlURI(dest, context, hop);
@@ -151,6 +155,9 @@ public abstract class Extractor extends Processor {
protected void addOutlink(CrawlURI curi, UURI uuri, LinkContext context,
Hop hop) {
if ("data".equalsIgnoreCase(uuri.getScheme())) {
return;
}
try {
CrawlURI link = curi.createCrawlURI(uuri, context, hop);
curi.getOutLinks().add(link);
@@ -183,16 +190,29 @@ public abstract class Extractor extends Processor {
ret.append(" " + numberOfLinksExtracted + " links from " + getURICount() +" CrawlURIs\n");
return ret.toString();
}
/**
* Adds an outlink to uri relative to uri.getBaseURI().
* @return the new outlink or null if the outlink was ignored
*/
public static CrawlURI addRelativeToBase(CrawlURI uri, int max,
String newUri, LinkContext context, Hop hop) throws URIException {
if (UriUtils.isDataUri(newUri)) {
return null;
}
UURI dest = UURIFactory.getInstance(uri.getBaseURI(), newUri);
return add2(uri, max, dest, context, hop);
}
/**
* Adds an outlink to uri relative to uri.getVia().
* @return the new outlink or null if the outlink was ignored
*/
public static CrawlURI addRelativeToVia(CrawlURI uri, int max, String newUri,
LinkContext context, Hop hop) throws URIException {
if (UriUtils.isDataUri(newUri)) {
return null;
}
UURI relTo = uri.getVia();
if (relTo == null) {
if (!uri.getAnnotations().contains("usedBaseForVia")) {
@@ -75,6 +75,10 @@ public class ExtractorRobotsTxt extends ContentExtractor {
CrawlURI newCuri = addRelativeToBase(curi, max, link,
LinkContext.MANIFEST_MISC, Hop.MANIFEST);
if (newCuri == null) {
continue;
}
// Annotate as a Site Map:
newCuri.getAnnotations().add(
ExtractorRobotsTxt.ANNOTATION_IS_SITEMAP);
@@ -163,6 +163,10 @@ public class ExtractorSitemap extends ContentExtractor {
CrawlURI newCuri = addRelativeToBase(curi, max, newUri.toString(),
LinkContext.MANIFEST_MISC, Hop.MANIFEST);
if (newCuri == null) {
return;
}
if (isSitemap) {
// Annotate as a Site Map:
newCuri.getAnnotations().add(
@@ -268,6 +268,13 @@ public class ExtractorHTMLTest extends StringExtractorTestBase {
}
}));
}
public void testDataUrisAreIgnored() throws URIException {
CrawlURI curi = new CrawlURI(UURIFactory.getInstance("http://www.example.com"));
CharSequence cs = "<img src='data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='>";
getExtractor().extract(curi, cs);
assertEquals(0, curi.getOutLinks().size());
}
/**
* Test that relative base href's are resolved correctly:
@@ -521,7 +528,6 @@ public class ExtractorHTMLTest extends StringExtractorTestBase {
Arrays.sort(links);
String[] dest = {
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
"http://www.example.com/a,b,c",
"http://www.example.com/images/foo.jpg",
"http://www.example.com/images/foo1.jpg",