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

Patch lodash template #64985

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/setup_node_env/harden.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ var hook = require('require-in-the-middle');
hook(['child_process'], function(exports, name) {
return require(`./patches/${name}`)(exports); // eslint-disable-line import/no-dynamic-require
});

hook(['lodash'], function(exports, name) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to set up a new hook, instead you just add lodash to the array up above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Awesome, thanks Thomas!

return require(`./patches/${name}`)(exports); // eslint-disable-line import/no-dynamic-require
});
39 changes: 39 additions & 0 deletions src/setup_node_env/patches/lodash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

module.exports = function(lodash) {
lodash.template = new Proxy(lodash.template, {
apply: function(target, thisArg, args) {
var options;
if (args.length === 1) {
options = {
sourceURL: '',
};
} else {
options = { ...args[1] };
options.sourceURL = (options.sourceURL + '').replace(/\s/g, ' ');
}

args[1] = options;
return target.apply(thisArg, args);
},
});

return lodash;
};
93 changes: 93 additions & 0 deletions test/harden/lodash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

require('../../src/setup_node_env');
const _ = require('elasticsearch/node_modules/lodash'); // our fork has been patched already, using a non-patched version
const test = require('tape');

Object.prototype.sourceURL = '\u2028\u2029\n;global.whoops=true'; // eslint-disable-line no-extend-native

test.onFinish(() => {
delete Object.prototype.sourceURL;
});

test('test setup ok', t => {
t.equal({}.sourceURL, '\u2028\u2029\n;global.whoops=true');
t.end();
});

test(`_.template('<%= foo %>')`, t => {
const output = _.template('<%= foo %>')({ foo: 'bar' });
t.equal(output, 'bar');
t.equal(global.whoops, undefined);
t.end();
});

Copy link
Member

Choose a reason for hiding this comment

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

Do you think it's worthwhile to test lodash with the fourth guard parameter provided? In other words, testing that calling _.template as an iteree of _.map still works as expected? I only ask because there is special handling today to clear out the provided options:

https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L14776-L14778

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do... I need to give the guard parameter more attention. I came across it on Friday, grew frustrated, and moved on...

test(`_.template('<%= foo %>', {})`, t => {
const output = _.template('<%= foo %>', Object.freeze({}))({ foo: 'bar' });
t.equal(output, 'bar');
t.equal(global.whoops, undefined);
t.end();
});

test(`_.template('<%= data.foo %>', { variable: 'data' })`, t => {
const output = _.template('<%= data.foo %>', Object.freeze({ variable: 'data' }))({ foo: 'bar' });
t.equal(output, 'bar');
t.equal(global.whoops, undefined);
t.end();
});

test(`_.template('<%= foo %>', { sourceURL: '/foo/bar' })`, t => {
// throwing errors in the template and parsing the stack, which is a string, is super ugly, but all I know to do
const template = _.template('<% throw new Error() %>', Object.freeze({ sourceURL: '/foo/bar' }));
t.plan(2);
try {
template();
} catch (err) {
const path = parsePathFromStack(err.stack);
t.equal(path, '/foo/bar');
t.equal(global.whoops, undefined);
}
});

test(`_.template('<%= foo %>', { sourceURL: '\\u2028\\u2029\\nglobal.whoops=true' })`, t => {
// throwing errors in the template and parsing the stack, which is a string, is super ugly, but all I know to do
const template = _.template(
'<% throw new Error() %>',
Object.freeze({ sourceURL: '\u2028\u2029\nglobal.whoops=true' })
);
t.plan(2);
try {
template();
} catch (err) {
const path = parsePathFromStack(err.stack);
t.equal(path, 'global.whoops=true');
t.equal(global.whoops, undefined);
}
});

function parsePathFromStack(stack) {
const lines = stack.split('\n');
// the frame starts at the second line
const frame = lines[1];

// the path is in parathensis, and ends with a colon before the line/column numbers
const [, path] = /\(([^:]+)/.exec(frame);
return path;
}