url-sanitizer 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 asamuzaK (Kazz)
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,191 @@
1
+ # urlSanitizer
2
+
3
+ [![build](https://github.com/asamuzaK/urlSanitizer/workflows/build/badge.svg)](https://github.com/asamuzaK/urlSanitizer/actions?query=workflow%3Abuild)
4
+ [![CodeQL](https://github.com/asamuzaK/urlSanitizer/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/asamuzaK/urlSanitizer/actions?query=workflow%3Agithub-code-scanning%3Acodeql)
5
+ [![npm package](https://badge.fury.io/js/url-sanitizer.svg)](https://badge.fury.io/js/url-sanitizer)
6
+
7
+ URL sanitizer for Node.js (>=18), browsers and web sites.
8
+ **Experimental**
9
+
10
+ ## Install
11
+
12
+ ```shell
13
+ npm i url-sanitizer
14
+ ```
15
+
16
+ For browsers and web sites, standalone esm builds are available in `dist/` directory.
17
+ * node_modules/url-sanitizer/dist/url-sanitizer.js
18
+ * node_modules/url-sanitizer/dist/url-sanitizer.min.js
19
+
20
+ <!--
21
+ Or, download directly from unpkg.com
22
+ *
23
+ -->
24
+
25
+ ## Usage
26
+
27
+ ```javascript
28
+ import urlSanitizer, {
29
+ isURI, isURISync, sanitizeURL, sanitizeURLSync
30
+ } from 'url-sanitizer';
31
+ ```
32
+
33
+ ## sanitizeURL(url, opt)
34
+
35
+ Sanitize the given URL.
36
+ * `data` and/or `file` schemes must be explicitly allowed.
37
+ * `javascript` and/or `vbscript` schemes can not be allowed.
38
+
39
+ ### Parameters
40
+
41
+ * `url` **[string][1]** URL input
42
+ * `opt` **[object][4]** options
43
+
44
+ Returns **[Promise][5]<[string][1]?>** sanitized URL, `null`able
45
+
46
+ ```javascript
47
+ const res1 = await sanitizeURL('http://example.com/?<script>alert(1);</script>')
48
+ .then(res => decodeURIComponent(res));
49
+ // -> 'http://example.com/?&lt;script&gt;alert(1);&lt;/script&gt;'
50
+
51
+ const res2 = await sanitizeURL('data:text/html,<script>alert(1);</script>', {
52
+ allow: ['data']
53
+ }).then(res => decodeURIComponent(res));
54
+ // -> 'data:text/html,&lt;script&gt;alert(1);&lt;/script&gt;'
55
+
56
+ // Can parse and sanitize base64 encoded data
57
+ const base64data3 = btoa('<script>alert(1);</script>');
58
+ const res3 = await sanitizeURL(`data:text/html;base64,${base64data3}`, {
59
+ allow: ['data']
60
+ }).then(res => decodeURIComponent(res));
61
+ // -> 'data:text/html,&lt;script&gt;alert(1);&lt;/script&gt;'
62
+
63
+ // Also an option if you don't want to escape tags and quotes in data URL
64
+ // But use it with care
65
+ const res4 = await sanitizeURL('data:image/svg+xml,%3Csvg%3E%3C/svg%3E', {
66
+ allow: ['data'],
67
+ escapeTags: false
68
+ }).then(res => decodeURIComponent(res));
69
+ // -> 'data:image/svg+xml,<svg></svg>'
70
+
71
+ const res5 = await sanitizeURL('data:text/html,%3Cscript%3Ealert(1);%3C/script%3E', {
72
+ allow: ['data'],
73
+ escapeTags: false
74
+ }).then(res => decodeURIComponent(res));
75
+ // WATCH OUT!!!
76
+ // -> 'data:text/html,<script>alert(1);</script>'
77
+ ```
78
+
79
+ ## sanitizeURLSync
80
+
81
+ Synchronous version of the [sanitizeURL()](#sanitizeurlurl-opt).
82
+
83
+ ## isURI(uri)
84
+
85
+ Determines whether the given URI is valid.
86
+
87
+ ### Parameters
88
+
89
+ * `uri` **[string][1]** URI input
90
+
91
+ Returns **[Promise][5]<[boolean][3]?>** result
92
+
93
+ ```javascript
94
+ const res1 = await isURI('https://example.com/foo');
95
+ // -> true
96
+
97
+ const res2 = await isURI('mailto:foo@example.com');
98
+ // -> true
99
+
100
+ const res3 = await isURI('foo:bar');
101
+ // -> false
102
+
103
+ const res4 = await isURI('web+foo:bar');
104
+ // -> true
105
+ ```
106
+
107
+ ## isURISync(uri)
108
+
109
+ Synchronous version of the [isURI()](#isuriuri).
110
+
111
+ ## urlSanitizer
112
+
113
+ ### urlSanitizer.get()
114
+
115
+ Get an array of URI schemes registered at [iana.org](https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml "Uniform Resource Identifier (URI) Schemes").
116
+ * Historical schemes omitted.
117
+ * `moz-extension` scheme added by default.
118
+
119
+ Returns **[Array][2]<[string][1]>** array of registered URI schemes
120
+
121
+ ```javascript
122
+ const schemes = urlSanitizer.get();
123
+ // -> ['aaa', 'aaas', 'about', 'acap', 'acct', 'acd', 'acr', ...];
124
+ ```
125
+
126
+ ### urlSanitizer.has(scheme)
127
+
128
+ Check if the given scheme is registered.
129
+
130
+ #### Parameters
131
+
132
+ * `scheme` **[string][1]** scheme
133
+
134
+ Returns **[boolean][3]** result
135
+ * Always `true` for `web+*` and/or `ext+*` schemes
136
+
137
+ ```javascript
138
+ const res1 = urlSanitizer.has('https');
139
+ // -> true
140
+
141
+ const res2 = urlSanitizer.has('foo');
142
+ // -> false
143
+
144
+ const res3 = uriSanitizer.has('web+foo');
145
+ // -> true
146
+ ```
147
+
148
+ ### urlSanitizer.add(scheme)
149
+
150
+ Add a scheme to the list of URI schemes.
151
+ * `javascript` and/or `vbscript` schemes can not be registered. It throws.
152
+
153
+ #### Parameters
154
+
155
+ * `scheme` **[string][1]** scheme
156
+
157
+ Returns **[Array][2]<[string][1]>** array of registered URI schemes
158
+
159
+ ```javascript
160
+ const res = urlSanitizer.add('foo');
161
+ // -> ['aaa', 'aaas', 'about', 'acap', 'acct', 'acd', ... 'foo', ...];
162
+ ```
163
+
164
+ ### urlSanitizer.remove(scheme)
165
+
166
+ Remove a scheme from the list of URI schemes.
167
+
168
+ #### Parameters
169
+
170
+ * `scheme` **[string][1]** scheme
171
+
172
+ Returns **[boolean][3]** result
173
+ * `true` if the scheme is successfully removed, `false` otherwise.
174
+
175
+ ```javascript
176
+ const res1 = urlSanitizer.remove('aaa');
177
+ // -> true
178
+
179
+ const res2 = urlSanitizer.remove('foo');
180
+ // -> false
181
+ ```
182
+
183
+ [1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
184
+
185
+ [2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
186
+
187
+ [3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
188
+
189
+ [4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
190
+
191
+ [5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise