ses 0.18.8 → 0.18.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ses",
3
- "version": "0.18.8",
3
+ "version": "0.18.9",
4
4
  "description": "Hardened JavaScript for Fearless Cooperation",
5
5
  "keywords": [
6
6
  "lockdown",
@@ -179,5 +179,5 @@
179
179
  ],
180
180
  "timeout": "2m"
181
181
  },
182
- "gitHead": "9c779d317c4b02133172dbe142c5b2d1727efc49"
182
+ "gitHead": "5da0bcae87b92be185ab3569cf117f8e01b6ca39"
183
183
  }
package/src/transforms.js CHANGED
@@ -3,6 +3,7 @@
3
3
  import {
4
4
  FERAL_REG_EXP,
5
5
  SyntaxError,
6
+ regexpExec,
6
7
  stringReplace,
7
8
  stringSearch,
8
9
  stringSlice,
@@ -106,10 +107,78 @@ export const evadeHtmlCommentTest = src => {
106
107
  // /////////////////////////////////////////////////////////////////////////////
107
108
 
108
109
  const importPattern = new FERAL_REG_EXP(
109
- '(^|[^.]|\\.\\.\\.)\\bimport(\\s*(?:\\(|/[/*]))',
110
+ '(^|[^.]|\\.\\.\\.)\\bimport(\\s*(?:\\(|\\.|/[/*]))',
110
111
  'g',
111
112
  );
112
113
 
114
+ /**
115
+ * Matches one line of a run of `//# sourceURL=` and `//# sourceMappingURL=`
116
+ * comments at the very end of the source (`sourceMetaTrailerStart` finds
117
+ * the run), as module-source, compartment-mapper and
118
+ * bundlers append to evaluated code (a module's location may well contain
119
+ * `import.`, as in `conditional-import.js`).
120
+ *
121
+ * Each line must begin at a line start and its value must hold no whitespace
122
+ * and none of `*`, backtick, `$`, quotes or backslash. Such a line is inert
123
+ * wherever it stands: outside any literal it is a line comment; a regular
124
+ * expression literal cannot span lines; and inside a block comment, a string
125
+ * continued from the line before or a template literal, the line can neither
126
+ * close that context (no `*`, quote or backtick) nor open a substitution (no
127
+ * `$`). Since nothing but whitespace follows the run, an unclosed context
128
+ * there is a SyntaxError. So `rejectImportExpressions` need not scan it.
129
+ *
130
+ * A `/*# sourceMappingURL=` block comment is not skipped but scanned like
131
+ * any other code, which can only make the check stricter.
132
+ */
133
+ const sourceMetaLinePattern = new FERAL_REG_EXP(
134
+ '^//# source(?:Mapping)?URL=[^\\s*`$\'"\\\\]*$',
135
+ );
136
+
137
+ const whitespacePattern = new FERAL_REG_EXP('^\\s$');
138
+
139
+ const lineTerminatorPattern = new FERAL_REG_EXP('^[\\n\\r\\u2028\\u2029]$');
140
+
141
+ /**
142
+ * Returns the index at which the trailing run of source meta comments
143
+ * begins, at its leading line terminator if it has one, or the length of
144
+ * the source if there is no such run. It reads the source backward, one
145
+ * line at a time, and tests each line with `sourceMetaLinePattern` alone,
146
+ * so it takes time linear in the length of the source. A single regular
147
+ * expression for the whole run, `(?:(?:^|[\n\r\u2028\u2029])<line>)+\s*$`,
148
+ * finds the same run, but a search retries it at every line terminator,
149
+ * which takes time quadratic in the number of lines of the run.
150
+ *
151
+ * @param {string} src
152
+ * @returns {number}
153
+ */
154
+ const sourceMetaTrailerStart = src => {
155
+ let end = src.length;
156
+ while (end > 0 && regexpExec(whitespacePattern, src[end - 1]) !== null) {
157
+ end -= 1;
158
+ }
159
+ let start = src.length;
160
+ for (;;) {
161
+ let lineStart = end;
162
+ while (
163
+ lineStart > 0 &&
164
+ regexpExec(lineTerminatorPattern, src[lineStart - 1]) === null
165
+ ) {
166
+ lineStart -= 1;
167
+ }
168
+ if (
169
+ regexpExec(sourceMetaLinePattern, stringSlice(src, lineStart, end)) ===
170
+ null
171
+ ) {
172
+ return start;
173
+ }
174
+ if (lineStart === 0) {
175
+ return 0;
176
+ }
177
+ start = lineStart - 1;
178
+ end = lineStart - 1;
179
+ }
180
+ };
181
+
113
182
  /**
114
183
  * Conservatively reject the source text if it may contain a dynamic
115
184
  * import expression. To reject without parsing, `rejectImportExpressions` will
@@ -125,10 +194,23 @@ const importPattern = new FERAL_REG_EXP(
125
194
  * apparent import expressions in literal strings or comments.
126
195
  *
127
196
  * The current conservative rule looks for the identifier "import"
128
- * followed by either an open paren or something that looks like the
129
- * beginning of a comment. We assume that we do not need to worry
130
- * about html comment syntax because that was already rejected by
131
- * rejectHtmlComments.
197
+ * followed by either an open paren, a dot, or something that looks like
198
+ * the beginning of a comment. The dot case rejects not just a
199
+ * phase-qualified call such as `import.source(...)` or `import.defer(...)`,
200
+ * but any property access under `import` at all, including `import.meta`,
201
+ * regardless of whether the property is then called. This is broader than
202
+ * strictly necessary: the substring "import." can occur incidentally, for
203
+ * example in a module path or a comment, and such source text is rejected
204
+ * too. That tradeoff is intentional: covering every `import.<name>` form,
205
+ * called or not, without naming specific property names, is judged simpler
206
+ * and safer than narrowing the match to the shape of a call. We assume that
207
+ * we do not need to worry about html comment syntax because that was
208
+ * already rejected by rejectHtmlComments.
209
+ *
210
+ * The scan stops short of a trailing run of `//# sourceURL=` or
211
+ * `//# sourceMappingURL=` comments whose values could not end a comment,
212
+ * string or template (see `sourceMetaTrailerStart`), so that a module
213
+ * whose location contains `import.` still loads.
132
214
  *
133
215
  * this \s *must* match all kinds of syntax-defined whitespace. If e.g.
134
216
  * U+2028 (LINE SEPARATOR) or U+2029 (PARAGRAPH SEPARATOR) is treated as
@@ -141,7 +223,8 @@ const importPattern = new FERAL_REG_EXP(
141
223
  * @returns {string}
142
224
  */
143
225
  export const rejectImportExpressions = src => {
144
- const lineNumber = getLineNumber(src, importPattern);
226
+ const scanned = stringSlice(src, 0, sourceMetaTrailerStart(src));
227
+ const lineNumber = getLineNumber(scanned, importPattern);
145
228
  if (lineNumber < 0) {
146
229
  return src;
147
230
  }