heimdall-sdk 0.1.0 → 0.2.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) 2026 Alessandro Amormino
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,122 @@
1
+ # heimdall-sdk
2
+
3
+ **Zero-config API logger for React (and any JS framework).**
4
+
5
+ The Heimdall SDK automatically intercepts every `fetch` and `XMLHttpRequest` call in your frontend and sends the logs to a [Heimdall](https://github.com/alessandroamormino/heimdall) server, where you can browse them in a dashboard.
6
+
7
+ ---
8
+
9
+ ## Requirements
10
+
11
+ A running Heimdall server. The easiest way is Docker:
12
+
13
+ ```bash
14
+ docker run -d \
15
+ --name heimdall \
16
+ -p 3333:3333 \
17
+ -v heimdall_data:/data \
18
+ ghcr.io/alessandroamormino/heimdall:latest
19
+ ```
20
+
21
+ See the [full server setup guide](https://github.com/alessandroamormino/heimdall).
22
+
23
+ ---
24
+
25
+ ## Installation
26
+
27
+ ```bash
28
+ npm install heimdall-sdk
29
+ ```
30
+
31
+ ---
32
+
33
+ ## Usage
34
+
35
+ ### React
36
+
37
+ ```tsx
38
+ // main.tsx or App.tsx
39
+ import { HeimdallProvider } from 'heimdall-sdk'
40
+
41
+ export default function App() {
42
+ return (
43
+ <HeimdallProvider collectorUrl="http://localhost:3333">
44
+ <YourApp />
45
+ </HeimdallProvider>
46
+ )
47
+ }
48
+ ```
49
+
50
+ That's it. Every `fetch` and `XHR` call is now logged automatically.
51
+
52
+ ### Next.js
53
+
54
+ ```tsx
55
+ // app/layout.tsx
56
+ import { HeimdallProvider } from 'heimdall-sdk'
57
+
58
+ export default function RootLayout({ children }: { children: React.ReactNode }) {
59
+ return (
60
+ <html>
61
+ <body>
62
+ <HeimdallProvider collectorUrl="http://localhost:3333">
63
+ {children}
64
+ </HeimdallProvider>
65
+ </body>
66
+ </html>
67
+ )
68
+ }
69
+ ```
70
+
71
+ > Add `'use client'` at the top if Next.js requires it.
72
+
73
+ ### Vue / Svelte / Vanilla JS
74
+
75
+ ```ts
76
+ import { patchFetch } from 'heimdall-sdk'
77
+
78
+ patchFetch({
79
+ collectorUrl: 'http://localhost:3333',
80
+ project: 'my-app',
81
+ })
82
+ ```
83
+
84
+ ---
85
+
86
+ ## Options
87
+
88
+ | Prop | Type | Default | Description |
89
+ |---|---|---|---|
90
+ | `collectorUrl` | `string` | — | **Required.** URL of the Heimdall server |
91
+ | `project` | `string` | `"default"` | Label to group logs by app |
92
+ | `ignore` | `(string \| RegExp)[]` | `[]` | URLs to skip (matched by substring or regex) |
93
+ | `enabled` | `boolean` | `true` | Set to `false` to disable interception |
94
+
95
+ ```tsx
96
+ <HeimdallProvider
97
+ collectorUrl="http://localhost:3333"
98
+ project="my-app"
99
+ ignore={['/health', /^\/internal/]}
100
+ enabled={process.env.NODE_ENV !== 'production'}
101
+ >
102
+ ```
103
+
104
+ ---
105
+
106
+ ## Secret detection
107
+
108
+ The SDK automatically scans every request for exposed secrets **before** sending the log. If a token or credential is found, its value is replaced with `[REDACTED]` and a warning is recorded.
109
+
110
+ | Location | Examples |
111
+ |---|---|
112
+ | **URL query parameters** | `access_token`, `api_key`, `token`, `secret`, `password`, … |
113
+ | **Request headers** | `Authorization`, `x-api-key`, `x-auth-token`, `Cookie`, … |
114
+ | **Token-shaped values** | JWT (`eyJ…`), Mapbox (`sk.`/`pk.`), long hex/base64 strings |
115
+
116
+ The real token never reaches the server or the database — redaction happens entirely in the browser.
117
+
118
+ ---
119
+
120
+ ## License
121
+
122
+ MIT — © Alessandro Amormino
@@ -1,6 +1,49 @@
1
1
  function shouldIgnore(url, patterns) {
2
2
  return patterns.some(p => typeof p === 'string' ? url.includes(p) : p.test(url));
3
3
  }
4
+ const SENSITIVE_URL_PARAMS = new Set([
5
+ 'access_token', 'token', 'api_key', 'apikey', 'secret', 'key',
6
+ 'auth', 'password', 'pass', 'pwd', 'client_secret', 'private_key',
7
+ 'api_secret', 'api_token', 'client_id',
8
+ ]);
9
+ const SENSITIVE_HEADERS = new Set([
10
+ 'authorization', 'x-api-key', 'x-auth-token', 'x-access-token',
11
+ 'x-secret', 'x-secret-key', 'x-token', 'x-auth', 'cookie',
12
+ 'x-api-secret', 'x-private-key',
13
+ ]);
14
+ const TOKEN_PATTERNS = [
15
+ /^eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/, // JWT
16
+ /^(sk|pk)\.[A-Za-z0-9._-]{20,}/, // Mapbox
17
+ /^[a-f0-9]{32,}$/, // hex API key
18
+ /^[A-Za-z0-9+/]{40,}={0,2}$/, // base64
19
+ ];
20
+ function looksLikeToken(value) {
21
+ return TOKEN_PATTERNS.some(p => p.test(value));
22
+ }
23
+ function scanForSecrets(url, headers) {
24
+ const warnings = [];
25
+ let safeUrl = url;
26
+ try {
27
+ const parsed = new URL(url);
28
+ parsed.searchParams.forEach((value, key) => {
29
+ if (SENSITIVE_URL_PARAMS.has(key.toLowerCase()) || looksLikeToken(value)) {
30
+ warnings.push(`Exposed secret in URL parameter "${key}"`);
31
+ parsed.searchParams.set(key, '[REDACTED]');
32
+ }
33
+ });
34
+ if (warnings.length > 0)
35
+ safeUrl = parsed.toString();
36
+ }
37
+ catch { /* relative URL */ }
38
+ const safeHeaders = { ...headers };
39
+ for (const [key, value] of Object.entries(headers)) {
40
+ if (SENSITIVE_HEADERS.has(key.toLowerCase()) || looksLikeToken(value)) {
41
+ warnings.push(`Exposed secret in header "${key}"`);
42
+ safeHeaders[key] = '[REDACTED]';
43
+ }
44
+ }
45
+ return { url: safeUrl, requestHeaders: safeHeaders, warnings };
46
+ }
4
47
  function sendLog(collectorUrl, payload) {
5
48
  // Use the original fetch before we patch it
6
49
  const f = window.__heimdall_fetch;
@@ -37,7 +80,8 @@ export function patchFetch(config) {
37
80
  requestBodyParsed = String(requestBody);
38
81
  }
39
82
  }
40
- const requestHeaders = Object.fromEntries(new Headers(init?.headers ?? (input instanceof Request ? input.headers : {})).entries());
83
+ const rawHeaders = Object.fromEntries(new Headers(init?.headers ?? (input instanceof Request ? input.headers : {})).entries());
84
+ const scanned = scanForSecrets(url, rawHeaders);
41
85
  const start = performance.now();
42
86
  let response;
43
87
  try {
@@ -46,11 +90,12 @@ export function patchFetch(config) {
46
90
  catch (err) {
47
91
  sendLog(config.collectorUrl, {
48
92
  method,
49
- url,
93
+ url: scanned.url,
50
94
  error: String(err),
51
95
  duration_ms: Math.round(performance.now() - start),
52
- request_headers: requestHeaders,
96
+ request_headers: scanned.requestHeaders,
53
97
  request_body: requestBodyParsed,
98
+ security_warnings: scanned.warnings.length ? scanned.warnings : undefined,
54
99
  project: config.project ?? 'default',
55
100
  timestamp: new Date().toISOString(),
56
101
  });
@@ -72,13 +117,14 @@ export function patchFetch(config) {
72
117
  const responseHeaders = Object.fromEntries(response.headers.entries());
73
118
  sendLog(config.collectorUrl, {
74
119
  method,
75
- url,
120
+ url: scanned.url,
76
121
  status_code: response.status,
77
122
  duration_ms: duration,
78
- request_headers: requestHeaders,
123
+ request_headers: scanned.requestHeaders,
79
124
  request_body: requestBodyParsed,
80
125
  response_headers: responseHeaders,
81
126
  response_body: responseBody,
127
+ security_warnings: scanned.warnings.length ? scanned.warnings : undefined,
82
128
  project: config.project ?? 'default',
83
129
  timestamp: new Date().toISOString(),
84
130
  });
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "heimdall-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Heimdall SDK — zero-config API logger for React",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
+ "license": "MIT",
8
9
  "files": ["dist"],
9
10
  "scripts": {
10
11
  "build": "tsc",