@goosetools/goose-tools 0.0.0-stage → 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/README.md +98 -2
- package/bin/goose-tools.js +61 -0
- package/lib/check/src/agent.js +90 -0
- package/lib/check/src/analyze.js +311 -0
- package/lib/check/src/client.js +136 -0
- package/lib/check/src/copy.js +355 -0
- package/lib/check/src/fixes.js +153 -0
- package/lib/check/src/frame.js +92 -0
- package/lib/check/src/guard.js +441 -0
- package/lib/check/src/index.js +18 -0
- package/lib/check/src/mcp-http.js +169 -0
- package/lib/check/src/mcp.js +153 -0
- package/lib/check/src/scan.js +55 -0
- package/lib/check/src/stack.js +61 -0
- package/lib/check/src/tlds.js +3 -0
- package/lib/check/src/view.js +130 -0
- package/lib/check/src/worker.js +257 -0
- package/lib/engine/src/bots.js +146 -0
- package/lib/engine/src/directives.js +131 -0
- package/lib/engine/src/exposure.js +70 -0
- package/lib/engine/src/frontend.js +157 -0
- package/lib/engine/src/headers.js +76 -0
- package/lib/engine/src/hidden-text.js +425 -0
- package/lib/engine/src/html.js +794 -0
- package/lib/engine/src/index.js +20 -0
- package/lib/engine/src/php.js +316 -0
- package/lib/engine/src/policies.js +234 -0
- package/lib/engine/src/reach.js +199 -0
- package/lib/engine/src/robots.js +275 -0
- package/lib/engine/src/secrets.js +142 -0
- package/lib/engine/src/signals.js +144 -0
- package/lib/rules/data/bots.json.js +1 -0
- package/lib/rules/data/exposure.json.js +1 -0
- package/lib/rules/data/frontend.json.js +1 -0
- package/lib/rules/data/headers.json.js +1 -0
- package/lib/rules/data/secrets.json.js +1 -0
- package/lib/ui/LICENSE-Inter.txt +93 -0
- package/lib/ui/assets.js +5 -0
- package/package.json +37 -4
- package/src/local.js +61 -0
- package/src/mcp.js +61 -0
- package/src/setup.js +395 -0
- package/src/site/auth.js +287 -0
- package/src/site/beacon.js +462 -0
- package/src/site/collect.js +484 -0
- package/src/site/handler.js +405 -0
- package/src/site/index.js +88 -0
- package/src/site/limits.js +48 -0
- package/src/site/pages.js +75 -0
- package/src/site/qr.js +318 -0
- package/src/site/reports.js +1117 -0
- package/src/site/source.js +68 -0
- package/src/site/store-pg.js +225 -0
- package/src/site/store.js +249 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reachable by each engine (port of Crawlers/Reach.php). For each search crawler, which of the
|
|
3
|
+
* sampled pages robots.txt lets it fetch, and which say noindex to it. Each engine answers from its
|
|
4
|
+
* own index, so a page one crawler cannot fetch is missing from that engine's answers only.
|
|
5
|
+
*
|
|
6
|
+
* Engines and what each crawler feeds: OAI-SearchBot (ChatGPT search), Claude-SearchBot (Claude's
|
|
7
|
+
* search), PerplexityBot (Perplexity), Bingbot (Bing and Microsoft Copilot), Googlebot (Google
|
|
8
|
+
* Search, AI Overviews and AI Mode).
|
|
9
|
+
*
|
|
10
|
+
* Snippet controls (nosnippet, max-snippet) are explained by their effect: Google's AI features
|
|
11
|
+
* guide (https://developers.google.com/search/docs/appearance/ai-features) lists them "To limit the
|
|
12
|
+
* information shown from your pages in Search".
|
|
13
|
+
*
|
|
14
|
+
* Not checked here: firewall rules, which decide before the site sees a request.
|
|
15
|
+
*/
|
|
16
|
+
import { str, int, isset, values, sprintf } from './php.js';
|
|
17
|
+
import { decisive } from './robots.js';
|
|
18
|
+
import { forBot, noindex, nosnippet } from './directives.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {object} Page
|
|
22
|
+
* @property {string} [label]
|
|
23
|
+
* @property {string} [path]
|
|
24
|
+
* @property {number} [status] HTTP status; 0 when the request failed, -1 when not requested.
|
|
25
|
+
* @property {string[]} [headers] X-Robots-Tag values.
|
|
26
|
+
* @property {import('./directives.js').Meta[]} [metas] Robots meta tags.
|
|
27
|
+
*
|
|
28
|
+
* @typedef {object} Engine
|
|
29
|
+
* @property {string} token
|
|
30
|
+
* @property {string} feeds
|
|
31
|
+
* @property {string} state ok, partly or blocked.
|
|
32
|
+
* @property {string[]} blocked
|
|
33
|
+
* @property {string[]} noindex
|
|
34
|
+
* @property {number} unchecked
|
|
35
|
+
* @property {string} rule
|
|
36
|
+
* @property {string[]} sentences
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Engines: robots.txt token => what it feeds.
|
|
41
|
+
* @returns {Record<string, string>}
|
|
42
|
+
*/
|
|
43
|
+
export function engines() {
|
|
44
|
+
return {
|
|
45
|
+
'OAI-SearchBot': 'ChatGPT search',
|
|
46
|
+
'Claude-SearchBot': 'Claude',
|
|
47
|
+
PerplexityBot: 'Perplexity',
|
|
48
|
+
bingbot: 'Bing and Microsoft Copilot',
|
|
49
|
+
Googlebot: 'Google Search, AI Overviews and AI Mode',
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Evaluate the sample.
|
|
55
|
+
* @param {import('./robots.js').RobotsTxt} robots The robots.txt crawlers get.
|
|
56
|
+
* @param {Page[]} pages Sampled pages.
|
|
57
|
+
* @returns {{engines: Engine[], blocked: number, sentence: string}}
|
|
58
|
+
*/
|
|
59
|
+
export function evaluate(robots, pages) {
|
|
60
|
+
const list = values(pages);
|
|
61
|
+
/** @type {Engine[]} */
|
|
62
|
+
const out = [];
|
|
63
|
+
let blocked = 0;
|
|
64
|
+
const all = engines();
|
|
65
|
+
for (const [token, feeds] of Object.entries(all)) {
|
|
66
|
+
const e = engine(robots, list, token, feeds);
|
|
67
|
+
if (e.state !== 'ok') blocked++;
|
|
68
|
+
out.push(e);
|
|
69
|
+
}
|
|
70
|
+
const sentence =
|
|
71
|
+
blocked === 0
|
|
72
|
+
? sprintf('Every search crawler checked can reach the %d sampled pages, and none of them is asked not to index them.', list.length)
|
|
73
|
+
: sprintf('%1$d of %2$d search crawlers checked are kept from some of the sampled pages.', blocked, Object.keys(all).length);
|
|
74
|
+
return { engines: out, blocked, sentence };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Headers and metas of a page, as for_bot() reads them.
|
|
79
|
+
* @param {unknown} p
|
|
80
|
+
* @returns {[unknown[], unknown[]]}
|
|
81
|
+
*/
|
|
82
|
+
function pageDirectives(p) {
|
|
83
|
+
const page = /** @type {Record<string, unknown>} */ (p);
|
|
84
|
+
return [isset(p, 'headers') ? values(page.headers) : [], isset(p, 'metas') ? values(page.metas) : []];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* One engine.
|
|
89
|
+
* @param {import('./robots.js').RobotsTxt} robots
|
|
90
|
+
* @param {unknown[]} pages
|
|
91
|
+
* @param {string} token
|
|
92
|
+
* @param {string} feeds
|
|
93
|
+
* @returns {Engine}
|
|
94
|
+
*/
|
|
95
|
+
function engine(robots, pages, token, feeds) {
|
|
96
|
+
/** @type {string[]} */
|
|
97
|
+
const blocked = [];
|
|
98
|
+
/** @type {string[]} */
|
|
99
|
+
const noindexed = [];
|
|
100
|
+
let unchecked = 0;
|
|
101
|
+
let homeRule = '';
|
|
102
|
+
let homeOut = false;
|
|
103
|
+
for (const p of pages) {
|
|
104
|
+
const page = /** @type {Record<string, unknown>} */ (p);
|
|
105
|
+
const path = isset(p, 'path') ? str(page.path) : '/';
|
|
106
|
+
const rule = decisive(robots, token, path);
|
|
107
|
+
if (rule !== null && !rule.allow) {
|
|
108
|
+
blocked.push(path);
|
|
109
|
+
if (path === '/') {
|
|
110
|
+
homeOut = true;
|
|
111
|
+
homeRule = 'Disallow: ' + rule.path;
|
|
112
|
+
}
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const status = isset(p, 'status') ? int(page.status) : -1;
|
|
116
|
+
if (status === 0) {
|
|
117
|
+
unchecked++;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const [headers, metas] = pageDirectives(p);
|
|
121
|
+
if (noindex(forBot(headers, metas, token))) noindexed.push(path);
|
|
122
|
+
}
|
|
123
|
+
const name = token === 'bingbot' ? 'Bingbot' : token;
|
|
124
|
+
const who = sprintf('%1$s (%2$s)', feeds, name);
|
|
125
|
+
/** @type {string[]} */
|
|
126
|
+
const sentences = [];
|
|
127
|
+
const total = pages.length;
|
|
128
|
+
let state;
|
|
129
|
+
if (homeOut && blocked.length === total) {
|
|
130
|
+
state = 'blocked';
|
|
131
|
+
sentences.push(sprintf('%1$s: robots.txt blocks the whole site (%2$s), so %3$s cannot use this site in its answers.', who, homeRule, feeds));
|
|
132
|
+
} else if (blocked.length) {
|
|
133
|
+
state = 'partly';
|
|
134
|
+
sentences.push(
|
|
135
|
+
sprintf(
|
|
136
|
+
'%1$s: robots.txt blocks %2$d of %3$d sampled pages (%4$s), so %5$s cannot use them in its answers.',
|
|
137
|
+
who,
|
|
138
|
+
blocked.length,
|
|
139
|
+
total,
|
|
140
|
+
blocked.slice(0, 5).join(', '),
|
|
141
|
+
feeds,
|
|
142
|
+
),
|
|
143
|
+
);
|
|
144
|
+
} else {
|
|
145
|
+
state = 'ok';
|
|
146
|
+
sentences.push(sprintf('%1$s: robots.txt allows all %2$d sampled pages.', who, total));
|
|
147
|
+
}
|
|
148
|
+
if (noindexed.length) {
|
|
149
|
+
if (state === 'ok') state = 'partly';
|
|
150
|
+
sentences.push(
|
|
151
|
+
sprintf(
|
|
152
|
+
'%1$d sampled pages tell %2$s not to index them (noindex in a header or meta tag): %3$s.',
|
|
153
|
+
noindexed.length,
|
|
154
|
+
name,
|
|
155
|
+
noindexed.slice(0, 5).join(', '),
|
|
156
|
+
),
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
if (unchecked) {
|
|
160
|
+
sentences.push(sprintf("Couldn't check the headers of %d pages, because the site could not request itself.", unchecked));
|
|
161
|
+
}
|
|
162
|
+
return { token: name, feeds, state, blocked, noindex: noindexed, unchecked, rule: homeRule, sentences };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Snippet controls found on the sampled pages, explained by their effect.
|
|
167
|
+
* @param {Page[]} pages Pages, as for evaluate().
|
|
168
|
+
* @returns {string[]} Sentences.
|
|
169
|
+
*/
|
|
170
|
+
export function snippets(pages) {
|
|
171
|
+
/** @type {string[]} */
|
|
172
|
+
const out = [];
|
|
173
|
+
for (const p of values(pages)) {
|
|
174
|
+
const page = /** @type {Record<string, unknown>} */ (p);
|
|
175
|
+
const [headers, metas] = pageDirectives(p);
|
|
176
|
+
const dirs = forBot(headers, metas, 'Googlebot');
|
|
177
|
+
const path = isset(p, 'path') ? str(page.path) : '/';
|
|
178
|
+
if (nosnippet(dirs)) {
|
|
179
|
+
out.push(
|
|
180
|
+
sprintf('%s says nosnippet: Google shows no text from it, and its guide to AI features names this as a way to limit what they show.', path),
|
|
181
|
+
);
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
for (const d of dirs) {
|
|
185
|
+
const m = /^max-snippet:(\d+)$/.exec(d);
|
|
186
|
+
if (m) {
|
|
187
|
+
out.push(
|
|
188
|
+
sprintf(
|
|
189
|
+
'%1$s says max-snippet:%2$d: Google shows at most %3$d characters of its text, and its guide to AI features names this as a way to limit what they show.',
|
|
190
|
+
path,
|
|
191
|
+
m[1],
|
|
192
|
+
m[1],
|
|
193
|
+
),
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Robots.txt parser and matcher (port of Crawlers/Robots.php). Follows RFC 9309, so reachability is
|
|
3
|
+
* answered as a crawler would answer it.
|
|
4
|
+
*
|
|
5
|
+
* - Groups: one or more user-agent lines followed by rules. A user-agent line after a rule starts a
|
|
6
|
+
* new group. Lines outside the protocol (sitemap, content-signal, crawl-delay) do not end a group.
|
|
7
|
+
* - Group selection: a crawler obeys the groups whose user-agent value is its product token,
|
|
8
|
+
* compared without case; several matching groups are combined. Without one it obeys the "*"
|
|
9
|
+
* groups; without those, every path is allowed. A matching group with no rules allows everything.
|
|
10
|
+
* - Rules: the longest matching path wins; on a tie, allow wins. An empty path matches nothing.
|
|
11
|
+
* "*" matches any characters, and "$" at the end anchors the end ("$" elsewhere is literal).
|
|
12
|
+
* Paths are compared with non-ASCII bytes percent-encoded, escapes in upper case, and escaped
|
|
13
|
+
* unreserved characters decoded ("%62%61%7A" is "baz").
|
|
14
|
+
* - /robots.txt itself is always allowed. Only the first 500 KiB are read.
|
|
15
|
+
* Also kept: Sitemap lines, and Content-Signal lines (Cloudflare's Content Signals Policy) with the
|
|
16
|
+
* user agents of the group they sit in ("*" outside any group).
|
|
17
|
+
*
|
|
18
|
+
* The PHP works on bytes: the 500 KiB cut, the percent-encoding and the rule lengths count UTF-8
|
|
19
|
+
* bytes. This port parses a byte string (see toBytes) and returns text.
|
|
20
|
+
*/
|
|
21
|
+
import { trim, lower, toBytes, fromBytes } from './php.js';
|
|
22
|
+
|
|
23
|
+
/** Bytes read (RFC 9309, 2.5: at least 500 kibibytes). */
|
|
24
|
+
export const MAX_BYTES = 512000;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {object} Rule
|
|
28
|
+
* @property {boolean} allow
|
|
29
|
+
* @property {string} path Normalized pattern (ASCII).
|
|
30
|
+
*
|
|
31
|
+
* @typedef {object} Group
|
|
32
|
+
* @property {string[]} agents Lower-case product tokens, "*" or "".
|
|
33
|
+
* @property {Rule[]} rules
|
|
34
|
+
*
|
|
35
|
+
* @typedef {object} SignalLine
|
|
36
|
+
* @property {string[]} agents
|
|
37
|
+
* @property {Record<string, string>} signals Name => "yes" or "no".
|
|
38
|
+
*
|
|
39
|
+
* @typedef {object} RobotsTxt A parsed robots.txt. Plain data: it can be stored or sent as JSON.
|
|
40
|
+
* @property {Group[]} groups
|
|
41
|
+
* @property {string[]} sitemaps Sitemap addresses, in order.
|
|
42
|
+
* @property {SignalLine[]} signals Content-Signal lines, in order.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Parse a robots.txt file.
|
|
47
|
+
* @param {string} text File contents.
|
|
48
|
+
* @returns {RobotsTxt}
|
|
49
|
+
*/
|
|
50
|
+
export function parse(text) {
|
|
51
|
+
/** @type {RobotsTxt} */
|
|
52
|
+
const r = { groups: [], sitemaps: [], signals: [] };
|
|
53
|
+
let b = toBytes(String(text ?? '')).slice(0, MAX_BYTES);
|
|
54
|
+
if (b.startsWith('\xef\xbb\xbf')) b = b.slice(3); // a UTF-8 byte order mark
|
|
55
|
+
/** @type {Group | null} */
|
|
56
|
+
let current = null;
|
|
57
|
+
let inRules = false;
|
|
58
|
+
for (let line of b.split(/\r\n|\r|\n/)) {
|
|
59
|
+
const hash = line.indexOf('#');
|
|
60
|
+
if (hash >= 0) line = line.slice(0, hash);
|
|
61
|
+
const colon = line.indexOf(':');
|
|
62
|
+
if (colon < 0) continue;
|
|
63
|
+
const key = lower(trim(line.slice(0, colon)));
|
|
64
|
+
const value = trim(line.slice(colon + 1));
|
|
65
|
+
if (key === 'user-agent') {
|
|
66
|
+
if (current === null || inRules) {
|
|
67
|
+
current = { agents: [], rules: [] };
|
|
68
|
+
r.groups.push(current);
|
|
69
|
+
inRules = false;
|
|
70
|
+
}
|
|
71
|
+
current.agents.push(agentValue(value));
|
|
72
|
+
} else if (key === 'allow' || key === 'disallow') {
|
|
73
|
+
if (current === null) continue; // a rule outside any group applies to no crawler
|
|
74
|
+
inRules = true;
|
|
75
|
+
if (value !== '') current.rules.push({ allow: key === 'allow', path: normalizePattern(value) });
|
|
76
|
+
} else if (key === 'sitemap') {
|
|
77
|
+
if (value !== '') r.sitemaps.push(fromBytes(value));
|
|
78
|
+
} else if (key === 'content-signal') {
|
|
79
|
+
r.signals.push({ agents: current === null ? ['*'] : [...current.agents], signals: signalPairs(value) });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return r;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The product token in a user-agent value ("Googlebot/2.1" and "googlebot" both give "googlebot").
|
|
87
|
+
* @param {string} value
|
|
88
|
+
* @returns {string}
|
|
89
|
+
*/
|
|
90
|
+
function agentValue(value) {
|
|
91
|
+
const v = lower(trim(value));
|
|
92
|
+
if (v === '*' || v === '') return v;
|
|
93
|
+
const m = /^[a-z0-9_-]+/.exec(v);
|
|
94
|
+
return m ? m[0] : '';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Key and value pairs of a Content-Signal line ("search=yes, ai-train=no").
|
|
99
|
+
* @param {string} value Bytes.
|
|
100
|
+
* @returns {Record<string, string>}
|
|
101
|
+
*/
|
|
102
|
+
function signalPairs(value) {
|
|
103
|
+
/** @type {Record<string, string>} */
|
|
104
|
+
const out = {};
|
|
105
|
+
for (const pair of value.split(',')) {
|
|
106
|
+
const eq = pair.indexOf('=');
|
|
107
|
+
if (eq < 0) continue;
|
|
108
|
+
const k = lower(trim(pair.slice(0, eq)));
|
|
109
|
+
const v = lower(trim(pair.slice(eq + 1)));
|
|
110
|
+
if (k !== '' && (v === 'yes' || v === 'no')) out[fromBytes(k)] = v;
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* A rule path as compared: starting with "/" (or "*"), encoded like a request path.
|
|
117
|
+
* @param {string} path Bytes.
|
|
118
|
+
* @returns {string}
|
|
119
|
+
*/
|
|
120
|
+
function normalizePattern(path) {
|
|
121
|
+
let p = encode(path);
|
|
122
|
+
if (p !== '' && p[0] !== '/' && p[0] !== '*') p = '/' + p;
|
|
123
|
+
return p;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Percent-encode non-ASCII bytes and spaces, upper-case existing escapes, and decode escaped
|
|
128
|
+
* unreserved characters (RFC 3986: letters, digits, - . _ ~).
|
|
129
|
+
* @param {string} s Bytes.
|
|
130
|
+
* @returns {string} ASCII.
|
|
131
|
+
*/
|
|
132
|
+
function encode(s) {
|
|
133
|
+
const e = s.replace(/[\x80-\xff ]/g, (c) => '%' + c.charCodeAt(0).toString(16).toUpperCase().padStart(2, '0'));
|
|
134
|
+
return e.replace(/%[0-9a-fA-F]{2}/g, (m) => {
|
|
135
|
+
const c = String.fromCharCode(parseInt(m.slice(1), 16));
|
|
136
|
+
return /^[A-Za-z0-9._~-]$/.test(c) ? c : m.toUpperCase();
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The rules that apply to a crawler, or null when no group applies (everything is allowed).
|
|
142
|
+
* @param {RobotsTxt} r
|
|
143
|
+
* @param {string} token Product token, for example "GPTBot".
|
|
144
|
+
* @returns {Rule[] | null}
|
|
145
|
+
*/
|
|
146
|
+
export function rulesFor(r, token) {
|
|
147
|
+
const t = lower(trim(String(token ?? '')));
|
|
148
|
+
/** @type {Rule[] | null} */
|
|
149
|
+
let named = null;
|
|
150
|
+
/** @type {Rule[] | null} */
|
|
151
|
+
let star = null;
|
|
152
|
+
for (const g of r.groups) {
|
|
153
|
+
if (t !== '' && g.agents.includes(t)) named = [...(named ?? []), ...g.rules];
|
|
154
|
+
if (g.agents.includes('*')) star = [...(star ?? []), ...g.rules];
|
|
155
|
+
}
|
|
156
|
+
return named !== null ? named : star;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Whether a crawler names its own group (rather than following "*").
|
|
161
|
+
* @param {RobotsTxt} r
|
|
162
|
+
* @param {string} token
|
|
163
|
+
* @returns {boolean}
|
|
164
|
+
*/
|
|
165
|
+
export function names(r, token) {
|
|
166
|
+
const t = lower(trim(String(token ?? '')));
|
|
167
|
+
return r.groups.some((g) => g.agents.includes(t));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The rule that decides a path for a crawler, or null when no rule matches (allowed).
|
|
172
|
+
* @param {RobotsTxt} r
|
|
173
|
+
* @param {string} token
|
|
174
|
+
* @param {string} path Path with any query, for example "/shop/?page=2".
|
|
175
|
+
* @returns {Rule | null}
|
|
176
|
+
*/
|
|
177
|
+
export function decisive(r, token, path) {
|
|
178
|
+
const rules = rulesFor(r, token);
|
|
179
|
+
if (!rules || rules.length === 0) return null;
|
|
180
|
+
// A literal "*" or "$" in the address is matched by a pattern that writes it as %2A or %24.
|
|
181
|
+
const p = encode(toBytes(path === '' || path == null ? '/' : String(path)))
|
|
182
|
+
.replaceAll('*', '%2A')
|
|
183
|
+
.replaceAll('$', '%24');
|
|
184
|
+
if (p === '/robots.txt') return null;
|
|
185
|
+
/** @type {Rule | null} */
|
|
186
|
+
let best = null;
|
|
187
|
+
let len = -1;
|
|
188
|
+
for (const rule of rules) {
|
|
189
|
+
if (!matches(rule.path, p)) continue;
|
|
190
|
+
const l = rule.path.length;
|
|
191
|
+
if (l > len || (l === len && rule.allow && best !== null && !best.allow)) {
|
|
192
|
+
best = rule;
|
|
193
|
+
len = l;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return best;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Whether a crawler may fetch a path.
|
|
201
|
+
* @param {RobotsTxt} r
|
|
202
|
+
* @param {string} token
|
|
203
|
+
* @param {string} path
|
|
204
|
+
* @returns {boolean}
|
|
205
|
+
*/
|
|
206
|
+
export function allowed(r, token, path) {
|
|
207
|
+
const rule = decisive(r, token, path);
|
|
208
|
+
return rule === null || rule.allow;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Whether a rule pattern matches a path from its start.
|
|
213
|
+
* @param {string} pattern Pattern ("*" and a final "$" are special).
|
|
214
|
+
* @param {string} path
|
|
215
|
+
* @returns {boolean}
|
|
216
|
+
*/
|
|
217
|
+
export function matches(pattern, path) {
|
|
218
|
+
let p = String(pattern ?? '');
|
|
219
|
+
if (p === '') return false;
|
|
220
|
+
const anchored = p.endsWith('$');
|
|
221
|
+
if (anchored) p = p.slice(0, -1);
|
|
222
|
+
// "$" is special only as the last character; elsewhere it is a literal "$", which decisive()
|
|
223
|
+
// writes as %24 in the path.
|
|
224
|
+
const parts = p.split('*').map((part) => part.replace(/[\\^$.*+?()[\]{}|/-]/g, (c) => (c === '$' ? '(?:\\$|%24)' : '\\' + c)));
|
|
225
|
+
// PHP's "$" also matches before a final newline.
|
|
226
|
+
return new RegExp('^' + parts.join('[\\s\\S]*') + (anchored ? '\\n?$' : '')).test(String(path ?? ''));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Sitemap addresses listed.
|
|
231
|
+
* @param {RobotsTxt} r
|
|
232
|
+
* @returns {string[]}
|
|
233
|
+
*/
|
|
234
|
+
export function sitemaps(r) {
|
|
235
|
+
return r.sitemaps;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Content-Signal lines.
|
|
240
|
+
* @param {RobotsTxt} r
|
|
241
|
+
* @returns {SignalLine[]}
|
|
242
|
+
*/
|
|
243
|
+
export function signals(r) {
|
|
244
|
+
return r.signals;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The Content-Signal values that apply to every crawler: lines outside a group or in a "*" group,
|
|
249
|
+
* later lines win. Like PHP's array_merge(), a name that is a whole number is not overwritten but
|
|
250
|
+
* added under the next free number (PHP stores "5" as the integer key 5).
|
|
251
|
+
* @param {RobotsTxt} r
|
|
252
|
+
* @returns {Record<string, string>}
|
|
253
|
+
*/
|
|
254
|
+
export function generalSignals(r) {
|
|
255
|
+
/** @type {Record<string, string>} */
|
|
256
|
+
const out = {};
|
|
257
|
+
let next = 0;
|
|
258
|
+
for (const s of r.signals) {
|
|
259
|
+
if (!s.agents.includes('*')) continue;
|
|
260
|
+
for (const [k, v] of Object.entries(s.signals)) {
|
|
261
|
+
if (/^(?:0|-?[1-9][0-9]{0,17})$/.test(k)) out[String(next++)] = v;
|
|
262
|
+
else out[k] = v;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return out;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Number of groups.
|
|
270
|
+
* @param {RobotsTxt} r
|
|
271
|
+
* @returns {number}
|
|
272
|
+
*/
|
|
273
|
+
export function groupCount(r) {
|
|
274
|
+
return r.groups.length;
|
|
275
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Secrets in code (port of Signals/Secrets.php): finds credentials written into source files.
|
|
3
|
+
* Results carry only a masked form of each value; the value itself is never returned. The formats
|
|
4
|
+
* and rules live in rules/data/secrets.json, which goose-tools checks against the PHP.
|
|
5
|
+
*
|
|
6
|
+
* The PHP works on bytes: the 2000-byte line limit, the mask, and the entropy count UTF-8 bytes.
|
|
7
|
+
* This port runs on byte strings and returns text.
|
|
8
|
+
*/
|
|
9
|
+
import data from '../../rules/data/secrets.json.js' ;
|
|
10
|
+
import { str, toBytes, fromBytes, phpRegex } from './php.js';
|
|
11
|
+
|
|
12
|
+
/** Known formats, in the order they are tried on each line. */
|
|
13
|
+
export const PATTERNS = data.patterns.list.map((p) => ({ type: p.type, re: phpRegex(p.source, p.flags) }));
|
|
14
|
+
|
|
15
|
+
/** A known-format match that contains this is a documentation example. */
|
|
16
|
+
const EXAMPLE = phpRegex(data.example_filter.source, data.example_filter.flags);
|
|
17
|
+
|
|
18
|
+
/** A secret-like name assigned a quoted literal. */
|
|
19
|
+
const GENERIC = phpRegex(data.generic.source, data.generic.flags);
|
|
20
|
+
|
|
21
|
+
const PLACEHOLDER = phpRegex(data.random.placeholder.source, data.random.placeholder.flags);
|
|
22
|
+
const CLASSES = data.random.classes.map((c) => phpRegex(c));
|
|
23
|
+
const SPLIT = phpRegex(data.lines.split.source, data.lines.split.flags);
|
|
24
|
+
const REMASK = phpRegex(data.remask.source, data.remask.flags);
|
|
25
|
+
|
|
26
|
+
/** Plain names for each type. */
|
|
27
|
+
export const LABELS = Object.freeze({ ...data.labels });
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {object} Finding
|
|
31
|
+
* @property {number} line Line number, from 1.
|
|
32
|
+
* @property {string} type aws, stripe, google, github, slack, openai, private_key or generic.
|
|
33
|
+
* @property {string} masked
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Find secrets in a file's content.
|
|
38
|
+
* @param {string} content
|
|
39
|
+
* @returns {Finding[]}
|
|
40
|
+
*/
|
|
41
|
+
export function scan(content) {
|
|
42
|
+
/** @type {Finding[]} */
|
|
43
|
+
const found = [];
|
|
44
|
+
const lines = toBytes(str(content)).split(SPLIT);
|
|
45
|
+
lines.forEach((line, i) => {
|
|
46
|
+
if (line.length > data.lines.max_bytes) return; // minified or data lines
|
|
47
|
+
let hit = false;
|
|
48
|
+
for (const { type, re } of PATTERNS) {
|
|
49
|
+
const m = re.exec(line);
|
|
50
|
+
if (m && !EXAMPLE.test(m[0])) {
|
|
51
|
+
found.push({ line: i + 1, type, masked: type === 'private_key' ? data.mask.private_key : maskBytes(m[0]) });
|
|
52
|
+
hit = true;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (!hit) {
|
|
57
|
+
const m = GENERIC.exec(line);
|
|
58
|
+
if (m && randomBytes(m[data.generic.value_group])) found.push({ line: i + 1, type: 'generic', masked: maskBytes(m[data.generic.value_group]) });
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return found;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Mask a byte string: only the end shows (the last 4 bytes, the last 2 of a value under 16 bytes,
|
|
66
|
+
* nothing of one under 8).
|
|
67
|
+
* @param {string} b
|
|
68
|
+
* @returns {string}
|
|
69
|
+
*/
|
|
70
|
+
function maskBytes(b) {
|
|
71
|
+
const rule = data.mask.show_last.find((r) => b.length >= r.min_bytes);
|
|
72
|
+
const last = rule ? rule.last : 0;
|
|
73
|
+
return data.mask.dots + (last ? fromBytes(b.slice(-last)) : '');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Only the end shows, enough to find the value.
|
|
78
|
+
* @param {string} s Secret.
|
|
79
|
+
* @returns {string}
|
|
80
|
+
*/
|
|
81
|
+
export function mask(s) {
|
|
82
|
+
return maskBytes(toBytes(str(s)));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* A mask stored in the older format (first and last 4 characters) in the current one.
|
|
87
|
+
* @param {string} masked
|
|
88
|
+
* @returns {string}
|
|
89
|
+
*/
|
|
90
|
+
export function remask(masked) {
|
|
91
|
+
const s = str(masked);
|
|
92
|
+
const m = REMASK.exec(s);
|
|
93
|
+
return m ? data.mask.dots + m[1] : s;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Whether a byte string looks like a real random secret rather than a placeholder or a variable.
|
|
98
|
+
* @param {string} b
|
|
99
|
+
* @returns {boolean}
|
|
100
|
+
*/
|
|
101
|
+
function randomBytes(b) {
|
|
102
|
+
if (PLACEHOLDER.test(b)) return false;
|
|
103
|
+
const classes = CLASSES.filter((re) => re.test(b)).length;
|
|
104
|
+
return classes >= data.random.min_classes && entropyBytes(b) >= data.random.min_entropy;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Whether a value looks like a real random secret rather than a placeholder or a variable.
|
|
109
|
+
* @param {string} s
|
|
110
|
+
* @returns {boolean}
|
|
111
|
+
*/
|
|
112
|
+
export function random(s) {
|
|
113
|
+
return randomBytes(toBytes(str(s)));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Shannon entropy of a byte string, in bits per byte.
|
|
118
|
+
* @param {string} b
|
|
119
|
+
* @returns {number}
|
|
120
|
+
*/
|
|
121
|
+
function entropyBytes(b) {
|
|
122
|
+
const len = b.length;
|
|
123
|
+
if (!len) return 0;
|
|
124
|
+
const counts = new Array(256).fill(0);
|
|
125
|
+
for (let i = 0; i < len; i++) counts[b.charCodeAt(i)]++;
|
|
126
|
+
let h = 0;
|
|
127
|
+
for (const n of counts) {
|
|
128
|
+
if (!n) continue;
|
|
129
|
+
const p = n / len;
|
|
130
|
+
h -= p * (Math.log(p) / Math.log(2));
|
|
131
|
+
}
|
|
132
|
+
return h;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Shannon entropy in bits per byte (PHP counts bytes, so "éé" is 1 bit).
|
|
137
|
+
* @param {string} s
|
|
138
|
+
* @returns {number}
|
|
139
|
+
*/
|
|
140
|
+
export function entropy(s) {
|
|
141
|
+
return entropyBytes(toBytes(str(s)));
|
|
142
|
+
}
|