Skip to content

Commit

Permalink
[FIX] LibraryFormatter: Fix handling of paths containing special char…
Browse files Browse the repository at this point in the history
…acters

FS paths containing characters which can be interpreted as regular
expression metacharacters may lead to issues during the namespaces
detection of libraries.

Alternatively we could switch to using
https://www.npmjs.com/package/escape-string-regexp which we already use
elsewhere. But I think this case here can be kept simple.

Fixes SAP/ui5-tooling#526
  • Loading branch information
RandomByte committed Jun 23, 2021
1 parent 037b0a6 commit 0755409
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/types/library/LibraryFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,7 @@ class LibraryFormatter extends AbstractUi5Formatter {
// Remove base path from fsPath
const posixBasePath = this.getSourceBasePath(true);

// Can match /library/src as well as /library/src/some/namespace
const basePathPrefixRegExp = new RegExp(`^${posixBasePath}/?`);

if (!basePathPrefixRegExp.test(posixFsPath)) {
if (!posixFsPath.startsWith(posixBasePath)) {
if (posixBasePath === posixFsPath + "/") {
// The given file system path does not contain a namespace path
// It is equal to the source base path
Expand All @@ -289,7 +286,11 @@ class LibraryFormatter extends AbstractUi5Formatter {
throw new Error(`Given file system path ${posixFsPath} is not based on source base ` +
`path ${posixBasePath}.`);
}
const namespacePath = posixFsPath.replace(basePathPrefixRegExp, "");
let namespacePath = posixFsPath.replace(posixBasePath, "");
if (namespacePath.startsWith("/")) {
// Remove any leading slash
namespacePath = namespacePath.substr(1);
}
return namespacePath;
}

Expand Down
14 changes: 14 additions & 0 deletions test/lib/types/library/LibraryFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,20 @@ test("getNamespaceFromFsPath: fsPath is not based on base path", async (t) => {
"Threw with correct error message");
});

test("getNamespaceFromFsPath: fsPath w/ regex metacharacters", async (t) => {
const myProject = clone(libraryETree);
myProject.resources.pathMappings = {
"/resources/": myProject.resources.configuration.paths.src
};

const libraryFormatter = new LibraryFormatter({project: myProject});
sinon.stub(libraryFormatter, "getSourceBasePath").returns("/some/(path");

const fsPath = "/some/(path/my/namespace";
const res = libraryFormatter.getNamespaceFromFsPath(fsPath);
t.deepEqual(res, "my/namespace", "Returned correct namespace");
});

test.serial("getPreloadExcludesFromDotLibrary: No excludes", async (t) => {
const log = {
verbose: sinon.stub()
Expand Down

0 comments on commit 0755409

Please sign in to comment.