From c4fe4087d32d07ac039a1aa817e715c60fb1bbb0 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sat, 19 Jan 2019 19:54:08 +0100 Subject: [PATCH] Implement a unit test for metadata parsing to ensure that it's not vulnerable to the billion laughs attack --- test/unit/metadata_spec.js | 50 ++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/test/unit/metadata_spec.js b/test/unit/metadata_spec.js index b88d31c1d..c710a8717 100644 --- a/test/unit/metadata_spec.js +++ b/test/unit/metadata_spec.js @@ -18,12 +18,12 @@ import { Metadata } from '../../src/display/metadata'; describe('metadata', function() { it('should handle valid metadata', function() { - var validData = '' + + const data = '' + '' + '' + 'Foo bar baz' + ''; - var metadata = new Metadata(validData); + const metadata = new Metadata(data); expect(metadata.has('dc:title')).toBeTruthy(); expect(metadata.has('dc:qux')).toBeFalsy(); @@ -35,12 +35,12 @@ describe('metadata', function() { }); it('should repair and handle invalid metadata', function() { - var invalidData = '' + + const data = '' + '' + '' + '\\376\\377\\000P\\000D\\000F\\000&' + ''; - var metadata = new Metadata(invalidData); + const metadata = new Metadata(data); expect(metadata.has('dc:title')).toBeTruthy(); expect(metadata.has('dc:qux')).toBeFalsy(); @@ -52,7 +52,7 @@ describe('metadata', function() { }); it('should repair and handle invalid metadata (bug 1424938)', function() { - let invalidData = '' + '' + @@ -82,7 +82,7 @@ describe('metadata', function() { '\\376\\377\\000O\\000D\\000I\\000S' + '' + ''; - let metadata = new Metadata(invalidData); + const metadata = new Metadata(data); expect(metadata.has('dc:title')).toBeTruthy(); expect(metadata.has('dc:qux')).toBeFalsy(); @@ -99,7 +99,7 @@ describe('metadata', function() { }); it('should gracefully handle incomplete tags (issue 8884)', function() { - let data = '' + '' + '' + '' + ''; - let metadata = new Metadata(data); + const metadata = new Metadata(data); expect(isEmptyObj(metadata.getAll())).toEqual(true); }); @@ -218,4 +218,38 @@ describe('metadata', function() { expect(isEmptyObj(metadata.getAll())).toEqual(true); }); + + it('should not be vulnerable to the billion laughs attack', function() { + const data = '' + + '' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ']>' + + '' + + ' ' + + ' ' + + ' ' + + ' a&lol9;b' + + ' ' + + ' ' + + ' ' + + ''; + const metadata = new Metadata(data); + + expect(metadata.has('dc:title')).toBeTruthy(); + expect(metadata.has('dc:qux')).toBeFalsy(); + + expect(metadata.get('dc:title')).toEqual('a&lol9;b'); + expect(metadata.get('dc:qux')).toEqual(null); + + expect(metadata.getAll()).toEqual({ 'dc:title': 'a&lol9;b', }); + }); });