codebase-onboarder 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 +275 -0
- package/bin/onboarder.js +14 -0
- package/bin/postinstall.js +6 -0
- package/cli/commands.js +436 -0
- package/cli/main.js +129 -0
- package/cli/prompt.js +101 -0
- package/cli/ui.js +55 -0
- package/cli/wizard.js +325 -0
- package/package.json +48 -0
- package/public/app.js +1339 -0
- package/public/index.html +455 -0
- package/public/js/about.js +284 -0
- package/public/js/aiDraft.js +163 -0
- package/public/js/analysisPanel.js +270 -0
- package/public/js/analysisReport.js +154 -0
- package/public/js/api.js +264 -0
- package/public/js/atlas.js +98 -0
- package/public/js/blameView.js +41 -0
- package/public/js/codeTab.js +256 -0
- package/public/js/codeViewer.js +184 -0
- package/public/js/components/FileChip.js +102 -0
- package/public/js/components/MetricSparkline.js +89 -0
- package/public/js/components/RiskBadge.js +105 -0
- package/public/js/deepAnalysisView.js +496 -0
- package/public/js/diagramPane.js +154 -0
- package/public/js/diffView.js +352 -0
- package/public/js/docsView.js +566 -0
- package/public/js/fileSourceBrowser.js +47 -0
- package/public/js/flameGraph.js +75 -0
- package/public/js/forceGraph.js +592 -0
- package/public/js/heatmap.js +185 -0
- package/public/js/highlight.js +122 -0
- package/public/js/html.js +50 -0
- package/public/js/insightsView.js +236 -0
- package/public/js/inspector.js +535 -0
- package/public/js/llm.js +345 -0
- package/public/js/mapView.js +159 -0
- package/public/js/markdown.js +119 -0
- package/public/js/mindmap.js +289 -0
- package/public/js/repoFiles.js +44 -0
- package/public/js/sbomView.js +142 -0
- package/public/js/scanCache.js +76 -0
- package/public/js/search.js +874 -0
- package/public/js/serverSettings.js +169 -0
- package/public/js/state.js +192 -0
- package/public/js/tour.js +17 -0
- package/public/js/transitions.js +15 -0
- package/public/js/tree.js +200 -0
- package/public/js/workflowsView.js +95 -0
- package/public/styles.css +2767 -0
- package/public/vendor/mermaid.min.js +3587 -0
- package/public/vendor/monaco/vs/base/browser/ui/codicons/codicon/codicon.ttf +0 -0
- package/public/vendor/monaco/vs/base/worker/workerMain.js +31 -0
- package/public/vendor/monaco/vs/basic-languages/abap/abap.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/apex/apex.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/azcli/azcli.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/bat/bat.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/bicep/bicep.js +11 -0
- package/public/vendor/monaco/vs/basic-languages/cameligo/cameligo.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/clojure/clojure.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/coffee/coffee.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/cpp/cpp.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/csharp/csharp.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/csp/csp.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/css/css.js +12 -0
- package/public/vendor/monaco/vs/basic-languages/cypher/cypher.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/dart/dart.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/dockerfile/dockerfile.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/ecl/ecl.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/elixir/elixir.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/flow9/flow9.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/freemarker2/freemarker2.js +12 -0
- package/public/vendor/monaco/vs/basic-languages/fsharp/fsharp.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/go/go.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/graphql/graphql.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/handlebars/handlebars.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/hcl/hcl.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/html/html.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/ini/ini.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/java/java.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/javascript/javascript.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/julia/julia.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/kotlin/kotlin.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/less/less.js +11 -0
- package/public/vendor/monaco/vs/basic-languages/lexon/lexon.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/liquid/liquid.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/lua/lua.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/m3/m3.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/markdown/markdown.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/mdx/mdx.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/mips/mips.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/msdax/msdax.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/mysql/mysql.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/objective-c/objective-c.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/pascal/pascal.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/pascaligo/pascaligo.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/perl/perl.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/pgsql/pgsql.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/php/php.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/pla/pla.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/postiats/postiats.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/powerquery/powerquery.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/powershell/powershell.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/protobuf/protobuf.js +11 -0
- package/public/vendor/monaco/vs/basic-languages/pug/pug.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/python/python.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/qsharp/qsharp.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/r/r.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/razor/razor.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/redis/redis.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/redshift/redshift.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/restructuredtext/restructuredtext.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/ruby/ruby.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/rust/rust.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/sb/sb.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/scala/scala.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/scheme/scheme.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/scss/scss.js +12 -0
- package/public/vendor/monaco/vs/basic-languages/shell/shell.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/solidity/solidity.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/sophia/sophia.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/sparql/sparql.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/sql/sql.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/st/st.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/swift/swift.js +13 -0
- package/public/vendor/monaco/vs/basic-languages/systemverilog/systemverilog.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/tcl/tcl.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/twig/twig.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/typescript/typescript.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/typespec/typespec.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/vb/vb.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/wgsl/wgsl.js +307 -0
- package/public/vendor/monaco/vs/basic-languages/xml/xml.js +10 -0
- package/public/vendor/monaco/vs/basic-languages/yaml/yaml.js +10 -0
- package/public/vendor/monaco/vs/editor/editor.main.css +8 -0
- package/public/vendor/monaco/vs/editor/editor.main.js +798 -0
- package/public/vendor/monaco/vs/language/css/cssMode.js +13 -0
- package/public/vendor/monaco/vs/language/css/cssWorker.js +77 -0
- package/public/vendor/monaco/vs/language/html/htmlMode.js +13 -0
- package/public/vendor/monaco/vs/language/html/htmlWorker.js +454 -0
- package/public/vendor/monaco/vs/language/json/jsonMode.js +19 -0
- package/public/vendor/monaco/vs/language/json/jsonWorker.js +42 -0
- package/public/vendor/monaco/vs/language/typescript/tsMode.js +20 -0
- package/public/vendor/monaco/vs/language/typescript/tsWorker.js +51328 -0
- package/public/vendor/monaco/vs/loader.js +11 -0
- package/public/vendor/monaco/worker-boot.js +9 -0
- package/server/.fuse_hidden0000000800000001 +36 -0
- package/server/apiDiff.js +25 -0
- package/server/apiDocs.js +67 -0
- package/server/apiFile.js +36 -0
- package/server/apiGitBlame.js +63 -0
- package/server/apiMcp.js +110 -0
- package/server/apiScan.js +119 -0
- package/server/apiSearch.js +253 -0
- package/server/apiSettings.js +118 -0
- package/server/apiTools.js +90 -0
- package/server/config.js +227 -0
- package/server/fileSourceNode.js +44 -0
- package/server/gitClone.js +95 -0
- package/server/gitDiff.js +184 -0
- package/server/gitHistory.js +110 -0
- package/server/htmlText.js +44 -0
- package/server/http.js +55 -0
- package/server/httpGuards.js +87 -0
- package/server/index.js +156 -0
- package/server/llmProxy.js +162 -0
- package/server/logger.js +29 -0
- package/server/mcp/analysis.js +209 -0
- package/server/mcp/http.js +213 -0
- package/server/mcp/runner.js +278 -0
- package/server/mcp/server.js +241 -0
- package/server/mcp/standalone.js +42 -0
- package/server/mcp/tools.js +683 -0
- package/server/paths.js +39 -0
- package/server/router.js +218 -0
- package/server/searchIndex.js +118 -0
- package/server/sessions.js +163 -0
- package/server/static.js +59 -0
- package/server/tools/install.js +246 -0
- package/server/tools/parse.js +170 -0
- package/server/tools/platform.js +91 -0
- package/server/tools/registry.js +212 -0
- package/server/tools/scan.js +136 -0
- package/server/tools.js +212 -0
- package/server/tunnel.js +92 -0
- package/shared/analyzer/docs.js +135 -0
- package/shared/analyzer/explainLocal.js +163 -0
- package/shared/analyzer/graph.js +461 -0
- package/shared/analyzer/health.js +215 -0
- package/shared/analyzer/history.js +146 -0
- package/shared/analyzer/languages/csharp.js +39 -0
- package/shared/analyzer/languages/generic.js +131 -0
- package/shared/analyzer/languages/go.js +70 -0
- package/shared/analyzer/languages/index.js +43 -0
- package/shared/analyzer/languages/java.js +39 -0
- package/shared/analyzer/languages/javascript.js +240 -0
- package/shared/analyzer/languages/python.js +120 -0
- package/shared/analyzer/languages/rust.js +42 -0
- package/shared/analyzer/languages/typescript.js +138 -0
- package/shared/analyzer/licenses.js +151 -0
- package/shared/analyzer/metrics.js +120 -0
- package/shared/analyzer/pathUtil.js +69 -0
- package/shared/analyzer/patterns.js +272 -0
- package/shared/analyzer/scan.js +473 -0
- package/shared/analyzer/security.js +187 -0
- package/shared/analyzer/services.js +157 -0
- package/shared/analyzer/stack.js +173 -0
- package/shared/analyzer/tour.js +69 -0
- package/shared/analyzer/util.js +123 -0
- package/shared/analyzer/workflows.js +143 -0
- package/shared/diagram/aiFacts.js +145 -0
- package/shared/diagram/aiMermaid.js +59 -0
- package/shared/diagram/atlas.js +93 -0
- package/shared/diagram/mermaid.js +459 -0
- package/shared/search/query.js +518 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// FileSource over node:fs. Paths crossing the boundary are repo-relative
|
|
2
|
+
// POSIX style; this adapter is the only place that knows about the real
|
|
3
|
+
// filesystem layout.
|
|
4
|
+
|
|
5
|
+
import { promises as fs } from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
|
|
8
|
+
import { resolveInside } from './paths.js';
|
|
9
|
+
|
|
10
|
+
export function nodeFileSource(rootAbs) {
|
|
11
|
+
const toAbs = (rel) => {
|
|
12
|
+
// `list('')` is how the scanner asks for the top of the repo. Everywhere
|
|
13
|
+
// else an empty path is a caller mistake, which is why resolveInside
|
|
14
|
+
// refuses it rather than quietly meaning "the root".
|
|
15
|
+
if (rel === '' || rel == null) return rootAbs;
|
|
16
|
+
// The scanner only ever produces paths from directory listings, so this
|
|
17
|
+
// should never fire — but it's the same check the HTTP routes use, and a
|
|
18
|
+
// free one.
|
|
19
|
+
const abs = resolveInside(rootAbs, String(rel));
|
|
20
|
+
if (!abs) throw new Error('Path escapes the repository root');
|
|
21
|
+
return abs;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
kind: 'node',
|
|
26
|
+
root: rootAbs,
|
|
27
|
+
name: path.basename(rootAbs),
|
|
28
|
+
|
|
29
|
+
async list(dir) {
|
|
30
|
+
const entries = await fs.readdir(toAbs(dir), { withFileTypes: true });
|
|
31
|
+
const out = [];
|
|
32
|
+
for (const e of entries) {
|
|
33
|
+
if (e.isSymbolicLink()) continue; // no loops, no surprises
|
|
34
|
+
const rel = dir ? dir + '/' + e.name : e.name;
|
|
35
|
+
out.push({ name: e.name, path: rel, type: e.isDirectory() ? 'dir' : 'file' });
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
async read(rel) {
|
|
41
|
+
return fs.readFile(toAbs(rel), 'utf8');
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Shallow-cloning for the git-URL flow. The URL is validated, then passed to
|
|
2
|
+
// git as an argument array — never through a shell — so nothing in it gets
|
|
3
|
+
// executed. Clones land in the OS temp dir and are removed on request.
|
|
4
|
+
|
|
5
|
+
import { spawn } from 'node:child_process';
|
|
6
|
+
import { promises as fs, rmSync } from 'node:fs';
|
|
7
|
+
import os from 'node:os';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import crypto from 'node:crypto';
|
|
10
|
+
|
|
11
|
+
const GIT_URL_RE = /^(https?:\/\/[^\s]+|git@[^\s:]+:[^\s]+|ssh:\/\/[^\s]+)$/;
|
|
12
|
+
const CLONE_TIMEOUT_MS = 180_000;
|
|
13
|
+
|
|
14
|
+
// Where clones go, and the only shape of path the removals below will touch.
|
|
15
|
+
// One definition: a delete that walks a directory tree should not be guarded by
|
|
16
|
+
// a condition restated at each call site.
|
|
17
|
+
const CLONE_PREFIX = path.join(os.tmpdir(), 'onboarder-');
|
|
18
|
+
|
|
19
|
+
export function isCloneDir(dir) {
|
|
20
|
+
return typeof dir === 'string' && dir.startsWith(CLONE_PREFIX) && dir !== CLONE_PREFIX;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function assertGitUrl(url) {
|
|
24
|
+
if (typeof url !== 'string' || !GIT_URL_RE.test(url.trim())) {
|
|
25
|
+
throw new Error('That doesn’t look like a git URL. https://, ssh:// or git@ forms work.');
|
|
26
|
+
}
|
|
27
|
+
return url.trim();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The name people recognize, from any URL shape we accept:
|
|
31
|
+
// https://github.com/org/repo.git, ssh://host/org/repo, git@host:org/repo
|
|
32
|
+
export function repoNameFromUrl(url) {
|
|
33
|
+
const clean = String(url).trim().replace(/\.git$/, '').replace(/\/+$/, '');
|
|
34
|
+
const tail = !clean.includes('://') && clean.includes(':') ? clean.split(':').pop() : clean;
|
|
35
|
+
return tail.split('/').pop() || 'repo';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function cloneRepo(url) {
|
|
39
|
+
const id = crypto.randomBytes(6).toString('hex');
|
|
40
|
+
const dir = CLONE_PREFIX + id;
|
|
41
|
+
try {
|
|
42
|
+
// Blobless rather than shallow: `--depth 1` threw the history away, and
|
|
43
|
+
// history is half of what this tool has to say (churn × complexity).
|
|
44
|
+
// `--filter=blob:none` fetches every commit and tree but no file versions —
|
|
45
|
+
// full history at a fraction of a full clone's weight; blobs arrive on
|
|
46
|
+
// demand if anything ever checks one out. `--single-branch` keeps it to the
|
|
47
|
+
// default branch: the newcomer cares about main, not every fork line.
|
|
48
|
+
await run('git', ['clone', '--filter=blob:none', '--single-branch', '--quiet', url, dir]);
|
|
49
|
+
} catch (err) {
|
|
50
|
+
await fs.rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
51
|
+
throw err;
|
|
52
|
+
}
|
|
53
|
+
return { id, dir };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function removeClone(dir) {
|
|
57
|
+
if (!isCloneDir(dir)) return;
|
|
58
|
+
await fs.rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// The version an exit handler can actually finish. A process on its way out will
|
|
62
|
+
// not come back to resolve a promise, so the sweep on shutdown has to be
|
|
63
|
+
// synchronous or it does not happen at all.
|
|
64
|
+
export function removeCloneSync(dir) {
|
|
65
|
+
if (!isCloneDir(dir)) return;
|
|
66
|
+
try {
|
|
67
|
+
rmSync(dir, { recursive: true, force: true });
|
|
68
|
+
} catch {
|
|
69
|
+
/* going down anyway; a leftover temp dir is not worth a crash on exit */
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function run(cmd, args) {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
const child = spawn(cmd, args, { stdio: ['ignore', 'ignore', 'pipe'] });
|
|
76
|
+
let stderr = '';
|
|
77
|
+
child.stderr.on('data', (chunk) => {
|
|
78
|
+
stderr += chunk;
|
|
79
|
+
if (stderr.length > 4000) stderr = stderr.slice(-4000);
|
|
80
|
+
});
|
|
81
|
+
const timer = setTimeout(() => {
|
|
82
|
+
child.kill('SIGKILL');
|
|
83
|
+
reject(new Error('git clone took too long and was stopped.'));
|
|
84
|
+
}, CLONE_TIMEOUT_MS);
|
|
85
|
+
child.on('error', (err) => {
|
|
86
|
+
clearTimeout(timer);
|
|
87
|
+
reject(new Error('Could not run git: ' + err.message));
|
|
88
|
+
});
|
|
89
|
+
child.on('close', (code) => {
|
|
90
|
+
clearTimeout(timer);
|
|
91
|
+
if (code === 0) resolve();
|
|
92
|
+
else reject(new Error('git clone failed. ' + (stderr.trim().split('\n').pop() || '')));
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// Git diff & refs provider.
|
|
2
|
+
// Executes git subcommands safely via child_process and parses patch hunks.
|
|
3
|
+
|
|
4
|
+
import { execFile } from 'node:child_process';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
|
|
7
|
+
const execFileAsync = promisify(execFile);
|
|
8
|
+
const MAX_DIFF_BYTES = 5 * 1024 * 1024; // 5 MB cap
|
|
9
|
+
|
|
10
|
+
export async function getGitRefs(root) {
|
|
11
|
+
try {
|
|
12
|
+
const [branchRes, tagRes, headRes, logRes] = await Promise.all([
|
|
13
|
+
execFileAsync('git', ['branch', '-a', '--format=%(refname:short)'], { cwd: root }).catch(() => ({ stdout: '' })),
|
|
14
|
+
execFileAsync('git', ['tag', '--sort=-creatordate'], { cwd: root }).catch(() => ({ stdout: '' })),
|
|
15
|
+
execFileAsync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: root }).catch(() => ({ stdout: 'main\n' })),
|
|
16
|
+
execFileAsync('git', ['log', '-n', '40', '--format=%H|%h|%s|%an|%aI'], { cwd: root }).catch(() => ({ stdout: '' })),
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
const branches = branchRes.stdout.split('\n').map((b) => b.trim()).filter(Boolean);
|
|
20
|
+
const tags = tagRes.stdout.split('\n').map((t) => t.trim()).filter(Boolean);
|
|
21
|
+
const current = headRes.stdout.trim() || 'HEAD';
|
|
22
|
+
|
|
23
|
+
const commits = logRes.stdout.split('\n').filter(Boolean).map((line) => {
|
|
24
|
+
const [hash, short, message, author, date] = line.split('|');
|
|
25
|
+
return { hash, short, message, author, date };
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
available: true,
|
|
30
|
+
branches: [...new Set(branches)],
|
|
31
|
+
tags,
|
|
32
|
+
current,
|
|
33
|
+
commits,
|
|
34
|
+
};
|
|
35
|
+
} catch (err) {
|
|
36
|
+
return { available: false, reason: err.message };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function getGitDiff(root, options = {}) {
|
|
41
|
+
const { base, head, file } = options;
|
|
42
|
+
const args = ['diff', '--no-color', '-U3'];
|
|
43
|
+
|
|
44
|
+
if (base && head) {
|
|
45
|
+
args.push(`${base}...${head}`);
|
|
46
|
+
} else if (base) {
|
|
47
|
+
args.push(base);
|
|
48
|
+
} else {
|
|
49
|
+
args.push('HEAD');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (file) {
|
|
53
|
+
args.push('--', file);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
const { stdout } = await execFileAsync('git', args, {
|
|
58
|
+
cwd: root,
|
|
59
|
+
maxBuffer: MAX_DIFF_BYTES,
|
|
60
|
+
});
|
|
61
|
+
return parseUnifiedDiff(stdout);
|
|
62
|
+
} catch (err) {
|
|
63
|
+
// If git diff exited with empty or specific error, fallback to HEAD~1..HEAD or working tree
|
|
64
|
+
try {
|
|
65
|
+
const fallback = await execFileAsync('git', ['diff', '--no-color', '-U3'], {
|
|
66
|
+
cwd: root,
|
|
67
|
+
maxBuffer: MAX_DIFF_BYTES,
|
|
68
|
+
});
|
|
69
|
+
return parseUnifiedDiff(fallback.stdout);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
return { files: [], stats: { filesChanged: 0, additions: 0, deletions: 0 }, raw: '' };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function parseUnifiedDiff(diffText) {
|
|
77
|
+
if (!diffText || typeof diffText !== 'string') {
|
|
78
|
+
return { files: [], stats: { filesChanged: 0, additions: 0, deletions: 0 }, raw: '' };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const files = [];
|
|
82
|
+
const lines = diffText.split('\n');
|
|
83
|
+
let currentFile = null;
|
|
84
|
+
let currentHunk = null;
|
|
85
|
+
let totalAdditions = 0;
|
|
86
|
+
let totalDeletions = 0;
|
|
87
|
+
|
|
88
|
+
for (let i = 0; i < lines.length; i++) {
|
|
89
|
+
const line = lines[i];
|
|
90
|
+
|
|
91
|
+
// File header: diff --git a/path b/path
|
|
92
|
+
if (line.startsWith('diff --git ')) {
|
|
93
|
+
const m = line.match(/^diff --git a\/(.+?) b\/(.+)$/);
|
|
94
|
+
const oldPath = m ? m[1] : '';
|
|
95
|
+
const newPath = m ? m[2] : '';
|
|
96
|
+
currentFile = {
|
|
97
|
+
oldPath,
|
|
98
|
+
newPath,
|
|
99
|
+
status: 'modified',
|
|
100
|
+
additions: 0,
|
|
101
|
+
deletions: 0,
|
|
102
|
+
hunks: [],
|
|
103
|
+
};
|
|
104
|
+
files.push(currentFile);
|
|
105
|
+
currentHunk = null;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (!currentFile) continue;
|
|
110
|
+
|
|
111
|
+
if (line.startsWith('new file mode ')) {
|
|
112
|
+
currentFile.status = 'added';
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (line.startsWith('deleted file mode ')) {
|
|
116
|
+
currentFile.status = 'deleted';
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (line.startsWith('rename from ')) {
|
|
120
|
+
currentFile.status = 'renamed';
|
|
121
|
+
currentFile.oldPath = line.slice(12).trim();
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (line.startsWith('rename to ')) {
|
|
125
|
+
currentFile.newPath = line.slice(10).trim();
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Hunk header: @@ -1,5 +1,6 @@ optional context
|
|
130
|
+
if (line.startsWith('@@ ')) {
|
|
131
|
+
const match = line.match(/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)$/);
|
|
132
|
+
if (match) {
|
|
133
|
+
const oldStart = parseInt(match[1], 10);
|
|
134
|
+
const oldLines = match[2] !== undefined ? parseInt(match[2], 10) : 1;
|
|
135
|
+
const newStart = parseInt(match[3], 10);
|
|
136
|
+
const newLines = match[4] !== undefined ? parseInt(match[4], 10) : 1;
|
|
137
|
+
currentHunk = {
|
|
138
|
+
header: line,
|
|
139
|
+
oldStart,
|
|
140
|
+
oldLines,
|
|
141
|
+
newStart,
|
|
142
|
+
newLines,
|
|
143
|
+
heading: match[5]?.trim() || '',
|
|
144
|
+
lines: [],
|
|
145
|
+
};
|
|
146
|
+
currentFile.hunks.push(currentHunk);
|
|
147
|
+
}
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (!currentHunk) continue;
|
|
152
|
+
|
|
153
|
+
if (line.startsWith('+') && !line.startsWith('+++')) {
|
|
154
|
+
currentFile.additions++;
|
|
155
|
+
totalAdditions++;
|
|
156
|
+
currentHunk.lines.push({
|
|
157
|
+
type: 'add',
|
|
158
|
+
text: line.slice(1),
|
|
159
|
+
});
|
|
160
|
+
} else if (line.startsWith('-') && !line.startsWith('---')) {
|
|
161
|
+
currentFile.deletions++;
|
|
162
|
+
totalDeletions++;
|
|
163
|
+
currentHunk.lines.push({
|
|
164
|
+
type: 'del',
|
|
165
|
+
text: line.slice(1),
|
|
166
|
+
});
|
|
167
|
+
} else if (line.startsWith(' ')) {
|
|
168
|
+
currentHunk.lines.push({
|
|
169
|
+
type: 'context',
|
|
170
|
+
text: line.slice(1),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
files,
|
|
177
|
+
stats: {
|
|
178
|
+
filesChanged: files.length,
|
|
179
|
+
additions: totalAdditions,
|
|
180
|
+
deletions: totalDeletions,
|
|
181
|
+
},
|
|
182
|
+
raw: diffText,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Reading git history for a scanned root. The same discipline as gitClone.js:
|
|
2
|
+
// git is spawned with an argument array — never through a shell — and its
|
|
3
|
+
// output is parsed, not interpreted. A root without history is an expected
|
|
4
|
+
// outcome, not an error: the browser folder-pick flow never touches git, a
|
|
5
|
+
// downloaded tarball has no .git, and a plain folder may simply not be a
|
|
6
|
+
// checkout. All of those come back as { available: false, reason } so the UI
|
|
7
|
+
// can say why instead of showing zeros.
|
|
8
|
+
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
|
|
11
|
+
// How far back the window goes. Two thousand commits is years of work on most
|
|
12
|
+
// repos and keeps the log parse bounded; `totalCommits` travels alongside so
|
|
13
|
+
// truncation is stated, not silent.
|
|
14
|
+
const LOG_CAP = 2000;
|
|
15
|
+
const TIMEOUT_MS = 30_000;
|
|
16
|
+
|
|
17
|
+
// Record separator \x1e between commits, unit separator \x1f between fields.
|
|
18
|
+
// Filenames are one per line after the header. A filename containing \x1e or
|
|
19
|
+
// \x1f is legal on paper and does not happen in practice — the same trade every
|
|
20
|
+
// machine-readable git format makes (documented limitation, like .gitignore
|
|
21
|
+
// negation in scan.js).
|
|
22
|
+
const LOG_FORMAT = '%x1e%H%x1f%an%x1f%ae%x1f%aI';
|
|
23
|
+
|
|
24
|
+
export async function gitLog(root) {
|
|
25
|
+
let countText;
|
|
26
|
+
try {
|
|
27
|
+
countText = await run(['-C', root, 'rev-list', '--count', 'HEAD']);
|
|
28
|
+
} catch (err) {
|
|
29
|
+
return { ok: false, reason: explain(err) };
|
|
30
|
+
}
|
|
31
|
+
const totalCommits = parseInt(countText.trim(), 10) || 0;
|
|
32
|
+
if (!totalCommits) {
|
|
33
|
+
return { ok: false, reason: 'The repository exists but has no commits yet.' };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let text;
|
|
37
|
+
try {
|
|
38
|
+
text = await run([
|
|
39
|
+
'-C', root, 'log',
|
|
40
|
+
'--max-count=' + LOG_CAP,
|
|
41
|
+
'--pretty=format:' + LOG_FORMAT,
|
|
42
|
+
'--name-only',
|
|
43
|
+
'--no-renames',
|
|
44
|
+
'HEAD',
|
|
45
|
+
]);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
return { ok: false, reason: explain(err) };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { ok: true, text, totalCommits };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Pure, so the format contract is testable without a git binary.
|
|
54
|
+
export function parseGitLog(text) {
|
|
55
|
+
const commits = [];
|
|
56
|
+
for (const record of String(text).split('\x1e')) {
|
|
57
|
+
if (!record.trim()) continue;
|
|
58
|
+
const lines = record.split('\n');
|
|
59
|
+
const [hash, name, email, date] = lines[0].split('\x1f');
|
|
60
|
+
if (!hash) continue;
|
|
61
|
+
const files = [];
|
|
62
|
+
for (let i = 1; i < lines.length; i++) {
|
|
63
|
+
const f = lines[i].trim();
|
|
64
|
+
if (f) files.push(f);
|
|
65
|
+
}
|
|
66
|
+
commits.push({ hash, author: { name: name || '', email: email || '' }, date: date || '', files });
|
|
67
|
+
}
|
|
68
|
+
return commits;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function explain(err) {
|
|
72
|
+
const message = err.message || '';
|
|
73
|
+
if (err.code === 'ENOENT' || /spawn git ENOENT/.test(message)) {
|
|
74
|
+
return 'git is not installed or not on PATH, so there is no history to read.';
|
|
75
|
+
}
|
|
76
|
+
if (/not a git repository/i.test(message)) {
|
|
77
|
+
return 'No .git here — this folder is not a checkout, so there is no history to read.';
|
|
78
|
+
}
|
|
79
|
+
if (/does not have any commits yet/i.test(message)) {
|
|
80
|
+
return 'The repository exists but has no commits yet.';
|
|
81
|
+
}
|
|
82
|
+
return 'git would not say: ' + message.split('\n').pop();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function run(args) {
|
|
86
|
+
return new Promise((resolve, reject) => {
|
|
87
|
+
const child = spawn('git', args, { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
88
|
+
let out = '';
|
|
89
|
+
let stderr = '';
|
|
90
|
+
child.stdout.on('data', (chunk) => { out += chunk; });
|
|
91
|
+
child.stderr.on('data', (chunk) => {
|
|
92
|
+
stderr += chunk;
|
|
93
|
+
if (stderr.length > 2000) stderr = stderr.slice(-2000);
|
|
94
|
+
});
|
|
95
|
+
const timer = setTimeout(() => {
|
|
96
|
+
child.kill('SIGKILL');
|
|
97
|
+
reject(new Error('git took too long and was stopped.'));
|
|
98
|
+
}, TIMEOUT_MS);
|
|
99
|
+
child.on('error', (err) => {
|
|
100
|
+
clearTimeout(timer);
|
|
101
|
+
err.message = 'Could not run git: ' + err.message;
|
|
102
|
+
reject(err);
|
|
103
|
+
});
|
|
104
|
+
child.on('close', (code) => {
|
|
105
|
+
clearTimeout(timer);
|
|
106
|
+
if (code === 0) resolve(out);
|
|
107
|
+
else reject(new Error(stderr.trim() || ('git exited ' + code)));
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// HTML to readable text, for the docs fetch. Not a parser and not trying to be:
|
|
2
|
+
// the output is fed to a language model as context, so what matters is that the
|
|
3
|
+
// prose survives and the markup, scripts and styles do not.
|
|
4
|
+
//
|
|
5
|
+
// Order is the whole trick. Script and style bodies go first — their contents are
|
|
6
|
+
// text, not tags, so stripping tags first would leave a page of minified
|
|
7
|
+
// JavaScript behind. The title is lifted before the general tag strip, because
|
|
8
|
+
// after it there is no way to tell the title from the first paragraph. Entities
|
|
9
|
+
// are decoded last, so a `<script>` written *about* HTML in the docs
|
|
10
|
+
// cannot turn back into markup that the earlier passes would have removed.
|
|
11
|
+
|
|
12
|
+
// Enough context for a model to summarise from without sending a whole site.
|
|
13
|
+
const MAX_TEXT = 14000;
|
|
14
|
+
|
|
15
|
+
const ENTITIES = {
|
|
16
|
+
' ': ' ', '&': '&', '<': '<', '>': '>', '"': '"', ''': "'",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Horizontal space, newlines excluded. The non-breaking space belongs here:
|
|
20
|
+
// ` ` is decoded to a plain space below, but pages carry the raw character
|
|
21
|
+
// as well and it should not survive as indentation.
|
|
22
|
+
const RUN_OF_SPACE = /[ \t\u00a0]+/g;
|
|
23
|
+
const AROUND_NEWLINE = /[ \t\u00a0]*\n[ \t\u00a0]*/g;
|
|
24
|
+
|
|
25
|
+
export function htmlToText(html, limit = MAX_TEXT) {
|
|
26
|
+
let s = String(html);
|
|
27
|
+
s = s.replace(/<script[\s\S]*?<\/script>/gi, ' ');
|
|
28
|
+
s = s.replace(/<style[\s\S]*?<\/style>/gi, ' ');
|
|
29
|
+
|
|
30
|
+
const rawTitle = (s.match(/<title[^>]*>([\s\S]*?)<\/title>/i) || [, ''])[1];
|
|
31
|
+
const title = rawTitle.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim();
|
|
32
|
+
|
|
33
|
+
s = s.replace(/<[^>]+>/g, ' ');
|
|
34
|
+
s = s.replace(/ |&|<|>|"|'/g, (m) => ENTITIES[m]);
|
|
35
|
+
|
|
36
|
+
// Newlines survive as paragraph breaks \u2014 a docs page read as one long line
|
|
37
|
+
// loses the structure a summary needs \u2014 but everything else collapses. Every
|
|
38
|
+
// tag became a space, so a break arrives as " \n\n\n " and the indentation of
|
|
39
|
+
// the source becomes indentation of the text; trimming around each newline is
|
|
40
|
+
// what makes the break itself clean.
|
|
41
|
+
s = s.replace(RUN_OF_SPACE, ' ').replace(AROUND_NEWLINE, '\n').replace(/\n{3,}/g, '\n\n').trim();
|
|
42
|
+
|
|
43
|
+
return { title, text: s.slice(0, limit) };
|
|
44
|
+
}
|
package/server/http.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Reading a request body and writing a response — the two things every route
|
|
2
|
+
// does. Pulled out of the router so a handler can be read on its own, and so
|
|
3
|
+
// the body limit is one number in one place rather than a check repeated per
|
|
4
|
+
// route and forgotten on the next one.
|
|
5
|
+
|
|
6
|
+
// A scan request carries a path or a URL; an explain request carries a prompt
|
|
7
|
+
// and a slice of facts. Two megabytes is far more than either needs, and the
|
|
8
|
+
// point is to have a ceiling at all: a local server with no auth should not let
|
|
9
|
+
// a stray fetch hold memory open.
|
|
10
|
+
export const MAX_BODY_BYTES = 2 * 1024 * 1024;
|
|
11
|
+
|
|
12
|
+
// Resolves to the parsed JSON body, or `{}` for an empty one — a POST with no
|
|
13
|
+
// body is a valid "just the defaults" request and every caller would otherwise
|
|
14
|
+
// need the same guard. Rejects on oversized or malformed input; the router turns
|
|
15
|
+
// that into a 500 with the message.
|
|
16
|
+
export function readBody(req, limit = MAX_BODY_BYTES) {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
let size = 0;
|
|
19
|
+
const chunks = [];
|
|
20
|
+
req.on('data', (chunk) => {
|
|
21
|
+
size += chunk.length;
|
|
22
|
+
if (size > limit) {
|
|
23
|
+
reject(new Error('Request body too large.'));
|
|
24
|
+
req.destroy();
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
chunks.push(chunk);
|
|
28
|
+
});
|
|
29
|
+
req.on('end', () => {
|
|
30
|
+
try {
|
|
31
|
+
resolve(chunks.length ? JSON.parse(Buffer.concat(chunks).toString('utf8')) : {});
|
|
32
|
+
} catch {
|
|
33
|
+
reject(new Error('The request body was not valid JSON.'));
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
req.on('error', reject);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function sendJSON(res, status, data) {
|
|
41
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' });
|
|
42
|
+
res.end(JSON.stringify(data));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Every error the UI shows comes back through here, so the shape is fixed:
|
|
46
|
+
// `{ error }` with a sentence a person can act on. The front end reads `.error`
|
|
47
|
+
// and nothing else.
|
|
48
|
+
export function sendError(res, status, message) {
|
|
49
|
+
sendJSON(res, status, { error: message });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function sendText(res, status, text) {
|
|
53
|
+
res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' });
|
|
54
|
+
res.end(text);
|
|
55
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Two questions worth asking about a request before it is allowed to do
|
|
2
|
+
// anything: did it arrive at a name that really is this machine, and did it
|
|
3
|
+
// come from this app's own page?
|
|
4
|
+
//
|
|
5
|
+
// Neither is authentication — a single-user local tool has nobody to
|
|
6
|
+
// authenticate. Both exist to stop some *other* page the user happens to have
|
|
7
|
+
// open from driving this server on their behalf. Binding to 127.0.0.1 keeps
|
|
8
|
+
// the network out; it does nothing about the browser, which is already inside.
|
|
9
|
+
|
|
10
|
+
const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '[::1]', '::1']);
|
|
11
|
+
|
|
12
|
+
// "localhost:4310" -> "localhost", "[::1]:4310" -> "[::1]"
|
|
13
|
+
function hostnameOf(hostHeader) {
|
|
14
|
+
const raw = String(hostHeader ?? '').trim().toLowerCase();
|
|
15
|
+
if (raw.startsWith('[')) {
|
|
16
|
+
const close = raw.indexOf(']');
|
|
17
|
+
return close === -1 ? raw : raw.slice(0, close + 1);
|
|
18
|
+
}
|
|
19
|
+
const colon = raw.lastIndexOf(':');
|
|
20
|
+
return colon === -1 ? raw : raw.slice(0, colon);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// DNS rebinding, the attack this server's loopback bind does not cover: a page
|
|
24
|
+
// on evil.com whose DNS answer flips to 127.0.0.1 reaches us anyway, and from
|
|
25
|
+
// then on the browser treats evil.com as the same origin as everything we
|
|
26
|
+
// return — so the origin check below is looking at `same-origin` and sees
|
|
27
|
+
// nothing wrong. What still gives it away is the Host header, which the page
|
|
28
|
+
// cannot set and which still says evil.com.
|
|
29
|
+
//
|
|
30
|
+
// `extraHosts` is the self-hosted escape hatch: the domain and bind address
|
|
31
|
+
// from the settings file, so a server that was deliberately put on the network
|
|
32
|
+
// answers to its own name and nothing else. A local server passes nothing and
|
|
33
|
+
// keeps the loopback-only rule. Entries may be exact names or `*.suffix`
|
|
34
|
+
// wildcards — enabled tunnels put their random upstream names here, which
|
|
35
|
+
// cannot be known ahead of time.
|
|
36
|
+
//
|
|
37
|
+
// Returns null when the request is fine, or a short reason when it isn't.
|
|
38
|
+
export function rebindingReason(req, extraHosts = []) {
|
|
39
|
+
const host = req.headers?.host;
|
|
40
|
+
if (!host) return 'The request arrived with no Host header.';
|
|
41
|
+
const name = hostnameOf(host);
|
|
42
|
+
if (LOOPBACK_HOSTS.has(name)) return null;
|
|
43
|
+
for (const extra of extraHosts) {
|
|
44
|
+
if (extra.startsWith('*.')) {
|
|
45
|
+
// "*.trycloudflare.com" matches "abc.trycloudflare.com" but not the bare
|
|
46
|
+
// suffix itself and not "evil-trycloudflare.com".
|
|
47
|
+
if (name.endsWith(extra.slice(1)) && name.length > extra.length) return null;
|
|
48
|
+
} else if (name === extra) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return `It arrived addressed to ${name}.`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Cross-site requests: any page in the browser can POST here without being
|
|
56
|
+
// able to read the answer, and "without being able to read the answer" is not
|
|
57
|
+
// the same as harmless — /api/scan walks a directory the caller names, and
|
|
58
|
+
// /api/explain will POST to any URL the caller names.
|
|
59
|
+
//
|
|
60
|
+
// Two signals, and a request only has to fail one to be refused:
|
|
61
|
+
//
|
|
62
|
+
// Sec-Fetch-Site set by the browser, unsettable by page script. `none`
|
|
63
|
+
// means the user did it themselves — typed the URL, opened
|
|
64
|
+
// a bookmark — which no attacking page can arrange.
|
|
65
|
+
// Origin sent on every fetch that isn't GET/HEAD. Compared against
|
|
66
|
+
// this server's own origin, which is `http://` plus the Host
|
|
67
|
+
// we just confirmed is loopback.
|
|
68
|
+
//
|
|
69
|
+
// A request carrying neither header isn't a browser at all — curl, a test, a
|
|
70
|
+
// script — and there is no cross-site story for those, so they pass.
|
|
71
|
+
export function crossOriginReason(req) {
|
|
72
|
+
const site = req.headers?.['sec-fetch-site'];
|
|
73
|
+
if (site && site !== 'same-origin' && site !== 'none') {
|
|
74
|
+
return `The browser reported it as ${site}.`;
|
|
75
|
+
}
|
|
76
|
+
const origin = req.headers?.origin;
|
|
77
|
+
if (origin) {
|
|
78
|
+
// The host was just vetted by the rebinding check, so the origin only has
|
|
79
|
+
// to agree with it. Both schemes are accepted: a self-hosted server behind
|
|
80
|
+
// a tunnel is reached over https while the page's fetches still arrive
|
|
81
|
+
// here addressed to the same host the tunnel forwarded.
|
|
82
|
+
const host = String(req.headers.host ?? '').trim().toLowerCase();
|
|
83
|
+
const expected = new Set(['http://' + host, 'https://' + host]);
|
|
84
|
+
if (!expected.has(String(origin).trim().toLowerCase())) return `It came from ${origin}.`;
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|