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