Skip to content
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/*!
* jQuery Form Plugin
* version: 3.51.0-2014.06.20
* version: 3.51.0.LIFERAY-PATCHED-ISSUE-586
* Requires jQuery v1.5 or later
* Copyright (c) 2014 M. Alsup
* Examples and documentation at: http://malsup.com/jquery/form/
Expand Down Expand Up @@ -243,6 +243,14 @@
var oldSuccess = options.success || function () {};
callbacks.push(function (data) {
var fn = options.replaceTarget ? 'replaceWith' : 'html';

// Validate `data` through `HTML encoding` when passed `data` is passed
// to `html()`, as suggested in https://github.com/jquery-form/form/issues/464

fn == 'html'
? (data = $.parseHTML($('<div>').text(data).html()))
: '';
Copy link

@jbalsas jbalsas Jan 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a while to understand what this was doing and figure out it's using the ternary conditional to mutate data and to do that we use the previous value of fn... for a while it felt like you might be mutating fn. I think we should be able to write this in a simpler way doing something like:

var fn = options.replaceTarget ? 'replaceWith' : 'html';

data = options.replaceTarget ? data : $.parseHTML(...);

This way it's obvious that data is being mutated and it also depends on the same condition rather than on the evaluation of the first one.

Also, I know this is the original solution and this feels like idiomatic jQuery, but is the .parseHTML really necessary? isn't it enough with $('<div>').text(data).html()?

I think this translates to the vanilla version which is:

function sanitizeHTML(data) {
    const div = document.createElement('div');

    div.textContent = data;

    return div.innerHTML;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather keep the .parseHTML to respect the original solution and simply as I am not sure if it would cause any security issue or not

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sent a refactor commit, besides the removal of .parseHTML I followed up on everything. Thanks


$(options.target)[fn](data).each(oldSuccess, arguments);
});
}
Expand Down Expand Up @@ -1076,8 +1084,12 @@
var parseJSON =
$.parseJSON ||
function (s) {
/*jslint evil:true */
return window['eval']('(' + s + ')');

// Arise an error resolvable including jquery instead of
// making a new function using unsanitized inputs

window.console.error('jquery.parseJSON is undefined');
return null;
};

var httpData = function (xhr, type, s) {
Expand Down