Skip to content

Commit

Permalink
[7.17] Fix eslint rule for restricting certain lodash imports (#151023)…
Browse files Browse the repository at this point in the history
… (#151550)

# Backport

This will backport the following commits from `main` to `7.17`:
- [Fix eslint rule for restricting certain lodash imports
(#151023)](#151023)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Thomas
Watson","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-02-16T15:35:09Z","message":"Fix
eslint rule for restricting certain lodash imports (#151023)\n\nFixes
#110422\r\n\r\nTL;DR: The `lodash.set` function is unsafe and shouldn't
be called.\r\n\r\nCause of error: If you specify multiple
`no-restricted-imports` paths\r\nfor the same module, only the last path
is used. Instead you need to\r\ncombine them into a single path as I've
done in this PR.\r\n\r\nThis regression was introduced in
#100277","sha":"fbdeffb48fcc6c23ded1e84e62d3f33dc84de23d","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:APM","Team:uptime","release_note:skip","backport:all-open","v8.8.0"],"number":151023,"url":"https://github.com/elastic/kibana/pull/151023","mergeCommit":{"message":"Fix
eslint rule for restricting certain lodash imports (#151023)\n\nFixes
#110422\r\n\r\nTL;DR: The `lodash.set` function is unsafe and shouldn't
be called.\r\n\r\nCause of error: If you specify multiple
`no-restricted-imports` paths\r\nfor the same module, only the last path
is used. Instead you need to\r\ncombine them into a single path as I've
done in this PR.\r\n\r\nThis regression was introduced in
#100277","sha":"fbdeffb48fcc6c23ded1e84e62d3f33dc84de23d"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/151023","number":151023,"mergeCommit":{"message":"Fix
eslint rule for restricting certain lodash imports (#151023)\n\nFixes
#110422\r\n\r\nTL;DR: The `lodash.set` function is unsafe and shouldn't
be called.\r\n\r\nCause of error: If you specify multiple
`no-restricted-imports` paths\r\nfor the same module, only the last path
is used. Instead you need to\r\ncombine them into a single path as I've
done in this PR.\r\n\r\nThis regression was introduced in
#100277","sha":"fbdeffb48fcc6c23ded1e84e62d3f33dc84de23d"}}]}]
BACKPORT-->
  • Loading branch information
Thomas Watson authored Feb 17, 2023
1 parent f82bbe3 commit 1150312
Show file tree
Hide file tree
Showing 20 changed files with 109 additions and 100 deletions.
159 changes: 78 additions & 81 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,50 +170,49 @@ const DEV_PATTERNS = [
const RESTRICTED_IMPORTS = [
{
name: 'lodash',
importNames: ['set', 'setWith'],
message: 'Please use @elastic/safer-lodash-set instead',
importNames: ['set', 'setWith', 'template'],
message:
'lodash.set/setWith: Please use @elastic/safer-lodash-set instead.\n' +
'lodash.template: Function is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash.set',
message: 'Please use @elastic/safer-lodash-set instead',
message: 'Please use @elastic/safer-lodash-set/set instead',
},
{
name: 'lodash.setwith',
message: 'Please use @elastic/safer-lodash-set instead',
message: 'Please use @elastic/safer-lodash-set/setWith instead',
},
{
name: 'lodash/set',
message: 'Please use @elastic/safer-lodash-set instead',
message: 'Please use @elastic/safer-lodash-set/set instead',
},
{
name: 'lodash/setWith',
message: 'Please use @elastic/safer-lodash-set instead',
message: 'Please use @elastic/safer-lodash-set/setWith instead',
},
{
name: 'lodash/fp',
importNames: ['set', 'setWith', 'assoc', 'assocPath'],
message: 'Please use @elastic/safer-lodash-set instead',
importNames: ['set', 'setWith', 'assoc', 'assocPath', 'template'],
message:
'lodash.set/setWith/assoc/assocPath: Please use @elastic/safer-lodash-set/fp instead\n' +
'lodash.template: Function is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash/fp/set',
message: 'Please use @elastic/safer-lodash-set instead',
message: 'Please use @elastic/safer-lodash-set/fp/set instead',
},
{
name: 'lodash/fp/setWith',
message: 'Please use @elastic/safer-lodash-set instead',
message: 'Please use @elastic/safer-lodash-set/fp/setWith instead',
},
{
name: 'lodash/fp/assoc',
message: 'Please use @elastic/safer-lodash-set instead',
message: 'Please use @elastic/safer-lodash-set/fp/assoc instead',
},
{
name: 'lodash/fp/assocPath',
message: 'Please use @elastic/safer-lodash-set instead',
},
{
name: 'lodash',
importNames: ['template'],
message: 'lodash.template is unsafe, and not compatible with our content security policy.',
message: 'Please use @elastic/safer-lodash-set/fp/assocPath instead',
},
{
name: 'lodash.template',
Expand All @@ -223,11 +222,6 @@ const RESTRICTED_IMPORTS = [
name: 'lodash/template',
message: 'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash/fp',
importNames: ['template'],
message: 'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash/fp/template',
message: 'lodash.template is unsafe, and not compatible with our content security policy.',
Expand Down Expand Up @@ -787,47 +781,54 @@ module.exports = {
{
files: ['**/*.{js,mjs,ts,tsx}'],
rules: {
'no-restricted-imports': [
2,
'no-restricted-imports': ['error', ...RESTRICTED_IMPORTS],
'no-restricted-modules': [
'error',
{
paths: RESTRICTED_IMPORTS,
name: 'lodash.set',
message: 'Please use @elastic/safer-lodash-set instead',
},
],
'no-restricted-modules': [
2,
{
paths: [
{
name: 'lodash.set',
message: 'Please use @elastic/safer-lodash-set instead',
},
{
name: 'lodash.setwith',
message: 'Please use @elastic/safer-lodash-set instead',
},
{
name: 'lodash.template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash/set',
message: 'Please use @elastic/safer-lodash-set instead',
},
{
name: 'lodash/setWith',
message: 'Please use @elastic/safer-lodash-set instead',
},
{
name: 'lodash/template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
],
name: 'lodash.setwith',
message: 'Please use @elastic/safer-lodash-set instead',
},
{
name: 'lodash.template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
name: 'lodash/set',
message: 'Please use @elastic/safer-lodash-set/set instead',
},
{
name: 'lodash/setWith',
message: 'Please use @elastic/safer-lodash-set/setWith instead',
},
{
name: 'lodash/fp/set',
message: 'Please use @elastic/safer-lodash-set/fp/set instead',
},
{
name: 'lodash/fp/setWith',
message: 'Please use @elastic/safer-lodash-set/fp/setWith instead',
},
{
name: 'lodash/fp/assoc',
message: 'Please use @elastic/safer-lodash-set/fp/assoc instead',
},
{
name: 'lodash/fp/assocPath',
message: 'Please use @elastic/safer-lodash-set/fp/assocPath instead',
},
{
name: 'lodash/template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
],
'no-restricted-properties': [
2,
'error',
{
object: 'lodash',
property: 'set',
Expand All @@ -838,18 +839,6 @@ module.exports = {
property: 'set',
message: 'Please use @elastic/safer-lodash-set instead',
},
{
object: 'lodash',
property: 'template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
object: '_',
property: 'template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
object: 'lodash',
property: 'setWith',
Expand Down Expand Up @@ -880,26 +869,34 @@ module.exports = {
property: 'assocPath',
message: 'Please use @elastic/safer-lodash-set instead',
},
{
object: 'lodash',
property: 'template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
object: '_',
property: 'template',
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
],
},
},
{
files: ['**/common/**/*.{js,mjs,ts,tsx}', '**/public/**/*.{js,mjs,ts,tsx}'],
rules: {
'no-restricted-imports': [
2,
'error',
...RESTRICTED_IMPORTS,
{
paths: [
...RESTRICTED_IMPORTS,
{
name: 'semver',
message: 'Please use "semver/*/{function}" instead',
},
{
name: '@kbn/rule-data-utils',
message: `Import directly from @kbn/rule-data-utils/* submodules in public/common code`,
},
],
name: 'semver',
message: 'Please use "semver/*/{function}" instead',
},
{
name: '@kbn/rule-data-utils',
message: `Import directly from @kbn/rule-data-utils/* submodules in public/common code`,
},
],
},
Expand Down
2 changes: 2 additions & 0 deletions packages/elastic-apm-synthtrace/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ NPM_MODULE_EXTRA_FILES = [

RUNTIME_DEPS = [
"//packages/elastic-datemath",
"//packages/elastic-safer-lodash-set",
"@npm//@elastic/elasticsearch",
"@npm//lodash",
"@npm//moment",
Expand All @@ -38,6 +39,7 @@ RUNTIME_DEPS = [

TYPES_DEPS = [
"//packages/elastic-datemath:npm_module_types",
"//packages/elastic-safer-lodash-set",
"@npm//@elastic/elasticsearch",
"@npm//moment",
"@npm//p-limit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { set } from 'lodash';
import { set } from '@elastic/safer-lodash-set';
import { getObserverDefaults } from '../..';
import { Fields } from '../entity';

Expand Down
2 changes: 1 addition & 1 deletion packages/elastic-safer-lodash-set/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Lodash v4.x.
## Example Usage

```js
const { set } = require('@elastic/safer-loadsh-set');
const { set } = require('@elastic/safer-lodash-set');

const object = { a: [{ b: { c: 3 } }] };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { set } from 'lodash';
import { set } from '@elastic/safer-lodash-set';
import {
buildInlineScriptForPhraseFilter,
buildPhraseFilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { setMockValues } from '../../../../__mocks__/kea_logic';
import React from 'react';

import { shallow } from 'enzyme';
import { set } from 'lodash/fp';

import { set } from '@elastic/safer-lodash-set/fp';

import { SuggestionsCallout } from '../components/suggestions_callout';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import React from 'react';

import { shallow } from 'enzyme';

import { set } from 'lodash/fp';

import { EuiTab } from '@elastic/eui';
import { set } from '@elastic/safer-lodash-set/fp';

import { getPageHeaderTabs, getPageTitle } from '../../../../test_helpers';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import '../../../__mocks__/engine_logic.mock';
import React from 'react';

import { shallow } from 'enzyme';
import { set } from 'lodash/fp';

import { set } from '@elastic/safer-lodash-set/fp';

import { CurationsTable, EmptyState } from '../components';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { setMockValues } from '../../../../__mocks__/kea_logic';
import React from 'react';

import { shallow } from 'enzyme';
import { set } from 'lodash/fp';

import { set } from '@elastic/safer-lodash-set/fp';

import { SuggestionsCallout } from '../../curations/components/suggestions_callout';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import moment from 'moment';
import { set, unset } from 'lodash';
import { set } from '@elastic/safer-lodash-set';
import { unset } from 'lodash';
import { STANDALONE_CLUSTER_CLUSTER_UUID } from '../../../common/constants';
import { handleResponse, getNodeInfo } from './get_node_info';
import { LegacyRequest } from '../../types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import { pickBy, get, isEmpty, isString, unset, set, intersection } from 'lodash';
import { set } from '@elastic/safer-lodash-set';
import { pickBy, get, isEmpty, isString, unset, intersection } from 'lodash';
import satisfies from 'semver/functions/satisfies';
import {
EuiFlexGroup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import moment from 'moment-timezone';
import { has, mapKeys, set, unset, find } from 'lodash';
import { set } from '@elastic/safer-lodash-set';
import { has, mapKeys, unset, find } from 'lodash';
import { schema } from '@kbn/config-schema';
import { produce } from 'immer';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import moment from 'moment-timezone';
import { set, unset, has, difference, filter, find, map, mapKeys, uniq } from 'lodash';
import { set } from '@elastic/safer-lodash-set';
import { unset, has, difference, filter, find, map, mapKeys, uniq } from 'lodash';
import { schema } from '@kbn/config-schema';
import { produce } from 'immer';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import { produce } from 'immer';
import { satisfies } from 'semver';
import { filter, reduce, mapKeys, each, set, unset, uniq, map, has } from 'lodash';
import { set } from '@elastic/safer-lodash-set';
import { filter, reduce, mapKeys, each, unset, uniq, map, has } from 'lodash';
import { packSavedObjectType } from '../../../common/types';
import {
PACKAGE_POLICY_SAVED_OBJECT_TYPE,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/reporting/server/lib/content_stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { set } from 'lodash';
import { set } from '@elastic/safer-lodash-set';
import { elasticsearchServiceMock } from 'src/core/server/mocks';
import { createMockLevelLogger } from '../test_helpers';
import { ContentStream } from './content_stream';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { set } from 'lodash';
import { set } from '@elastic/safer-lodash-set';
import { durationToNumber } from '../../../common/schema_utils';
import { HeadlessChromiumDriver } from '../../browsers';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { UnwrapPromise } from '@kbn/utility-types';
import { set } from 'lodash';
import { set } from '@elastic/safer-lodash-set';
import { ElasticsearchClient } from 'src/core/server';
import { statuses } from '../../lib';
import { createMockConfigSchema, createMockReportingCore } from '../../test_helpers';
Expand Down
Loading

0 comments on commit 1150312

Please sign in to comment.