From 8906cbbb1af2781ec3655dd39f33f516b673c977 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Tue, 16 Apr 2019 07:32:06 -0500 Subject: [PATCH] Prevent importing styles with no section. Fixes #687 (#697) * Prevent importing styles with no section. Fixes #687 And prevent issues with existing styles with no section * Revert sections checks for empty array * Add styleJSONseemsValid function * Clean up * Fix eslint nags --- background/update.js | 10 +--------- js/sections-util.js | 13 ++++++++++++- manage/import-export.js | 9 +++------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/background/update.js b/background/update.js index 328c8611..3276d883 100644 --- a/background/update.js +++ b/background/update.js @@ -1,6 +1,6 @@ /* global styleSectionsEqual prefs download tryJSONparse ignoreChromeError calcStyleDigest getStyleWithNoCode debounce chromeLocal - usercss semverCompare + usercss semverCompare styleJSONseemsValid API_METHODS styleManager */ 'use strict'; @@ -231,14 +231,6 @@ API_METHODS[json.usercssData ? 'installUsercss' : 'installStyle'](newStyle) : newStyle; } - - function styleJSONseemsValid(json) { - return json - && json.sections - && json.sections.length - && typeof json.sections.every === 'function' - && typeof json.sections[0].code === 'string'; - } } function schedule() { diff --git a/js/sections-util.js b/js/sections-util.js index aeeeb19b..f44b52a2 100644 --- a/js/sections-util.js +++ b/js/sections-util.js @@ -1,4 +1,4 @@ -/* exported styleSectionsEqual styleCodeEmpty calcStyleDigest */ +/* exported styleSectionsEqual styleCodeEmpty calcStyleDigest styleJSONseemsValid */ 'use strict'; function styleCodeEmpty(code) { @@ -96,3 +96,14 @@ function calcStyleDigest(style) { return parts.join(''); } } + +function styleJSONseemsValid(json) { + return json + && json.name + && json.name.trim() + && Array.isArray(json.sections) + && json.sections + && json.sections.length + && typeof json.sections.every === 'function' + && typeof json.sections[0].code === 'string'; +} diff --git a/manage/import-export.js b/manage/import-export.js index 1a2f1d15..327314c0 100644 --- a/manage/import-export.js +++ b/manage/import-export.js @@ -1,5 +1,6 @@ /* global messageBox styleSectionsEqual getOwnTab API onDOMready - tryJSONparse scrollElementIntoView $ $$ API $create t animateElement */ + tryJSONparse scrollElementIntoView $ $$ API $create t animateElement + styleJSONseemsValid */ 'use strict'; const STYLISH_DUMP_FILE_EXT = '.txt'; @@ -144,11 +145,7 @@ function importFromString(jsonString) { .then(done); function analyze(item, index) { - if (typeof item !== 'object' || - !item || - !item.name || - !item.name.trim() || - (item.sections && !Array.isArray(item.sections))) { + if (typeof item !== 'object' || !styleJSONseemsValid(item)) { stats.invalid.names.push(`#${index}: ${limitString(item && item.name || '')}`); return; }