mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Fixed handling SVG files with missing tag
fix https://linear.app/tryghost/issue/SLO-151/[ghost]-cannot-read-properties-of-null-reading-attributes-an - in the event the file doesn't contain a tag, the code currently crashes because it tries to read `attributes from `undefined` - we can fix that by checking the first element exists before reading from it - also includes a breaking test
This commit is contained in:
parent
cd8a54d7cc
commit
9a40440e82
3 changed files with 15 additions and 5 deletions
|
@ -152,19 +152,23 @@ const checkFileIsValid = (fileData, types, extensions) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {String} filepath
|
* @param {String} filepath
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
*
|
*
|
||||||
* Checks for the presence of <script> tags or 'on' attributes in an SVG file
|
* Checks for the presence of <script> tags or 'on' attributes in an SVG file
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
const isSvgSafe = (filepath) => {
|
const isSvgSafe = (filepath) => {
|
||||||
const fileContent = fs.readFileSync(filepath, 'utf8');
|
const fileContent = fs.readFileSync(filepath, 'utf8');
|
||||||
const document = new JSDOM(fileContent).window.document;
|
const document = new JSDOM(fileContent).window.document;
|
||||||
document.body.innerHTML = fileContent;
|
document.body.innerHTML = fileContent;
|
||||||
const svgEl = document.body.firstElementChild;
|
const svgEl = document.body.firstElementChild;
|
||||||
|
|
||||||
|
if (!svgEl) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const attributes = Array.from(svgEl.attributes).map(({name}) => name);
|
const attributes = Array.from(svgEl.attributes).map(({name}) => name);
|
||||||
const hasScriptAttr = !!attributes.find(attr => attr.startsWith('on'));
|
const hasScriptAttr = !!attributes.find(attr => attr.startsWith('on'));
|
||||||
const scripts = svgEl.getElementsByTagName('script');
|
const scripts = svgEl.getElementsByTagName('script');
|
||||||
|
|
|
@ -63,5 +63,10 @@ describe('web utils', function () {
|
||||||
dirtySvgContent.should.not.containEql('<script');
|
dirtySvgContent.should.not.containEql('<script');
|
||||||
validation.isSvgSafe(filepath).should.be.true;
|
validation.isSvgSafe(filepath).should.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns false for malformed svg', async function () {
|
||||||
|
const filepath = path.join(__dirname, imageFixturePath, 'svg-malformed.svg');
|
||||||
|
validation.isSvgSafe(filepath).should.be.false;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
1
ghost/core/test/utils/fixtures/images/svg-malformed.svg
Normal file
1
ghost/core/test/utils/fixtures/images/svg-malformed.svg
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<
|
Loading…
Add table
Reference in a new issue