@steve02081504/virtual-console 0.1.1 → 0.1.3

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.
Files changed (3) hide show
  1. package/README.md +30 -0
  2. package/package.json +1 -1
  3. package/util.mjs +1 -1
package/README.md CHANGED
@@ -31,6 +31,12 @@ A powerful and flexible virtual console for **Node.js and the Browser** that all
31
31
  npm install @steve02081504/virtual-console
32
32
  ```
33
33
 
34
+ ### Browser Import
35
+
36
+ ```javascript
37
+ import { VirtualConsole } from 'https://esm.sh/@steve02081504/virtual-console';
38
+ ```
39
+
34
40
  ## Usage
35
41
 
36
42
  ### 1. Basic Testing (Capture Output)
@@ -157,6 +163,30 @@ Hooks the virtual console into the current execution context.
157
163
  - **Scope Limitation:** `hookAsyncContext(fn)` works for the duration of the function execution. However, strict "async" context propagation (like passing context into a `setTimeout` callback) is mimicked but may not be as robust as Node.js's native hooks.
158
164
  - `freshLine` cannot erase previous lines in the real browser console limitations, so it appends logs instead.
159
165
 
166
+ ## Security Considerations
167
+
168
+ ### HTML Injection Protection
169
+
170
+ `VirtualConsole` is designed to be safe for rendering console output in an HTML context. All console arguments, including those used with `%s`, `%o`, and other format specifiers, are automatically sanitized to prevent Cross-Site Scripting (XSS) attacks.
171
+
172
+ Specifically:
173
+
174
+ - **Argument Sanitization:** All string-based inputs are escaped. For example, `<script>alert(1)</script>` becomes `&lt;script&gt;alert(1)&lt;/script&gt;`. This is handled by the underlying `ansi_up` library.
175
+ - **CSS Style (`%c`) Sanitization:** When using the `%c` specifier for styling, the provided CSS string is sanitized to prevent it from breaking out of the `style` attribute. Potentially malicious characters like `<`, `>`, and `"` are escaped, ensuring that HTML cannot be injected.
176
+
177
+ Example of protection:
178
+
179
+ ```javascript
180
+ // Malicious input
181
+ console.log('%cAttempting injection', '"><script>alert("pwned")</script><span style="');
182
+
183
+ // Sanitized HTML Output
184
+ // The malicious string is safely contained within the style attribute.
185
+ // <span style="&quot;>&lt;script>alert(&quot;pwned&quot;)&lt;/script>&lt;span style=&quot;">Attempting injection</span>
186
+ ```
187
+
188
+ This ensures that you can safely display logs in a web UI without creating security vulnerabilities.
189
+
160
190
  ## Integration for Library Authors
161
191
 
162
192
  If you are building a library that manages its own async contexts, you can synchronize with `VirtualConsole` using:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve02081504/virtual-console",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "A virtual console for capturing and manipulating terminal output.",
5
5
  "main": "main.mjs",
6
6
  "type": "module",
package/util.mjs CHANGED
@@ -34,7 +34,7 @@ export function argsToHtml(args) {
34
34
  switch (match) {
35
35
  case '%c': {
36
36
  hasStyle = true
37
- const style = String(arg)
37
+ const style = String(arg).replaceAll('"', '&quot;').replaceAll('<', '&lt;').replaceAll('>', '&gt;')
38
38
  return `</span><span style="${style}">`
39
39
  }
40
40
  case '%s':