diff --git a/lighthouse-cli/test/fixtures/seo/seo-failure-cases.html b/lighthouse-cli/test/fixtures/seo/seo-failure-cases.html index c27b8a7aa7cd..f6822f7f4dbe 100644 --- a/lighthouse-cli/test/fixtures/seo/seo-failure-cases.html +++ b/lighthouse-cli/test/fixtures/seo/seo-failure-cases.html @@ -25,5 +25,25 @@

Anchor text

click this click this CLICK THIS + + + + Your browser does not support the applet tag. + + + + + + + + + + + Get Adobe Flash player + + + + + diff --git a/lighthouse-cli/test/fixtures/seo/seo-tester.html b/lighthouse-cli/test/fixtures/seo/seo-tester.html index 3b5959e4ab10..1728dd1e87de 100644 --- a/lighthouse-cli/test/fixtures/seo/seo-tester.html +++ b/lighthouse-cli/test/fixtures/seo/seo-tester.html @@ -42,5 +42,7 @@

Small text

+ + diff --git a/lighthouse-cli/test/smokehouse/seo/expectations.js b/lighthouse-cli/test/smokehouse/seo/expectations.js index 42b8c4be535c..e5761f2f5b35 100644 --- a/lighthouse-cli/test/smokehouse/seo/expectations.js +++ b/lighthouse-cli/test/smokehouse/seo/expectations.js @@ -57,6 +57,9 @@ module.exports = [ 'hreflang': { score: true, }, + 'plugins': { + score: true, + }, }, }, { @@ -110,6 +113,14 @@ module.exports = [ }, }, }, + 'plugins': { + score: false, + details: { + items: { + length: 3, + }, + }, + }, }, }, ]; diff --git a/lighthouse-core/audits/seo/plugins.js b/lighthouse-core/audits/seo/plugins.js new file mode 100644 index 000000000000..d0d3441e21ff --- /dev/null +++ b/lighthouse-core/audits/seo/plugins.js @@ -0,0 +1,147 @@ +/** + * @license Copyright 2018 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +const Audit = require('../audit'); +const URL = require('../../lib/url-shim'); + +const JAVA_APPLET_TYPE = 'application/x-java-applet'; +const JAVA_BEAN_TYPE = 'application/x-java-bean'; +const TYPE_BLOCKLIST = new Set([ + 'application/x-shockwave-flash', + // See https://docs.oracle.com/cd/E19683-01/816-0378/using_tags/index.html + JAVA_APPLET_TYPE, + JAVA_BEAN_TYPE, + // See https://msdn.microsoft.com/es-es/library/cc265156(v=vs.95).aspx + 'application/x-silverlight', + 'application/x-silverlight-2', +]); +const FILE_EXTENSION_BLOCKLIST = new Set([ + 'swf', + 'flv', + 'class', + 'xap', +]); +const SOURCE_PARAMS = new Set([ + 'code', + 'movie', + 'source', + 'src', +]); + +/** + * Verifies if given MIME type matches any known plugin MIME type + * @param {string} type + */ +function isPluginType(type) { + type = type.trim().toLowerCase(); + + return TYPE_BLOCKLIST.has(type) || + type.startsWith(JAVA_APPLET_TYPE) || // e.g. "application/x-java-applet;jpi-version=1.4" + type.startsWith(JAVA_BEAN_TYPE); +} + +/** + * Verifies if given url points to a file that has a known plugin extension + * @param {string} url + */ +function isPluginURL(url) { + try { + // in order to support relative URLs we need to provied a base URL + const filePath = new URL(url, 'http://example.com').pathname; + const parts = filePath.split('.'); + + return parts.length > 1 && FILE_EXTENSION_BLOCKLIST.has(parts.pop().trim().toLowerCase()); + } catch (e) { + return false; + } +} + +class Plugins extends Audit { + /** + * @return {!AuditMeta} + */ + static get meta() { + return { + name: 'plugins', + description: 'Document avoids plugins.', + failureDescription: 'Document uses plugins', + helpText: 'Some types of media or content are not playable on mobile devices. ' + + '[Learn more](https://developers.google.com/speed/docs/insights/AvoidPlugins).', + requiredArtifacts: ['EmbeddedContent'], + }; + } + + /** + * @param {!Artifacts} artifacts + * @return {!AuditResult} + */ + static audit(artifacts) { + const plugins = artifacts.EmbeddedContent + .filter(item => { + if (item.tagName === 'APPLET') { + return true; + } + + if ( + (item.tagName === 'EMBED' || item.tagName === 'OBJECT') && + item.type && + isPluginType(item.type) + ) { + return true; + } + + const embedSrc = item.src || item.code; + if (item.tagName === 'EMBED' && embedSrc && isPluginURL(embedSrc)) { + return true; + } + + if (item.tagName === 'OBJECT' && item.data && isPluginURL(item.data)) { + return true; + } + + const failingParams = item.params.filter(param => + SOURCE_PARAMS.has(param.name.trim().toLowerCase()) && isPluginURL(param.value) + ); + + return failingParams.length > 0; + }) + .map(plugin => { + const tagName = plugin.tagName.toLowerCase(); + const attributes = ['src', 'data', 'code', 'type'] + .reduce((result, attr) => { + if (plugin[attr] !== null) { + result += ` ${attr}="${plugin[attr]}"`; + } + return result; + }, ''); + const params = plugin.params + .filter(param => SOURCE_PARAMS.has(param.name.trim().toLowerCase())) + .map(param => ``) + .join(''); + + return { + source: { + type: 'node', + snippet: `<${tagName}${attributes}>${params}`, + }, + }; + }); + + const headings = [ + {key: 'source', itemType: 'code', text: 'Source'}, + ]; + + const details = Audit.makeTableDetails(headings, plugins); + + return { + rawValue: plugins.length === 0, + details, + }; + } +} + +module.exports = Plugins; diff --git a/lighthouse-core/config/default.js b/lighthouse-core/config/default.js index 6f0881ff3b2e..86372b554572 100644 --- a/lighthouse-core/config/default.js +++ b/lighthouse-core/config/default.js @@ -42,6 +42,7 @@ module.exports = { 'seo/crawlable-links', 'seo/meta-robots', 'seo/hreflang', + 'seo/embedded-content', ], }, { @@ -171,6 +172,7 @@ module.exports = { 'seo/link-text', 'seo/is-crawlable', 'seo/hreflang', + 'seo/plugins', 'seo/manual/mobile-friendly', 'seo/manual/structured-data', ], @@ -386,6 +388,7 @@ module.exports = { {id: 'is-crawlable', weight: 1, group: 'seo-crawl'}, {id: 'hreflang', weight: 1, group: 'seo-content'}, {id: 'font-size', weight: 1, group: 'seo-mobile'}, + {id: 'plugins', weight: 1, group: 'seo-content'}, {id: 'mobile-friendly', weight: 0, group: 'manual-seo-checks'}, {id: 'structured-data', weight: 0, group: 'manual-seo-checks'}, ], diff --git a/lighthouse-core/gather/gatherers/seo/embedded-content.js b/lighthouse-core/gather/gatherers/seo/embedded-content.js new file mode 100644 index 000000000000..8d292b5272ee --- /dev/null +++ b/lighthouse-core/gather/gatherers/seo/embedded-content.js @@ -0,0 +1,41 @@ +/** + * @license Copyright 2018 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +const Gatherer = require('../gatherer'); +const DOMHelpers = require('../../../lib/dom-helpers.js'); + +class EmbeddedContent extends Gatherer { + /** + * @param {{driver: !Driver}} options Run options + * @return {!Promise}>>} All s, s and s with list of relevant attributes and child properties + */ + afterPass(options) { + const expression = `(function() { + ${DOMHelpers.getElementsInDocumentFnString}; // define function on page + const selector = 'object, embed, applet'; + const elements = getElementsInDocument(selector); + return elements + .map(node => ({ + tagName: node.tagName, + type: node.getAttribute('type'), + src: node.getAttribute('src'), + data: node.getAttribute('data'), + code: node.getAttribute('code'), + params: Array.from(node.children) + .filter(el => el.tagName === 'PARAM') + .map(el => ({ + name: el.getAttribute('name') || '', + value: el.getAttribute('value') || '', + })), + })); + })()`; + + return options.driver.evaluateAsync(expression); + } +} + +module.exports = EmbeddedContent; diff --git a/lighthouse-core/test/audits/seo/plugins.js b/lighthouse-core/test/audits/seo/plugins.js new file mode 100644 index 000000000000..6f70dfc807ed --- /dev/null +++ b/lighthouse-core/test/audits/seo/plugins.js @@ -0,0 +1,141 @@ +/** + * @license Copyright 2018 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +const PluginsAudit = require('../../../audits/seo/plugins.js'); +const assert = require('assert'); + +/* eslint-env mocha */ + +describe('SEO: Avoids plugins', () => { + it('fails when page contains java, silverlight or flash content', () => { + const embeddedContentValues = [ + [{ + tagName: 'APPLET', + params: [], + }], + [{ + tagName: 'OBJECT', + type: 'application/x-shockwave-flash', + params: [], + }], + [{ + tagName: 'EMBED', + type: 'application/x-java-applet;jpi-version=1.4', + params: [], + }], + [{ + tagName: 'OBJECT', + type: 'application/x-silverlight-2', + params: [], + }], + [{ + tagName: 'OBJECT', + data: 'https://example.com/movie_name.swf?uid=123', + params: [], + }], + [{ + tagName: 'EMBED', + src: '/path/to/movie_name.latest.swf', + params: [], + }], + [{ + tagName: 'OBJECT', + params: [ + {name: 'quality', value: 'low'}, + {name: 'movie', value: 'movie.swf?id=123'}, + ], + }], + [{ + tagName: 'OBJECT', + params: [ + {name: 'code', value: '../HelloWorld.class'}, + ], + }], + ]; + + embeddedContentValues.forEach(embeddedContent => { + const artifacts = { + EmbeddedContent: embeddedContent, + }; + + const auditResult = PluginsAudit.audit(artifacts); + assert.equal(auditResult.rawValue, false); + assert.equal(auditResult.details.items.length, 1); + }); + }); + + it('returns multiple results when there are multiple failing items', () => { + const artifacts = { + EmbeddedContent: [ + { + tagName: 'EMBED', + type: 'application/x-java-applet;jpi-version=1.4', + params: [], + }, + { + tagName: 'OBJECT', + type: 'application/x-silverlight-2', + params: [], + }, + { + tagName: 'APPLET', + params: [], + }, + ], + }; + + const auditResult = PluginsAudit.audit(artifacts); + assert.equal(auditResult.rawValue, false); + assert.equal(auditResult.details.items.length, 3); + }); + + it('succeeds when there is no external content found on page', () => { + const artifacts = { + EmbeddedContent: [], + }; + + const auditResult = PluginsAudit.audit(artifacts); + assert.equal(auditResult.rawValue, true); + }); + + it('succeeds when all external content is valid', () => { + const artifacts = { + EmbeddedContent: [ + { + tagName: 'OBJECT', + type: 'image/svg+xml', + data: 'https://example.com/test.svg', + params: [], + }, + { + tagName: 'OBJECT', + data: 'https://example.com', + params: [], + }, + { + tagName: 'EMBED', + type: 'video/quicktime', + src: 'movie.mov', + params: [], + }, + { + tagName: 'OBJECT', + params: [{ + name: 'allowFullScreen', + value: 'true', + }, { + name: 'movie', + value: 'http://www.youtube.com/v/example', + }], + }, + ], + }; + + const auditResult = PluginsAudit.audit(artifacts); + assert.equal(auditResult.rawValue, true); + }); +});