@suzukihayate/safe-html 0.1.0
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/LICENSE +21 -0
- package/README.md +58 -0
- package/index.d.ts +27 -0
- package/index.js +0 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hayate Suzuki (鈴木颯)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# safe-html
|
|
2
|
+
|
|
3
|
+
A tiny, **zero-dependency** tagged-template that **auto-escapes by context** (HTML text / attribute / URL). Build HTML strings safely without a framework.
|
|
4
|
+
|
|
5
|
+
文脈(テキスト・属性・URL)を理解して**自動でエスケープ**する、依存ゼロのタグ付きテンプレートです。フレームワークなしでXSSを防げます。
|
|
6
|
+
|
|
7
|
+
- Text position → HTML-escaped
|
|
8
|
+
- Attribute value → attribute-escaped
|
|
9
|
+
- URL attributes (`href`, `src`, …) → dangerous schemes like `javascript:` are neutralized
|
|
10
|
+
- `SafeString` values are **not double-escaped**, so fragments compose
|
|
11
|
+
- Interpolating into a tag name or attribute name is **rejected** (safety)
|
|
12
|
+
- Zero dependencies, typed, ESM — works in Node / Deno / Bun / browser
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
```bash
|
|
16
|
+
npm install @suzukihayate/safe-html
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
```js
|
|
21
|
+
import { html, raw } from '@suzukihayate/safe-html';
|
|
22
|
+
|
|
23
|
+
html`<p>${'<script>alert(1)</script>'}</p>`.toString();
|
|
24
|
+
// "<p><script>alert(1)</script></p>"
|
|
25
|
+
|
|
26
|
+
html`<a href="${'javascript:alert(1)'}">x</a>`.toString();
|
|
27
|
+
// '<a href="#">x</a>' (neutralized)
|
|
28
|
+
|
|
29
|
+
html`<a href="${'https://example.com/?a=1&b=2'}">ok</a>`.toString();
|
|
30
|
+
// '<a href="https://example.com/?a=1&b=2">ok</a>'
|
|
31
|
+
|
|
32
|
+
// Compose fragments without double-escaping
|
|
33
|
+
const items = ['a<', 'b&'].map((t) => html`<li>${t}</li>`);
|
|
34
|
+
html`<ul>${items}</ul>`.toString();
|
|
35
|
+
// '<ul><li>a<</li><li>b&</li></ul>'
|
|
36
|
+
|
|
37
|
+
// Opt-in raw (trusted strings only)
|
|
38
|
+
html`<p>${raw('<br>')}</p>`.toString(); // '<p><br></p>'
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API
|
|
42
|
+
- `` html`...` `` — context-aware auto-escaping tagged template, returns a `SafeString`
|
|
43
|
+
- `raw(str)` — wrap a trusted string (no escaping)
|
|
44
|
+
- `escapeHtml(str)` / `safeUrl(str)` — the underlying helpers
|
|
45
|
+
- `SafeString` — branded safe-HTML wrapper (`.toString()`)
|
|
46
|
+
|
|
47
|
+
## Why
|
|
48
|
+
Most escaping helpers escape the same way everywhere, so developers must remember which context they are in (and `href="javascript:..."` slips through plain HTML escaping). `safe-html` tracks the parsing context across the static template parts and escapes each interpolation correctly — including neutralizing dangerous URL schemes — in a few hundred bytes.
|
|
49
|
+
|
|
50
|
+
> Note: assumes reasonably well-formed templates (double/single-quoted or unquoted attribute values). For full untrusted-HTML sanitization, use a dedicated sanitizer.
|
|
51
|
+
|
|
52
|
+
## Test
|
|
53
|
+
```bash
|
|
54
|
+
node --test
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
MIT © 2026 Hayate Suzuki
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** 安全と分かっている HTML 文字列のラッパー。 */
|
|
2
|
+
export class SafeString {
|
|
3
|
+
constructor(value: string);
|
|
4
|
+
value: string;
|
|
5
|
+
toString(): string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** HTML テキストのエスケープ。 */
|
|
9
|
+
export function escapeHtml(s: string): string;
|
|
10
|
+
|
|
11
|
+
/** URL として危険なスキーム(javascript: 等)を無害化する。 */
|
|
12
|
+
export function safeUrl(value: string): string;
|
|
13
|
+
|
|
14
|
+
/** 文脈(テキスト / 属性 / URL)に応じて自動エスケープするタグ付きテンプレート。 */
|
|
15
|
+
export function html(strings: TemplateStringsArray, ...values: unknown[]): SafeString;
|
|
16
|
+
|
|
17
|
+
/** エスケープせずそのまま埋め込む(信頼できる文字列のみ)。 */
|
|
18
|
+
export function raw(value: string): SafeString;
|
|
19
|
+
|
|
20
|
+
declare const _default: {
|
|
21
|
+
html: typeof html;
|
|
22
|
+
raw: typeof raw;
|
|
23
|
+
SafeString: typeof SafeString;
|
|
24
|
+
escapeHtml: typeof escapeHtml;
|
|
25
|
+
safeUrl: typeof safeUrl;
|
|
26
|
+
};
|
|
27
|
+
export default _default;
|
package/index.js
ADDED
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@suzukihayate/safe-html",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "文脈(HTML/属性/URL)を理解して自動エスケープする、依存ゼロのタグ付きテンプレート。フレームワーク不要でXSSを防ぐ。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"default": "./index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["index.js", "index.d.ts", "README.md", "LICENSE"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "node --test"
|
|
17
|
+
},
|
|
18
|
+
"keywords": ["html", "escape", "xss", "template-literal", "tagged-template", "sanitize", "security", "contextual", "zero-dependency"],
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/HaYaTedonn/safe-html.git"
|
|
22
|
+
},
|
|
23
|
+
"author": "Hayate Suzuki",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"engines": { "node": ">=18" },
|
|
26
|
+
"sideEffects": false
|
|
27
|
+
}
|