Skip to content

Commit

Permalink
Prohibit importing from other module 'internal' dirs. (PolymerLabs#5999)
Browse files Browse the repository at this point in the history
Plus a couple of small fixes to sigh and tsconfig.
  • Loading branch information
mykmartin authored Aug 31, 2020
1 parent 2e889a8 commit 1d40e34
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
8 changes: 8 additions & 0 deletions tools/custom-loader.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import url from 'url';
import path from 'path';

export function resolve(specifier, parent, resolve) {
if (!/^[./]/.test(specifier)) {
Expand All @@ -12,6 +13,13 @@ export function resolve(specifier, parent, resolve) {
}
}
}
if (parent && parent.startsWith('file:///') && specifier.includes('/internal/')) {
const parentDir = path.dirname(parent.substr(7)); // 7 == len('file://')
const target = path.resolve(parentDir, specifier);
if (!target.startsWith(parentDir)) {
throw new Error(`cannot access internal file '${specifier}' from external location ${parent}`);
}
}
if (!/\.(js|mjs)$/.test(specifier)) {
if (/build/.test(parent)) {
const resolved = new url.URL(specifier, parent || 'file:///');
Expand Down
2 changes: 1 addition & 1 deletion tools/sigh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ function buildPath(path: string, preprocess: () => void): () => boolean {
return false;
}

if (!link(findProjectFiles('src', null, /\.js$/))) {
if (!link(findProjectFiles('src', /webpack\.config\.js$/, /\.js$/))) {
console.error('build::link failed');
return false;
}
Expand Down
6 changes: 2 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
"noErrorTruncation": true
},
"exclude": [
"build/**/*",
"cloud/**/*",
"./src/tools/language-server/**/*",
"./src/tools/vscode-language-client/**/*"
"src/tools/language-server/**/*",
"src/tools/vscode-language-client/**/*"
]
}

0 comments on commit 1d40e34

Please sign in to comment.