@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,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Goose Tools rules engine: crawler, exposure and front-end checks as pure functions. No
|
|
3
|
+
* network, no storage, no Node-only APIs, so the same code runs in Node, a Worker and a browser.
|
|
4
|
+
*
|
|
5
|
+
* Facts come from rules/data/*.json through JSON import attributes (with { type: 'json' }). The
|
|
6
|
+
* data files stay the single copy that goose-tools tests against the PHP, and bundlers inline
|
|
7
|
+
* them, so no file is read at run time.
|
|
8
|
+
*/
|
|
9
|
+
export * as html from './html.js';
|
|
10
|
+
export * as robots from './robots.js';
|
|
11
|
+
export * as directives from './directives.js';
|
|
12
|
+
export * as bots from './bots.js';
|
|
13
|
+
export * as reach from './reach.js';
|
|
14
|
+
export * as signals from './signals.js';
|
|
15
|
+
export * as policies from './policies.js';
|
|
16
|
+
export * as hiddenText from './hidden-text.js';
|
|
17
|
+
export * as exposure from './exposure.js';
|
|
18
|
+
export * as secrets from './secrets.js';
|
|
19
|
+
export * as headers from './headers.js';
|
|
20
|
+
export * as frontend from './frontend.js';
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small helpers that copy PHP behavior the ported checks depend on.
|
|
3
|
+
*
|
|
4
|
+
* The PHP code in goose-tools is the source of truth, and several of its built-in functions behave
|
|
5
|
+
* differently from their JavaScript look-alikes:
|
|
6
|
+
* - trim() strips only " \t\n\r\0\x0B". String.prototype.trim also strips U+00A0, U+FEFF and more.
|
|
7
|
+
* - strtolower() changes ASCII letters only (PHP 8.2 and later). toLowerCase changes every script.
|
|
8
|
+
* - strlen() and substr() count UTF-8 bytes, not UTF-16 code units.
|
|
9
|
+
* - Regexes without the u flag treat \s as ASCII whitespace only.
|
|
10
|
+
* - (int), (string), empty() and isset() have their own rules for null, booleans and strings.
|
|
11
|
+
*
|
|
12
|
+
* Plain JavaScript. TextEncoder and TextDecoder exist in browsers, Workers and Node.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** The characters PHP's trim() strips by default. */
|
|
16
|
+
export const PHP_TRIM = ' \t\n\r\0\x0B';
|
|
17
|
+
|
|
18
|
+
/** PHP's \s without the u flag: space, tab, newline, vertical tab, form feed, carriage return. */
|
|
19
|
+
export const WS = '[\\t\\n\\v\\f\\r ]';
|
|
20
|
+
|
|
21
|
+
/** PHP's \S without the u flag. */
|
|
22
|
+
export const NWS = '[^\\t\\n\\v\\f\\r ]';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* PHP's \s with the u flag. PHP turns on PCRE2_UCP with u, so \s is \p{Zs}, \h and \v:
|
|
26
|
+
* the ASCII spaces, U+0085, U+00A0, U+1680, U+180E, U+2000 to U+200A, U+2028, U+2029, U+202F,
|
|
27
|
+
* U+205F and U+3000. JavaScript's \s differs: it adds U+FEFF and leaves out U+0085 and U+180E.
|
|
28
|
+
*/
|
|
29
|
+
export const UWS = '[\\t\\n\\v\\f\\r \\u0085\\u00a0\\u1680\\u180e\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000]';
|
|
30
|
+
|
|
31
|
+
const encoder = new TextEncoder();
|
|
32
|
+
const decoder = new TextDecoder();
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* PHP trim(): strip the given characters from both ends.
|
|
36
|
+
* @param {string} s
|
|
37
|
+
* @param {string} [chars]
|
|
38
|
+
* @returns {string}
|
|
39
|
+
*/
|
|
40
|
+
export function trim(s, chars = PHP_TRIM) {
|
|
41
|
+
let start = 0;
|
|
42
|
+
let end = s.length;
|
|
43
|
+
while (start < end && chars.includes(s[start])) start++;
|
|
44
|
+
while (end > start && chars.includes(s[end - 1])) end--;
|
|
45
|
+
return start === 0 && end === s.length ? s : s.slice(start, end);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* PHP rtrim(): strip the given characters from the end.
|
|
50
|
+
* @param {string} s
|
|
51
|
+
* @param {string} [chars]
|
|
52
|
+
* @returns {string}
|
|
53
|
+
*/
|
|
54
|
+
export function rtrim(s, chars = PHP_TRIM) {
|
|
55
|
+
let end = s.length;
|
|
56
|
+
while (end > 0 && chars.includes(s[end - 1])) end--;
|
|
57
|
+
return end === s.length ? s : s.slice(0, end);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* PHP strtolower() since PHP 8.2: ASCII letters only.
|
|
62
|
+
* @param {string} s
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
65
|
+
export function lower(s) {
|
|
66
|
+
return /[A-Z]/.test(s) ? s.replace(/[A-Z]+/g, (m) => m.toLowerCase()) : s;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Length of a string in UTF-8 bytes (PHP strlen() of the same text).
|
|
71
|
+
* @param {string} s
|
|
72
|
+
* @returns {number}
|
|
73
|
+
*/
|
|
74
|
+
export function byteLength(s) {
|
|
75
|
+
let n = 0;
|
|
76
|
+
for (let i = 0; i < s.length; i++) {
|
|
77
|
+
const c = s.charCodeAt(i);
|
|
78
|
+
if (c < 0x80) n += 1;
|
|
79
|
+
else if (c < 0x800) n += 2;
|
|
80
|
+
else if (c >= 0xd800 && c <= 0xdbff && i + 1 < s.length) {
|
|
81
|
+
const d = s.charCodeAt(i + 1);
|
|
82
|
+
if (d >= 0xdc00 && d <= 0xdfff) {
|
|
83
|
+
n += 4;
|
|
84
|
+
i++;
|
|
85
|
+
} else n += 3;
|
|
86
|
+
} else n += 3;
|
|
87
|
+
}
|
|
88
|
+
return n;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The UTF-8 bytes of a string as a "byte string": one character per byte, codes 0 to 255.
|
|
93
|
+
* PHP code that counts or cuts bytes is ported by running it on byte strings.
|
|
94
|
+
* @param {string} s
|
|
95
|
+
* @returns {string}
|
|
96
|
+
*/
|
|
97
|
+
export function toBytes(s) {
|
|
98
|
+
if (!/[^\x00-\x7f]/.test(s)) return s;
|
|
99
|
+
const u8 = encoder.encode(s);
|
|
100
|
+
let out = '';
|
|
101
|
+
for (let i = 0; i < u8.length; i += 8192) {
|
|
102
|
+
out += String.fromCharCode.apply(null, /** @type {any} */ (u8.subarray(i, i + 8192)));
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Text from a byte string. Invalid UTF-8 (a value cut inside a character) becomes U+FFFD; PHP would
|
|
109
|
+
* keep the broken bytes, which its JSON encoder then refuses.
|
|
110
|
+
* @param {string} b
|
|
111
|
+
* @returns {string}
|
|
112
|
+
*/
|
|
113
|
+
export function fromBytes(b) {
|
|
114
|
+
if (!/[^\x00-\x7f]/.test(b)) return b;
|
|
115
|
+
const u8 = new Uint8Array(b.length);
|
|
116
|
+
for (let i = 0; i < b.length; i++) u8[i] = b.charCodeAt(i);
|
|
117
|
+
return decoder.decode(u8);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The first `max` UTF-8 bytes of a string, as text (PHP substr($s, 0, $max)).
|
|
122
|
+
* @param {string} s
|
|
123
|
+
* @param {number} max
|
|
124
|
+
* @returns {string}
|
|
125
|
+
*/
|
|
126
|
+
export function cutBytes(s, max) {
|
|
127
|
+
if (s.length * 3 <= max) return s;
|
|
128
|
+
// Whether it fits, counted natively: encodeInto stops when the buffer is full.
|
|
129
|
+
if (s.length <= max && encoder.encodeInto(s, fitBuffer(max)).read === s.length) return s;
|
|
130
|
+
return decoder.decode(encoder.encode(s).subarray(0, max));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** @type {Uint8Array | null} */
|
|
134
|
+
let fit = null;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A reused buffer of exactly `max` bytes.
|
|
138
|
+
* @param {number} max
|
|
139
|
+
* @returns {Uint8Array}
|
|
140
|
+
*/
|
|
141
|
+
function fitBuffer(max) {
|
|
142
|
+
if (!fit || fit.length !== max) fit = new Uint8Array(max);
|
|
143
|
+
return fit;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* PHP (int) cast.
|
|
148
|
+
* @param {unknown} v
|
|
149
|
+
* @returns {number}
|
|
150
|
+
*/
|
|
151
|
+
export function int(v) {
|
|
152
|
+
if (typeof v === 'number') return Number.isFinite(v) ? Math.trunc(v) : 0;
|
|
153
|
+
if (typeof v === 'boolean') return v ? 1 : 0;
|
|
154
|
+
if (typeof v === 'string') {
|
|
155
|
+
const m = /^[ \t\n\r\v\f]*([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)/.exec(v);
|
|
156
|
+
if (!m) return 0;
|
|
157
|
+
const n = Number(m[1]);
|
|
158
|
+
return Number.isFinite(n) ? Math.trunc(n) : 0;
|
|
159
|
+
}
|
|
160
|
+
if (Array.isArray(v)) return v.length ? 1 : 0;
|
|
161
|
+
if (v && typeof v === 'object') return Object.keys(v).length ? 1 : 0;
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* PHP (string) cast for the scalar values JSON can carry.
|
|
167
|
+
* @param {unknown} v
|
|
168
|
+
* @returns {string}
|
|
169
|
+
*/
|
|
170
|
+
export function str(v) {
|
|
171
|
+
if (v === null || v === undefined || v === false) return '';
|
|
172
|
+
if (v === true) return '1';
|
|
173
|
+
if (typeof v === 'string') return v;
|
|
174
|
+
if (typeof v === 'number') return String(v);
|
|
175
|
+
return 'Array';
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* PHP empty().
|
|
180
|
+
* @param {unknown} v
|
|
181
|
+
* @returns {boolean}
|
|
182
|
+
*/
|
|
183
|
+
export function empty(v) {
|
|
184
|
+
if (v === null || v === undefined || v === false || v === 0 || v === '' || v === '0') return true;
|
|
185
|
+
if (Array.isArray(v)) return v.length === 0;
|
|
186
|
+
if (typeof v === 'object') return Object.keys(/** @type {object} */ (v)).length === 0;
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* PHP isset($a[$key]): the value exists and is not null.
|
|
192
|
+
* @param {unknown} a
|
|
193
|
+
* @param {string} key
|
|
194
|
+
* @returns {boolean}
|
|
195
|
+
*/
|
|
196
|
+
export function isset(a, key) {
|
|
197
|
+
if (a === null || typeof a !== 'object') return false;
|
|
198
|
+
const v = /** @type {Record<string, unknown>} */ (a)[key];
|
|
199
|
+
return v !== null && v !== undefined;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* PHP (array) cast, as a list of values in order (what foreach visits).
|
|
204
|
+
* @param {unknown} v
|
|
205
|
+
* @returns {unknown[]}
|
|
206
|
+
*/
|
|
207
|
+
export function values(v) {
|
|
208
|
+
if (v === null || v === undefined) return [];
|
|
209
|
+
if (Array.isArray(v)) return v;
|
|
210
|
+
if (typeof v === 'object') return Object.values(v);
|
|
211
|
+
return [v];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Whether a value is a PHP array (a JSON list or object).
|
|
216
|
+
* @param {unknown} v
|
|
217
|
+
* @returns {v is Record<string, unknown> | unknown[]}
|
|
218
|
+
*/
|
|
219
|
+
export function isArray(v) {
|
|
220
|
+
return v !== null && typeof v === 'object';
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* PHP sprintf() for the conversions the ported sentences use: %s, %d and %1$s style positions.
|
|
225
|
+
* @param {string} format
|
|
226
|
+
* @param {...unknown} args
|
|
227
|
+
* @returns {string}
|
|
228
|
+
*/
|
|
229
|
+
export function sprintf(format, ...args) {
|
|
230
|
+
let next = 0;
|
|
231
|
+
return format.replace(/%(?:(\d+)\$)?([sd%])/g, (_, pos, type) => {
|
|
232
|
+
if (type === '%') return '%';
|
|
233
|
+
const v = args[pos ? Number(pos) - 1 : next++];
|
|
234
|
+
return type === 'd' ? intString(v) : str(v);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const INT_MAX = 9223372036854775807n;
|
|
239
|
+
const INT_MIN = -9223372036854775808n;
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* PHP (int) as decimal text. A string of digits past the 64-bit range is clamped as PHP does,
|
|
243
|
+
* without losing digits to floating point.
|
|
244
|
+
* @param {unknown} v
|
|
245
|
+
* @returns {string}
|
|
246
|
+
*/
|
|
247
|
+
function intString(v) {
|
|
248
|
+
if (typeof v === 'string') {
|
|
249
|
+
const m = /^[ \t\n\r\v\f]*([+-]?\d+)(?![.eE\d])/.exec(v);
|
|
250
|
+
if (m) {
|
|
251
|
+
let b = BigInt(m[1]);
|
|
252
|
+
if (b > INT_MAX) b = INT_MAX;
|
|
253
|
+
if (b < INT_MIN) b = INT_MIN;
|
|
254
|
+
return b.toString();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return String(int(v));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Escape text for use inside a RegExp.
|
|
262
|
+
* @param {string} s
|
|
263
|
+
* @returns {string}
|
|
264
|
+
*/
|
|
265
|
+
export function escapeRegExp(s) {
|
|
266
|
+
return s.replace(/[\\^$.*+?()[\]{}|/-]/g, '\\$&');
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* A PHP (PCRE) regex as a JavaScript RegExp with the same meaning on byte strings.
|
|
271
|
+
*
|
|
272
|
+
* Rewrites what PCRE and JavaScript read differently:
|
|
273
|
+
* - \s and \S are ASCII whitespace (PHP without the u flag).
|
|
274
|
+
* - "." does not match "\n" only (JavaScript also stops at "\r", U+2028 and U+2029).
|
|
275
|
+
* - "^" and "$" with m: after or before "\n" only (JavaScript also uses "\r", U+2028, U+2029).
|
|
276
|
+
* - "$" without m: at the end or before a final "\n".
|
|
277
|
+
* Everything else is passed through; the patterns used here need nothing more.
|
|
278
|
+
* @param {string} source Pattern between the PHP delimiters.
|
|
279
|
+
* @param {string} [flags] PHP modifiers among i, m, s, u.
|
|
280
|
+
* @returns {RegExp}
|
|
281
|
+
*/
|
|
282
|
+
export function phpRegex(source, flags = '') {
|
|
283
|
+
const multi = flags.includes('m');
|
|
284
|
+
const dotAll = flags.includes('s');
|
|
285
|
+
let out = '';
|
|
286
|
+
let inClass = false;
|
|
287
|
+
for (let k = 0; k < source.length; k++) {
|
|
288
|
+
const c = source[k];
|
|
289
|
+
if (c === '\\') {
|
|
290
|
+
const d = source[k + 1];
|
|
291
|
+
k++;
|
|
292
|
+
if (d === 's') out += inClass ? '\\t\\n\\v\\f\\r ' : WS;
|
|
293
|
+
else if (d === 'S' && !inClass) out += NWS;
|
|
294
|
+
else if (d === 'S') throw new Error('\\S inside a class is not supported');
|
|
295
|
+
else out += c + d;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (inClass) {
|
|
299
|
+
if (c === ']') inClass = false;
|
|
300
|
+
out += c;
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
if (c === '[') {
|
|
304
|
+
inClass = true;
|
|
305
|
+
out += c;
|
|
306
|
+
if (source[k + 1] === '^') out += source[++k];
|
|
307
|
+
if (source[k + 1] === ']') out += source[++k];
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
if (c === '.') out += dotAll ? '[\\s\\S]' : '[^\\n]';
|
|
311
|
+
else if (c === '^') out += multi ? '(?<![^\\n])' : '^';
|
|
312
|
+
else if (c === '$') out += multi ? '(?![^\\n])' : '(?=\\n?$)';
|
|
313
|
+
else out += c;
|
|
314
|
+
}
|
|
315
|
+
return new RegExp(out, flags.replace(/[ms]/g, ''));
|
|
316
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pointers and policies (port of Crawlers/Policies.php). Shapes facts gathered elsewhere into
|
|
3
|
+
* plain findings:
|
|
4
|
+
* - the sitemap is listed in robots.txt and loads;
|
|
5
|
+
* - the privacy page and, for stores, the terms page exist and are published;
|
|
6
|
+
* - both are linked from every page (from link records, else from a sample of pages);
|
|
7
|
+
* - AI signals agree (signals.check());
|
|
8
|
+
* - no hidden text or text addressed to AI in content (hidden-text.scan()).
|
|
9
|
+
* Each finding is ok, recommended or unchecked. Nothing here changes the site.
|
|
10
|
+
*
|
|
11
|
+
* The setting names in the sentences (Settings > Privacy, WooCommerce > Settings > Advanced) are the
|
|
12
|
+
* WordPress plugin's, kept as the PHP writes them.
|
|
13
|
+
*/
|
|
14
|
+
import { str, int, empty, isset, isArray, values, sprintf } from './php.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {import('./signals.js').Finding} Finding
|
|
18
|
+
*
|
|
19
|
+
* @typedef {object} Report
|
|
20
|
+
* @property {number} time
|
|
21
|
+
* @property {Finding[]} items
|
|
22
|
+
* @property {number} recommended
|
|
23
|
+
* @property {number} unchecked
|
|
24
|
+
* @property {string} sentence
|
|
25
|
+
* @property {unknown[]} hidden
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The value at a key as a PHP (array) cast, or an empty object.
|
|
30
|
+
* @param {unknown} f
|
|
31
|
+
* @param {string} key
|
|
32
|
+
* @returns {Record<string, unknown>}
|
|
33
|
+
*/
|
|
34
|
+
function arr(f, key) {
|
|
35
|
+
if (!isset(f, key)) return {};
|
|
36
|
+
const v = /** @type {Record<string, unknown>} */ (f)[key];
|
|
37
|
+
return isArray(v) ? /** @type {Record<string, unknown>} */ (v) : { 0: v };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Build the report.
|
|
42
|
+
* @param {Record<string, unknown>} f Facts: robots (virtual, file, not_root or unchecked), sitemaps
|
|
43
|
+
* (each {url, local, status, xml}), privacy and terms ({applies, set, published, url, title}),
|
|
44
|
+
* linking ({method: links, sample or none; privacy and terms: {pages, linked, missing}}), signals
|
|
45
|
+
* (signals.check() output), hidden ({items: each {title, url, kind, how, text, phrase}, scanned,
|
|
46
|
+
* total}).
|
|
47
|
+
* @param {number} time Unix time.
|
|
48
|
+
* @returns {Report}
|
|
49
|
+
*/
|
|
50
|
+
export function build(f, time) {
|
|
51
|
+
const privacy = arr(f, 'privacy');
|
|
52
|
+
const terms = arr(f, 'terms');
|
|
53
|
+
/** @type {Finding[]} */
|
|
54
|
+
const items = [
|
|
55
|
+
...sitemaps(isset(f, 'sitemaps') ? values(f.sitemaps) : [], isset(f, 'robots') ? str(f.robots) : 'unchecked'),
|
|
56
|
+
...page('privacy', 'privacy policy', privacy, 'Settings > Privacy'),
|
|
57
|
+
...page('terms', 'terms and conditions', terms, 'WooCommerce > Settings > Advanced'),
|
|
58
|
+
...linking(arr(f, 'linking'), privacy, terms),
|
|
59
|
+
...(/** @type {Finding[]} */ (isset(f, 'signals') ? values(f.signals) : [])),
|
|
60
|
+
...hidden(arr(f, 'hidden')),
|
|
61
|
+
];
|
|
62
|
+
let recommended = 0;
|
|
63
|
+
let unchecked = 0;
|
|
64
|
+
for (const i of items) {
|
|
65
|
+
if (i.level === 'recommended') recommended++;
|
|
66
|
+
if (i.level === 'unchecked') unchecked++;
|
|
67
|
+
}
|
|
68
|
+
let sentence;
|
|
69
|
+
if (recommended) {
|
|
70
|
+
sentence = sprintf('%1$d %2$s about pointers and policies.', recommended, recommended === 1 ? 'item is recommended' : 'items are recommended');
|
|
71
|
+
} else if (unchecked) {
|
|
72
|
+
sentence = 'Nothing is recommended in what could be checked, but some checks could not run.';
|
|
73
|
+
} else {
|
|
74
|
+
sentence = 'Pointers and policies are in place: the sitemap is listed, the policies are published and linked, and the AI signals agree.';
|
|
75
|
+
}
|
|
76
|
+
const h = arr(f, 'hidden');
|
|
77
|
+
return {
|
|
78
|
+
time: int(time),
|
|
79
|
+
items,
|
|
80
|
+
recommended,
|
|
81
|
+
unchecked,
|
|
82
|
+
sentence,
|
|
83
|
+
hidden: isset(h, 'items') ? values(h.items) : [],
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Sitemap findings.
|
|
89
|
+
* @param {unknown[]} list Sitemaps listed in robots.txt.
|
|
90
|
+
* @param {string} source Where robots.txt comes from.
|
|
91
|
+
* @returns {Finding[]}
|
|
92
|
+
*/
|
|
93
|
+
function sitemaps(list, source) {
|
|
94
|
+
if (source === 'unchecked') {
|
|
95
|
+
return [item('sitemap', 'unchecked', "Couldn't read robots.txt, so the sitemap line was not checked.")];
|
|
96
|
+
}
|
|
97
|
+
if (!list.length) {
|
|
98
|
+
return [item('sitemap', 'recommended', 'robots.txt lists no sitemap. A Sitemap: line there is where crawlers and agents look for one.')];
|
|
99
|
+
}
|
|
100
|
+
/** @type {Finding[]} */
|
|
101
|
+
const out = [];
|
|
102
|
+
for (const raw of list) {
|
|
103
|
+
const s = /** @type {Record<string, unknown>} */ (raw);
|
|
104
|
+
const url = isset(raw, 'url') ? str(s.url) : '';
|
|
105
|
+
const status = isset(raw, 'status') ? int(s.status) : -1;
|
|
106
|
+
if (empty(isArray(raw) ? s.local : undefined)) {
|
|
107
|
+
out.push(item('sitemap', 'ok', sprintf('robots.txt lists a sitemap on another site (%s); it was not requested.', url)));
|
|
108
|
+
} else if (status < 0) {
|
|
109
|
+
out.push(item('sitemap', 'unchecked', sprintf("Couldn't check the sitemap listed in robots.txt (%s).", url)));
|
|
110
|
+
} else if (status === 0) {
|
|
111
|
+
out.push(item('sitemap', 'unchecked', sprintf("Couldn't load the sitemap listed in robots.txt (%s), because the site could not request itself.", url)));
|
|
112
|
+
} else if (status === 200 && !empty(s.xml)) {
|
|
113
|
+
out.push(item('sitemap', 'ok', sprintf('robots.txt lists the sitemap %s, and it loads.', url)));
|
|
114
|
+
} else if (status === 200) {
|
|
115
|
+
out.push(item('sitemap', 'recommended', sprintf('The sitemap listed in robots.txt (%s) loads, but is not a sitemap (no urlset or sitemapindex).', url)));
|
|
116
|
+
} else {
|
|
117
|
+
out.push(item('sitemap', 'recommended', sprintf('The sitemap listed in robots.txt (%1$s) does not load (HTTP %2$d).', url, status)));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* A policy page's findings.
|
|
125
|
+
* @param {string} id privacy or terms.
|
|
126
|
+
* @param {string} what Plain name.
|
|
127
|
+
* @param {Record<string, unknown>} p {applies, set, published, url, title}.
|
|
128
|
+
* @param {string} setting Where the setting lives.
|
|
129
|
+
* @returns {Finding[]}
|
|
130
|
+
*/
|
|
131
|
+
function page(id, what, p, setting) {
|
|
132
|
+
if (empty(p.applies)) return [];
|
|
133
|
+
if (empty(p.set)) {
|
|
134
|
+
return [item(id, 'recommended', sprintf('No %1$s page is set (%2$s). Agents and shoppers look for one.', what, setting))];
|
|
135
|
+
}
|
|
136
|
+
if (empty(p.published)) {
|
|
137
|
+
return [item(id, 'recommended', sprintf('The %1$s page set in %2$s is not published, so visitors and crawlers cannot open it.', what, setting))];
|
|
138
|
+
}
|
|
139
|
+
return [
|
|
140
|
+
item(id, 'ok', sprintf('The %1$s page is published: %2$s (%3$s).', what, isset(p, 'title') ? str(p.title) : '', isset(p, 'url') ? str(p.url) : '')),
|
|
141
|
+
];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Whether the policy pages are linked from every page.
|
|
146
|
+
* @param {Record<string, unknown>} l Linking facts: method (links, sample or none), and per policy
|
|
147
|
+
* (privacy, terms) {pages, linked, missing}.
|
|
148
|
+
* @param {Record<string, unknown>} privacy
|
|
149
|
+
* @param {Record<string, unknown>} terms
|
|
150
|
+
* @returns {Finding[]}
|
|
151
|
+
*/
|
|
152
|
+
function linking(l, privacy, terms) {
|
|
153
|
+
const method = isset(l, 'method') ? str(l.method) : 'none';
|
|
154
|
+
/** @type {Finding[]} */
|
|
155
|
+
const out = [];
|
|
156
|
+
/** @type {[string, Record<string, unknown>, string][]} */
|
|
157
|
+
const policies = [
|
|
158
|
+
['privacy', privacy, 'privacy policy'],
|
|
159
|
+
['terms', terms, 'terms and conditions'],
|
|
160
|
+
];
|
|
161
|
+
for (const [key, p, what] of policies) {
|
|
162
|
+
if (empty(p.applies) || empty(p.published)) continue;
|
|
163
|
+
const c = /** @type {Record<string, unknown>} */ (isset(l, key) && isArray(l[key]) ? l[key] : {});
|
|
164
|
+
const pages = isset(c, 'pages') ? int(c.pages) : 0;
|
|
165
|
+
if (method === 'none' || pages === 0) {
|
|
166
|
+
out.push(item(key + '_linked', 'unchecked', sprintf("Couldn't check which pages link to the %s page, because no page could be read.", what)));
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const n = isset(c, 'linked') ? int(c.linked) : 0;
|
|
170
|
+
const missing = isset(c, 'missing') ? values(c.missing).slice(0, 5) : [];
|
|
171
|
+
const from = method === 'links' ? 'pages visitors loaded' : 'sampled pages';
|
|
172
|
+
if (n >= pages) {
|
|
173
|
+
out.push(item(key + '_linked', 'ok', sprintf('All %1$d %2$s link to the %3$s page.', pages, from, what)));
|
|
174
|
+
} else {
|
|
175
|
+
const share = n === 0 ? sprintf('None of the %1$d %2$s link', pages, from) : sprintf('%1$d of the %2$d %3$s link', n, pages, from);
|
|
176
|
+
out.push(
|
|
177
|
+
item(
|
|
178
|
+
key + '_linked',
|
|
179
|
+
'recommended',
|
|
180
|
+
sprintf('%1$s to the %2$s page. A plain footer link puts it on every page. Without it: %3$s.', share, what, missing.map(str).join(', ')),
|
|
181
|
+
),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** @type {Record<string, string>} */
|
|
189
|
+
const KINDS = {
|
|
190
|
+
hidden: 'has hidden text (%s)',
|
|
191
|
+
hidden_ai: 'has hidden text addressed to AI (%s)',
|
|
192
|
+
ai: 'has text addressed to AI (%s)',
|
|
193
|
+
comment: 'has an HTML comment addressed to AI (%s)',
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Hidden text findings, one line per item.
|
|
198
|
+
* @param {Record<string, unknown>} h {items, scanned, total}.
|
|
199
|
+
* @returns {Finding[]}
|
|
200
|
+
*/
|
|
201
|
+
function hidden(h) {
|
|
202
|
+
const items = isset(h, 'items') ? values(h.items) : [];
|
|
203
|
+
const scanned = isset(h, 'scanned') ? int(h.scanned) : 0;
|
|
204
|
+
const total = isset(h, 'total') ? int(h.total) : scanned;
|
|
205
|
+
if (!items.length) {
|
|
206
|
+
const of = total > scanned ? ' ' + sprintf('(the newest %1$d of %2$d)', scanned, total) : '';
|
|
207
|
+
return [item('hidden', 'ok', sprintf('No hidden text or text addressed to AI in the content of %1$d published items%2$s.', scanned, of))];
|
|
208
|
+
}
|
|
209
|
+
return items.map((raw) => {
|
|
210
|
+
const i = /** @type {Record<string, unknown>} */ (raw);
|
|
211
|
+
const kind = isset(raw, 'kind') && Object.hasOwn(KINDS, str(i.kind)) ? str(i.kind) : 'hidden';
|
|
212
|
+
const how = kind === 'hidden' || kind === 'hidden_ai' ? str(i.how) : str(i.phrase) !== '' ? '"' + str(i.phrase) + '"' : str(i.how);
|
|
213
|
+
return item(
|
|
214
|
+
'hidden',
|
|
215
|
+
'recommended',
|
|
216
|
+
sprintf(
|
|
217
|
+
'%1$s %2$s: "%3$s". Agents may read it as an instruction and search engines as spam; Goose Tools does not change it.',
|
|
218
|
+
isset(raw, 'title') ? str(i.title) : '',
|
|
219
|
+
sprintf(KINDS[kind], how),
|
|
220
|
+
isset(raw, 'text') ? str(i.text) : '',
|
|
221
|
+
),
|
|
222
|
+
);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @param {string} id
|
|
228
|
+
* @param {string} level
|
|
229
|
+
* @param {string} sentence
|
|
230
|
+
* @returns {Finding}
|
|
231
|
+
*/
|
|
232
|
+
function item(id, level, sentence) {
|
|
233
|
+
return { id, level, sentence };
|
|
234
|
+
}
|