From 25b07812b9d8f21769f747cf5b72ec9c755c65ec Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sat, 18 Nov 2017 16:47:32 +0100 Subject: [PATCH] Sanitize the display value for choice widget annotations --- src/core/annotation.js | 3 ++- test/unit/annotation_spec.js | 45 +++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index f9f9c4b6c..993303302 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -798,7 +798,8 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation { this.data.options[i] = { exportValue: isOptionArray ? xref.fetchIfRef(option[0]) : option, - displayValue: isOptionArray ? xref.fetchIfRef(option[1]) : option, + displayValue: stringToPDFString(isOptionArray ? + xref.fetchIfRef(option[1]) : option), }; } } diff --git a/test/unit/annotation_spec.js b/test/unit/annotation_spec.js index 6f54e1586..1fb89116c 100644 --- a/test/unit/annotation_spec.js +++ b/test/unit/annotation_spec.js @@ -1092,14 +1092,8 @@ describe('annotation', function() { var options = [['foo_export', 'Foo'], optionOneRef]; var expected = [ - { - exportValue: 'foo_export', - displayValue: 'Foo', - }, - { - exportValue: 'bar_export', - displayValue: 'Bar', - } + { exportValue: 'foo_export', displayValue: 'Foo', }, + { exportValue: 'bar_export', displayValue: 'Bar', }, ]; choiceWidgetDict.set('Opt', options); @@ -1125,14 +1119,8 @@ describe('annotation', function() { var options = ['Foo', optionBarRef]; var expected = [ - { - exportValue: 'Foo', - displayValue: 'Foo', - }, - { - exportValue: 'Bar', - displayValue: 'Bar', - } + { exportValue: 'Foo', displayValue: 'Foo', }, + { exportValue: 'Bar', displayValue: 'Bar', }, ]; choiceWidgetDict.set('Opt', options); @@ -1179,6 +1167,31 @@ describe('annotation', function() { expect(data.options).toEqual(expected); }); + it('should sanitize display values in option arrays (issue 8947)', + function() { + // The option value is a UTF-16BE string. The display value should be + // sanitized, but the export value should remain the same since that + // may be used as a unique identifier when exporting form values. + var options = ['\xFE\xFF\x00F\x00o\x00o']; + var expected = [ + { exportValue: '\xFE\xFF\x00F\x00o\x00o', displayValue: 'Foo', }, + ]; + + choiceWidgetDict.set('Opt', options); + + var choiceWidgetRef = new Ref(984, 0); + var xref = new XRefMock([ + { ref: choiceWidgetRef, data: choiceWidgetDict, }, + ]); + + var annotation = AnnotationFactory.create(xref, choiceWidgetRef, + pdfManagerMock, idFactoryMock); + var data = annotation.data; + expect(data.annotationType).toEqual(AnnotationType.WIDGET); + + expect(data.options).toEqual(expected); + }); + it('should handle array field values', function() { var fieldValue = ['Foo', 'Bar'];