Skip to content

Commit 7a68c17

Browse files
authored
Verify that importer 'contents' are a string-type (#167)
Implements verification added on sass/dart-sass#1816 See sass/sass-spec#1838
1 parent 770ce47 commit 7a68c17

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

lib/src/importer-registry.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
110110
return thenOr(importer.load(new URL(request.getUrl())), result => {
111111
const proto = new InboundMessage.ImportResponse();
112112
if (result) {
113+
if (typeof result.contents !== 'string') {
114+
throw Error(
115+
`Invalid argument (contents): must be a string but was: ${
116+
(result.contents as {}).constructor.name
117+
}`
118+
);
119+
}
120+
121+
if (result.sourceMapUrl && !result.sourceMapUrl.protocol) {
122+
throw Error(
123+
'Invalid argument (sourceMapUrl): must be absolute but was: ' +
124+
result.sourceMapUrl
125+
);
126+
}
127+
113128
const success = new InboundMessage.ImportResponse.ImportSuccess();
114129
success.setContents(result.contents);
115130
success.setSyntax(utils.protofySyntax(result.syntax));

lib/src/legacy/importer.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,22 @@ export class LegacyImporterWrapper<sync extends 'sync' | 'async'>
260260
thenOr(
261261
this.invokeCallback(this.callbacks[n], self, url, prev),
262262
result => {
263-
if (result !== null) return result;
264-
if (n === this.callbacks.length - 1) return null;
265-
return invokeNthCallback(n + 1);
263+
if (result === null) {
264+
if (n === this.callbacks.length - 1) return null;
265+
return invokeNthCallback(n + 1);
266+
}
267+
if (
268+
'contents' in result &&
269+
result.contents &&
270+
typeof result.contents !== 'string'
271+
) {
272+
throw new Error(
273+
`Invalid argument (contents): must be a string but was: ${
274+
(result.contents as {}).constructor.name
275+
}`
276+
);
277+
}
278+
return result;
266279
}
267280
);
268281

0 commit comments

Comments
 (0)