ses 1.15.0 → 1.15.1

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