Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip null (\x00) characters from the URLs in LinkAnnotations (issue 6832) #6834

Merged
merged 1 commit into from
Jan 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var AnnotationType = sharedUtil.AnnotationType;
var Util = sharedUtil.Util;
var isExternalLinkTargetSet = sharedUtil.isExternalLinkTargetSet;
var LinkTargetStringMap = sharedUtil.LinkTargetStringMap;
var removeNullCharacters = sharedUtil.removeNullCharacters;
var warn = sharedUtil.warn;
var CustomStyle = displayDOMUtils.CustomStyle;

Expand Down Expand Up @@ -232,7 +233,8 @@ var LinkAnnotationElement = (function LinkAnnotationElementClosure() {
this.container.className = 'linkAnnotation';

var link = document.createElement('a');
link.href = link.title = this.data.url || '';
link.href = link.title = (this.data.url ?
removeNullCharacters(this.data.url) : '');

if (this.data.url && isExternalLinkTargetSet()) {
link.target = LinkTargetStringMap[PDFJS.externalLinkTarget];
Expand Down
11 changes: 11 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,16 @@ var XRefParseException = (function XRefParseExceptionClosure() {
return XRefParseException;
})();

var NullCharactersRegExp = /\x00/g;

function removeNullCharacters(str) {
if (typeof str !== 'string') {
warn('The argument for removeNullCharacters must be a string.');
return str;
}
return str.replace(NullCharactersRegExp, '');
}
PDFJS.removeNullCharacters = removeNullCharacters;

function bytesToString(bytes) {
assert(bytes !== null && typeof bytes === 'object' &&
Expand Down Expand Up @@ -1690,6 +1700,7 @@ exports.log2 = log2;
exports.readInt8 = readInt8;
exports.readUint16 = readUint16;
exports.readUint32 = readUint32;
exports.removeNullCharacters = removeNullCharacters;
exports.shadow = shadow;
exports.string32 = string32;
exports.stringToBytes = stringToBytes;
Expand Down
14 changes: 1 addition & 13 deletions test/unit/ui_utils_spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
/* globals expect, it, describe, binarySearchFirstItem, removeNullCharacters */
/* globals expect, it, describe, binarySearchFirstItem */

'use strict';

describe('ui_utils', function() {
describe('removeNullCharacters', function() {
it('should not modify string without null characters', function() {
var str = 'string without null chars';
expect(removeNullCharacters(str)).toEqual('string without null chars');
});

it('should modify string with null characters', function() {
var str = 'string\x00With\x00Null\x00Chars';
expect(removeNullCharacters(str)).toEqual('stringWithNullChars');
});
});

describe('binary search', function() {
function isTrue(boolean) {
return boolean;
Expand Down
15 changes: 14 additions & 1 deletion test/unit/util_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* globals expect, it, describe, combineUrl, Dict, isDict, Name, PDFJS,
stringToPDFString, isExternalLinkTargetSet, LinkTarget */
stringToPDFString, isExternalLinkTargetSet, LinkTarget,
removeNullCharacters */

'use strict';

Expand Down Expand Up @@ -125,4 +126,16 @@ describe('util', function() {
// Reset the state.
PDFJS.externalLinkTarget = previousExternalLinkTarget;
});

describe('removeNullCharacters', function() {
it('should not modify string without null characters', function() {
var str = 'string without null chars';
expect(removeNullCharacters(str)).toEqual('string without null chars');
});

it('should modify string with null characters', function() {
var str = 'string\x00With\x00Null\x00Chars';
expect(removeNullCharacters(str)).toEqual('stringWithNullChars');
});
});
});
4 changes: 2 additions & 2 deletions web/pdf_attachment_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals getFileName, removeNullCharacters */
/* globals getFileName, PDFJS */

'use strict';

Expand Down Expand Up @@ -89,7 +89,7 @@ var PDFAttachmentView = (function PDFAttachmentViewClosure() {
div.className = 'attachmentsItem';
var button = document.createElement('button');
this._bindLink(button, item.content, filename);
button.textContent = removeNullCharacters(filename);
button.textContent = PDFJS.removeNullCharacters(filename);
div.appendChild(button);
this.container.appendChild(div);
}
Expand Down
4 changes: 2 additions & 2 deletions web/pdf_outline_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals removeNullCharacters */
/* globals PDFJS */

'use strict';

Expand Down Expand Up @@ -137,7 +137,7 @@ var PDFOutlineView = (function PDFOutlineViewClosure() {
div.className = 'outlineItem';
var element = document.createElement('a');
this._bindLink(element, item);
element.textContent = removeNullCharacters(item.title);
element.textContent = PDFJS.removeNullCharacters(item.title);
div.appendChild(element);

if (item.items.length > 0) {
Expand Down
6 changes: 0 additions & 6 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ var MAX_AUTO_SCALE = 1.25;
var SCROLLBAR_PADDING = 40;
var VERTICAL_PADDING = 5;

var NullCharactersRegExp = /\x00/g;

function removeNullCharacters(str) {
return str.replace(NullCharactersRegExp, '');
}

function getFileName(url) {
var anchor = url.indexOf('#');
var query = url.indexOf('?');
Expand Down