import test from 'node:test'; import {Linter} from 'eslint'; import css from '@eslint/css'; import json from '@eslint/json'; import markdown from '@eslint/markdown'; import html from '@html-eslint/eslint-plugin'; import toml from 'eslint-plugin-toml'; import unicorn from '../index.js'; import {getTester} from './utils/test.js'; const {test: ruleTest} = getTester(import.meta); const MESSAGE = 'Prefer HTTPS over HTTP.'; const RULE_ID = 'unicorn/prefer-https'; const LANGUAGE_PLUGINS = { css, json, markdown, html, toml, }; const XML_SECURITY_URIS = [ 'http://www.w3.org/TR/1999/REC-xpath-19991116', 'http://www.w3.org/TR/1999/REC-xslt-19991116', 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315', 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments', 'http://www.w3.org/2000/09/xmldsig#', 'http://www.w3.org/2000/09/xmldsig#base64', 'http://www.w3.org/2000/09/xmldsig#enveloped-signature', 'http://www.w3.org/2000/09/xmldsig#hmac-sha1', 'http://www.w3.org/2000/09/xmldsig#Manifest', 'http://www.w3.org/2000/09/xmldsig#rsa-sha1', 'http://www.w3.org/2000/09/xmldsig#sha1', 'http://www.w3.org/2001/04/xmldsig-more#', 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha1', 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256', 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha384', 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha512', 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256', 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha384', 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha512', 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256', 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384', 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512', 'http://www.w3.org/2001/04/xmldsig-more#sha384', 'http://www.w3.org/2001/04/xmlenc#sha256', 'http://www.w3.org/2001/04/xmlenc#sha512', 'http://www.w3.org/2001/10/xml-exc-c14n#', 'http://www.w3.org/2001/10/xml-exc-c14n#WithComments', 'http://www.w3.org/2002/06/xmldsig-filter2', 'http://www.w3.org/2002/07/decrypt#XML', 'http://www.w3.org/2006/12/xml-c14n11', 'http://www.w3.org/2006/12/xml-c14n11#WithComments', 'http://www.w3.org/2007/05/xmldsig-more#', 'http://www.w3.org/2007/05/xmldsig-more#MGF1', 'http://www.w3.org/2007/05/xmldsig-more#rsa-pss', 'http://www.w3.org/2007/05/xmldsig-more#sha1-rsa-MGF1', 'http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1', 'http://www.w3.org/2007/05/xmldsig-more#sha384-rsa-MGF1', 'http://www.w3.org/2007/05/xmldsig-more#sha512-rsa-MGF1', ]; const XML_SECURITY_IDENTIFIER_NAMES = [ 'xmldsig', 'xmldsig-more', 'xmlenc', 'xml-exc-c14n', 'decrypt', ]; const OPAQUE_IDENTIFIER_URIS = [ 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'http://www.w3.org/2000/01/rdf-schema#label', 'http://www.w3.org/2002/07/owl#Class', 'http://www.w3.org/2001/XMLSchema#string', 'http://purl.org/rss/1.0/title', 'http://purl.org/dc/elements/1.1/title', 'http://purl.org/dc/terms/title', 'http://www.w3.org/2001/XInclude', 'http://www.w3.org/2005/xpath-functions', 'http://www.w3.org/2005/xpath-functions#max', 'http://www.w3.org/2005/xpath-functions/map', 'http://www.w3.org/2005/xpath-functions/map#size', 'http://www.w3.org/2005/xpath-functions/array', 'http://www.w3.org/2005/xpath-functions/math#pi', 'http://www.w3.org/2005/xqt-errors', 'http://www.w3.org/2005/xqt-errors#XPST0017', 'http://www.w3.org/2005/07/scxml', 'http://www.w3.org/2004/02/skos/core#Concept', 'http://www.w3.org/ns/prov#Entity', 'http://www.w3.org/ns/shacl#NodeShape', 'http://www.w3.org/ns/dcat#Dataset', 'http://schemas.android.com/apk/res/android', 'http://schemas.android.com/apk/res/com.example', 'http://schemas.android.com/apk/res-auto', 'http://schemas.android.com/aapt', 'http://schemas.android.com/tools', 'http://maven.apache.org/POM/4.0.0', 'http://maven.apache.org/SETTINGS/1.0.0', 'http://maven.apache.org/TOOLCHAINS/1.1.0', 'http://maven.apache.org/METADATA/1.1.0', 'http://maven.apache.org/EXTENSIONS/1.0.0', 'http://schemas.microsoft.com/developer/msbuild/2003', 'http://www.springframework.org/schema/beans', 'http://www.springframework.org/schema/integration', 'http://www.springframework.org/schema/integration/security', 'http://json-schema.org/schema#', 'http://json-schema.org/draft-03/schema#', 'http://json-schema.org/draft-04/schema#', 'http://json-schema.org/draft-04/hyper-schema#', 'http://json-schema.org/draft-04/links#', 'http://json-schema.org/draft-06/schema#', 'http://json-schema.org/draft-07/schema#', 'http://json-schema.org/draft-07/schema', ]; ruleTest.snapshot({ testerOptions: { languageOptions: { parserOptions: { ecmaFeatures: { jsx: true, }, }, }, }, valid: [ 'const url = "HTTP://sindresorhus.com";', 'const url = "HTTP://example.com/http://sindresorhus.com";', // Reserved suffixes never resolve on the public internet 'const url = "http://myapp.local/api";', 'const url = "http://server.test";', 'const url = "http://api.internal";', 'const url = "http://foo.invalid";', 'const url = "http://foo.example";', 'const url = "http://app.localhost:3000";', 'const url = "http://nas.localdomain";', 'const url = "http://duckduckgo.onion";', 'const url = "http://SERVER.TEST";', 'const url = "http://server.test.";', 'const url = "https://sindresorhus.com";', 'const url = "https://example.com/http://sindresorhus.com";', 'const url = "http://localhost";', 'const url = "http://example";', 'const url = "http://127.0.0.1";', 'const url = "http://[::1]";', 'const url = "http://example.123";', 'const url = "http://sindresorhus.com:invalid";', 'const text = "prefixhttp://sindresorhus.com";', '// http://example,', '// http://localhost', 'const element = https://sindresorhus.com;', `// eslint-disable-next-line rule-to-test/prefer-https // http://sindresorhus.com`, // XML namespace URIs are opaque identifiers, not network requests 'const element = ;', 'const element = ;', 'const element = ;', 'const element = ;', // Hyphenated NCName prefix (XML allows hyphens in namespace prefixes) 'const element = ;', // Single-quoted attribute value 'const element = ;', // Spaces around the equals sign 'const element = ;', // Well-known namespace URIs are opaque identifiers anywhere they appear, not just in `xmlns=` attributes (e.g. passed to DOM `*NS` methods). 'const SVG_NAMESPACE = "http://www.w3.org/2000/svg";', 'document.createElementNS("http://www.w3.org/2000/svg", "svg");', 'svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", SVG_NAMESPACE);', 'const ns = "http://www.w3.org/1998/Math/MathML";', // Namespace URI ending in a fragment 'const ns = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";', // Non-W3C well-known namespaces 'const ns = "http://schemas.xmlsoap.org/soap/envelope/";', 'const ns = "http://purl.org/dc/elements/1.1/";', 'const ns = "http://www.sitemaps.org/schemas/sitemap/0.9";', ...XML_SECURITY_URIS.map(uri => `const uri = "${uri}";`), ...XML_SECURITY_IDENTIFIER_NAMES.map(name => `const uri = "http://www.w3.org/2099/12/${name}#future-algorithm";`), 'const uri = "http://www.w3.org/2009/xmldsig11#future-algorithm";', 'const uri = "http://www.w3.org/2009/xmlenc11#future-algorithm";', 'const uri = "http://www.w3.org/2099/12/xmldsig-filter3";', 'const uri = "http://www.w3.org/2099/12/xml-c14n12#WithComments";', ...OPAQUE_IDENTIFIER_URIS.map(uri => `const uri = "${uri}";`), { code: 'const uri = "http://example.com/identifier/value";', options: [{ignore: ['http://example.com/identifier/value']}], }, { code: 'const url = "http://example.com/http://sindresorhus.com";', options: [{ignore: ['http://example.com/http://sindresorhus.com']}], }, { code: 'const uri = "http://schemas.example.com/value";', options: [{ignore: [/^http:\/\/schemas\.example\.com\//v]}], }, { code: 'const uris = ["http://example.com/one", "http://example.com/two"];', options: [{ignore: [/^http:\/\/unused\.example\//v, /^http:\/\/example\.com\//gv]}], }, { code: 'const url = "http://example.com/identifier/value?format=xml#section";', options: [{ignore: [/\?format=xml#section$/v]}], }, ], invalid: [ 'const url = "http://sindresorhus.com";', 'const url = `http://sindresorhus.com/path`;', '// http://sindresorhus.com', '// http://sindresorhus.com,', 'const element = https://sindresorhus.com;', 'const element = http://sindresorhus.com;', `const urls = [ "http://sindresorhus.com", "http://example.com", ];`, 'const url = "http://user:password@sindresorhus.com";', 'const url = "http://êxample.com";', 'const url = "http://sindresorhus.com:8080/path";', 'const url = "http://example.com/http://sindresorhus.com";', 'const url = "http://sindresorhus.com.";', // Non-xmlns attribute adjacent to an xmlns attribute must still be flagged 'const element = ;', // Attribute names ending in "xmlns" are not namespace declarations 'const element =
;', 'const $xmlns = "http://sindresorhus.com";', // A longer URL that merely shares a namespace prefix is not a namespace 'const url = "http://www.w3.org/2000/svg/extra/path";', // The bare registry host is not itself a namespace identifier 'const url = "http://www.w3.org";', { code: 'const uri = "http://example.com/identifier/value";', options: [{ignore: ['http://other.example/identifier/value']}], }, { code: 'const uri = "http://example.com/identifier/value/extra";', options: [{ignore: ['http://example.com/identifier/value']}], }, 'const uri = "http://www.w3.org/2000/09/xmldsig-other#sha1";', 'const uri = "http://www.w3.org/2000/09/xmldsig/path";', 'const uri = "http://www.w3.org/2002/06/xmldsig-filter2/extra";', 'const uri = "http://www.w3.org/2006/12/xml-c14n11?format=xml";', 'const uri = "http://www.w3.org/2005/xpath-functions/map/extra";', 'const uri = "http://www.w3.org/2001/XInclude/extra";', 'const uri = "http://schemas.android.com/apk/res/android/extra";', 'const uri = "http://schemas.android.com/apk/res/android?theme=dark";', 'const uri = "http://schemas.android.com/apk/res/123-invalid";', 'const uri = "http://maven.apache.org/xsd/maven-4.0.0.xsd";', 'const uri = "http://maven.apache.org/DOWNLOAD/1.0";', 'const uri = "http://www.springframework.org/schema/beans/spring-beans.xsd";', 'const uri = "http://www.springframework.org/schema/beans?version=1";', 'const uri = "http://purl.org/dc/terms/title/document";', 'const uri = "http://purl.org/dc/terms/title?format=xml";', 'const uri = "http://purl.org/dc/terms/title.";', 'const uri = "http://schemas.android.com/apk/res/android.";', // Only JSON Schema meta-schema identifiers are ignored, not other pages on the same host 'const url = "http://json-schema.org/learn/getting-started-step-by-step";', 'const uri = "http://json-schema.org/draft-07/schema/extra";', 'const uri = "http://json-schema.org/draft-08/schema#";', // A reserved suffix only counts as the last label 'const url = "http://test.com";', 'const url = "http://local.example.com";', ], }); test('rejects non-HTTP string ignores', t => { const linter = new Linter({configType: 'flat'}); const config = { plugins: {unicorn}, rules: { [RULE_ID]: ['error', {ignore: ['example.com']}], }, }; t.assert.throws(() => linter.verify('const url = "http://example.com";', config), {message: /ignore.*start with.*http:\/\//iv}); }); test('does not ignore URLs that do not match a regular expression', t => { const code = 'const url = "http://example.com/identifier/value";'; const config = { plugins: {unicorn}, rules: { [RULE_ID]: ['error', {ignore: [/^http:\/\/other\.example\//v]}], }, }; const linter = new Linter({configType: 'flat'}); const messages = linter.verify(code, config); const result = linter.verifyAndFix(code, config); t.assert.strictEqual(messages.length, 1); t.assert.strictEqual(messages[0].message, MESSAGE); t.assert.strictEqual(result.fixed, true); t.assert.strictEqual(result.output, 'const url = "https://example.com/identifier/value";'); }); test('handles long hostname dot sequences efficiently', t => { const code = `// http://a${'.'.repeat(100_000)}b.com`; const config = { plugins: {unicorn}, rules: { [RULE_ID]: 'error', }, }; const linter = new Linter({configType: 'flat'}); const startTime = performance.now(); const messages = linter.verify(code, config); const duration = performance.now() - startTime; t.assert.strictEqual(messages.length, 1); t.assert.strictEqual(duration < 2000, true, `Expected linting to take less than 2 seconds, but it took ${duration} milliseconds.`); }); function createLanguageConfig(language, rule = 'error') { const pluginName = language.split('/', 1)[0]; return { files: ['**'], language, plugins: { [pluginName]: LANGUAGE_PLUGINS[pluginName], unicorn, }, rules: { [RULE_ID]: rule, }, }; } const languageCases = [ { name: 'TOML', filename: 'fixture.toml', language: 'toml/toml', code: '# http://sindresorhus.com\nurl = "http://example.com"', output: '# https://sindresorhus.com\nurl = "https://example.com"', errors: 2, }, { name: 'CSS', filename: 'fixture.css', language: 'css/css', code: '.logo { background-image: url("http://sindresorhus.com/logo.svg"); }', output: '.logo { background-image: url("https://sindresorhus.com/logo.svg"); }', errors: 1, }, { name: 'JSON', filename: 'fixture.json', language: 'json/json', code: '{"url": "http://sindresorhus.com"}', output: '{"url": "https://sindresorhus.com"}', errors: 1, }, { name: 'JSONC', filename: 'fixture.jsonc', language: 'json/jsonc', code: `{ // http://sindresorhus.com "url": "http://example.com" }`, output: `{ // https://sindresorhus.com "url": "https://example.com" }`, errors: 2, }, { name: 'JSON5', filename: 'fixture.json5', language: 'json/json5', code: '{url: "http://sindresorhus.com"}', output: '{url: "https://sindresorhus.com"}', errors: 1, }, { name: 'HTML', filename: 'fixture.html', language: 'html/html', code: 'http://example.com', output: 'https://example.com', errors: 2, }, { name: 'CommonMark', filename: 'fixture.md', language: 'markdown/commonmark', code: '[Sindre](http://sindresorhus.com)', output: '[Sindre](https://sindresorhus.com)', errors: 1, }, { name: 'GFM', filename: 'fixture.md', language: 'markdown/gfm', code: '| URL |\n| --- |\n| http://sindresorhus.com |', output: '| URL |\n| --- |\n| https://sindresorhus.com |', errors: 1, }, { name: 'JSON with a JSON Schema meta-schema identifier', filename: 'fixture.json', language: 'json/json', code: '{"$schema": "http://json-schema.org/draft-07/schema#", "homepage": "http://sindresorhus.com"}', output: '{"$schema": "http://json-schema.org/draft-07/schema#", "homepage": "https://sindresorhus.com"}', errors: 1, }, { name: 'JSON with ignored URL', filename: 'fixture.json', language: 'json/json', code: '{"url": "http://example.com/identifier/value"}', output: '{"url": "http://example.com/identifier/value"}', errors: 0, fixed: false, rule: ['error', {ignore: ['http://example.com/identifier/value']}], }, ]; for (const {name, code, output, language, filename, errors, fixed = true, rule} of languageCases) { test(`supports ${name}`, t => { const config = createLanguageConfig(language, rule); const linter = new Linter({configType: 'flat'}); const messages = linter.verify(code, config, {filename}); const result = linter.verifyAndFix(code, config, {filename}); t.assert.strictEqual(result.fixed, fixed); t.assert.strictEqual(result.output, output); t.assert.deepStrictEqual( messages.map(({message, ruleId}) => ({message, ruleId})), Array.from({length: errors}, () => ( { message: MESSAGE, ruleId: RULE_ID, } )), ); }); }