From 54ffc93478f77ab8e5bda8f94ff825b57071aa33 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Wed, 25 Jan 2023 09:33:19 -0800 Subject: [PATCH] [Backport 1.x] Trim trailing slashes before checking no-restricted-path rule (#3052) * Fixes `no-restricted-path` false-positives when `allowSameFolder` is true (#3020) `no-restricted-paths` compares source files and import statements, and their membership in restricted zones. However, when `allowSameFolder` is true, it failed to remove a trailing slash before validation which results in a false-positive. Signed-off-by: Miki Signed-off-by: Miki (cherry picked from commit 8732b1ca1695c9311643585f5b05270e61d248b1) Signed-off-by: github-actions[bot] # Conflicts: # CHANGELOG.md * Update tests in backport PR 3020 with correct paths (#3315) Signed-off-by: Miki Signed-off-by: Miki Co-authored-by: github-actions[bot] Co-authored-by: Anan Zhuang Co-authored-by: Miki --- .../rules/__tests__/no_restricted_paths.js | 16 ++++++++++++++++ .../rules/no_restricted_paths.js | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/osd-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js b/packages/osd-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js index bd5f719e06c1..edf05b1f02d0 100644 --- a/packages/osd-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js +++ b/packages/osd-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js @@ -86,6 +86,22 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { }, ], }, + { + code: 'import b from "files/no_restricted_paths/server/deep/deeper/e.js"', + filename: path.join(__dirname, 'files/no_restricted_paths/server/deep/d.js'), + options: [ + { + basePath: __dirname, + zones: [ + { + target: 'testfiles/**/server/**/*', + from: 'testfiles/**/server/**/*', + allowSameFolder: true, + }, + ], + }, + ], + }, // irrelevant function calls { diff --git a/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.js b/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.js index e6139d29d592..3a54dfe0f934 100644 --- a/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.js +++ b/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.js @@ -47,7 +47,7 @@ function traverseToTopFolder(src, pattern) { const srcIdx = src.lastIndexOf(path.sep); src = src.slice(0, srcIdx); } - return src.replace(/\\/g, '/'); + return src.replace(/\\/g, '/').replace(/\/$/, ''); } function isSameFolderOrDescendent(src, imported, pattern) {