feedkeeper 0.36.5 → 0.36.6
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/package.json +1 -1
- package/server/dist/api/v1/resources.js +1 -1
- package/server/dist/auth/avatar.js +2 -0
- package/server/dist/auth/security.js +10 -10
- package/server/dist/feeds/content/shared.js +4 -1
- package/server/dist/feeds/discovery.js +10 -1
- package/server/dist/feeds/extractor.js +14 -6
- package/server/dist/feeds/poller.js +15 -5
- package/server/dist/feeds/text.js +9 -1
- package/server/dist/mcp/http.js +8 -0
- package/server/dist/oauth/routes.js +9 -3
- package/server/dist/server.js +28 -0
- package/server/package.json +1 -1
package/package.json
CHANGED
|
@@ -673,7 +673,7 @@ resourcesRouter.get("/items/:id/image", imageLimiter, async (req, res) => {
|
|
|
673
673
|
res.status(503).json({ error: "image_busy" });
|
|
674
674
|
return;
|
|
675
675
|
}
|
|
676
|
-
console.error(
|
|
676
|
+
console.error("[image_proxy] Failed to fetch image for item %s (%s):", req.params.id, src, error instanceof Error ? error.message : error);
|
|
677
677
|
res.status(502).json({ error: "image_unavailable" });
|
|
678
678
|
}
|
|
679
679
|
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export const AVATAR_MAX_BYTES = 512 * 1024;
|
|
2
2
|
/** Identify an uploaded image by its signature instead of trusting the declared type. */
|
|
3
3
|
export function detectAvatarType(data) {
|
|
4
|
+
if (!data || (!Buffer.isBuffer(data) && !(data instanceof Uint8Array)))
|
|
5
|
+
return null;
|
|
4
6
|
if (data.length >= 3 && data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff)
|
|
5
7
|
return "image/jpeg";
|
|
6
8
|
if (data.length >= 8 && [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a].every((byte, index) => data[index] === byte))
|
|
@@ -3,7 +3,7 @@ import { TOTP, Secret } from "otpauth";
|
|
|
3
3
|
import { db } from "../db/index.js";
|
|
4
4
|
import { config } from "../config.js";
|
|
5
5
|
export const opaque = () => randomBytes(32).toString("base64url");
|
|
6
|
-
const
|
|
6
|
+
const tokenDigest = (text) => createHash("sha256").update(text).digest("hex");
|
|
7
7
|
const encryptionKey = () => createHash("sha256").update(`feedkeeper-totp:${config.sessionSecret}`).digest();
|
|
8
8
|
export function seal(text) {
|
|
9
9
|
const iv = randomBytes(12);
|
|
@@ -32,16 +32,16 @@ export function securityBinding(req, channel) {
|
|
|
32
32
|
if (channel === "native-login")
|
|
33
33
|
return channel;
|
|
34
34
|
if (req.headers.authorization)
|
|
35
|
-
return
|
|
35
|
+
return tokenDigest(`${channel}:${req.headers.authorization}`);
|
|
36
36
|
req.session ??= {};
|
|
37
37
|
req.session.securityBinding ??= opaque();
|
|
38
|
-
return
|
|
38
|
+
return tokenDigest(`${channel}:${req.session.securityBinding}`);
|
|
39
39
|
}
|
|
40
40
|
export function issueSecurityChallenge(purpose, userId, binding, payload, ttl = 5 * 60_000) {
|
|
41
41
|
db.prepare("DELETE FROM security_challenges WHERE expires_at <= ?").run(Date.now());
|
|
42
42
|
const id = opaque();
|
|
43
43
|
db.prepare("INSERT INTO security_challenges (id_hash, purpose, user_id, binding, version, payload, expires_at) VALUES (?, ?, ?, ?, ?, ?, ?)")
|
|
44
|
-
.run(
|
|
44
|
+
.run(tokenDigest(id), purpose, userId, binding, userId ? securityVersion(userId) : 0, JSON.stringify(payload), Date.now() + ttl);
|
|
45
45
|
return id;
|
|
46
46
|
}
|
|
47
47
|
/** Count attempts before checking the factor, including requests that end in an error. */
|
|
@@ -49,17 +49,17 @@ export function readSecurityChallenge(id, purpose, binding, consume = false) {
|
|
|
49
49
|
if (typeof id !== "string" || !/^[\w-]{43}$/.test(id))
|
|
50
50
|
return null;
|
|
51
51
|
return db.transaction(() => {
|
|
52
|
-
const row = db.prepare("SELECT * FROM security_challenges WHERE id_hash = ?").get(
|
|
52
|
+
const row = db.prepare("SELECT * FROM security_challenges WHERE id_hash = ?").get(tokenDigest(id));
|
|
53
53
|
if (!row || row.purpose !== purpose || row.binding !== binding || row.expires_at <= Date.now() || row.attempts >= 15 || (row.user_id && row.version !== securityVersion(row.user_id)))
|
|
54
54
|
return null;
|
|
55
55
|
if (consume)
|
|
56
|
-
db.prepare("DELETE FROM security_challenges WHERE id_hash = ?").run(
|
|
56
|
+
db.prepare("DELETE FROM security_challenges WHERE id_hash = ?").run(tokenDigest(id));
|
|
57
57
|
else
|
|
58
|
-
db.prepare("UPDATE security_challenges SET attempts = attempts + 1 WHERE id_hash = ?").run(
|
|
58
|
+
db.prepare("UPDATE security_challenges SET attempts = attempts + 1 WHERE id_hash = ?").run(tokenDigest(id));
|
|
59
59
|
return row;
|
|
60
60
|
})();
|
|
61
61
|
}
|
|
62
|
-
export function discardSecurityChallenge(id) { db.prepare("DELETE FROM security_challenges WHERE id_hash = ?").run(
|
|
62
|
+
export function discardSecurityChallenge(id) { db.prepare("DELETE FROM security_challenges WHERE id_hash = ?").run(tokenDigest(id)); }
|
|
63
63
|
export function totpFor(secret, label = "FeedKeeper") {
|
|
64
64
|
return new TOTP({ issuer: "FeedKeeper", label, algorithm: "SHA1", digits: 6, period: 30, secret: secret ? Secret.fromBase32(secret) : new Secret({ size: 20 }) });
|
|
65
65
|
}
|
|
@@ -86,14 +86,14 @@ export function verifySecondFactor(userId, code) {
|
|
|
86
86
|
const normalized = code.toUpperCase().replace(/[ -]/g, "");
|
|
87
87
|
if (!/^[A-F0-9]{24}$/.test(normalized))
|
|
88
88
|
return false;
|
|
89
|
-
return db.prepare("DELETE FROM recovery_codes WHERE user_id = ? AND code_hash = ?").run(userId,
|
|
89
|
+
return db.prepare("DELETE FROM recovery_codes WHERE user_id = ? AND code_hash = ?").run(userId, tokenDigest(normalized)).changes === 1;
|
|
90
90
|
})();
|
|
91
91
|
}
|
|
92
92
|
export function newRecoveryCodes(userId) {
|
|
93
93
|
db.prepare("DELETE FROM recovery_codes WHERE user_id = ?").run(userId);
|
|
94
94
|
const codes = Array.from({ length: 10 }, () => randomBytes(12).toString("hex").toUpperCase());
|
|
95
95
|
for (const code of codes)
|
|
96
|
-
db.prepare("INSERT INTO recovery_codes (user_id, code_hash) VALUES (?, ?)").run(userId,
|
|
96
|
+
db.prepare("INSERT INTO recovery_codes (user_id, code_hash) VALUES (?, ?)").run(userId, tokenDigest(code));
|
|
97
97
|
return codes.map(code => code.match(/.{6}/g).join("-"));
|
|
98
98
|
}
|
|
99
99
|
/**
|
|
@@ -16,7 +16,10 @@ export function pruneEmpty(node) {
|
|
|
16
16
|
export function linkParagraph(document, url, text) {
|
|
17
17
|
const paragraph = document.createElement("p");
|
|
18
18
|
const link = document.createElement("a");
|
|
19
|
-
|
|
19
|
+
const parsed = parseAddress(url);
|
|
20
|
+
if (parsed && (parsed.protocol === "https:" || parsed.protocol === "http:")) {
|
|
21
|
+
link.setAttribute("href", parsed.href);
|
|
22
|
+
}
|
|
20
23
|
link.textContent = text;
|
|
21
24
|
paragraph.append(link);
|
|
22
25
|
return paragraph;
|
|
@@ -174,7 +174,7 @@ export function extractFeedsFromHtml(html, baseUrl) {
|
|
|
174
174
|
for (const match of html.matchAll(/<a\b([^>]*href=["']([^"']+)["'][^>]*)>(.*?)<\/a>/gis)) {
|
|
175
175
|
const href = match[2];
|
|
176
176
|
if (href.endsWith("/feed") || href.endsWith(".rss") || href.endsWith(".xml") || href.includes("/rss/")) {
|
|
177
|
-
add(href, decodeEntities(match[3]
|
|
177
|
+
add(href, decodeEntities(stripHtmlTags(match[3]).trim()) || null, "application/rss+xml");
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
180
|
for (const match of decodeEntities(html).matchAll(FEED_URL_IN_TEXT)) {
|
|
@@ -189,4 +189,13 @@ function getAttr(attrsStr, attrName) {
|
|
|
189
189
|
return null;
|
|
190
190
|
return match[1] ?? match[2] ?? match[3] ?? null;
|
|
191
191
|
}
|
|
192
|
+
function stripHtmlTags(input) {
|
|
193
|
+
let text = input;
|
|
194
|
+
let previous;
|
|
195
|
+
do {
|
|
196
|
+
previous = text;
|
|
197
|
+
text = text.replace(/<[^>]+>/g, "");
|
|
198
|
+
} while (text !== previous);
|
|
199
|
+
return text;
|
|
200
|
+
}
|
|
192
201
|
//# sourceMappingURL=discovery.js.map
|
|
@@ -37,6 +37,14 @@ function cleanConsentDom(doc) {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
+
function isSafeSource(value) {
|
|
41
|
+
if (!value)
|
|
42
|
+
return false;
|
|
43
|
+
const trimmed = value.trim();
|
|
44
|
+
if (trimmed.length === 0 || trimmed.length > 4000 || /^javascript:/i.test(trimmed))
|
|
45
|
+
return false;
|
|
46
|
+
return /^https?:\/\//i.test(trimmed) || /^data:image\//i.test(trimmed) || trimmed.startsWith("/");
|
|
47
|
+
}
|
|
40
48
|
function resolveLazyImages(doc) {
|
|
41
49
|
const images = doc.querySelectorAll("img");
|
|
42
50
|
for (const img of images) {
|
|
@@ -45,20 +53,20 @@ function resolveLazyImages(doc) {
|
|
|
45
53
|
img.getAttribute("data-original") ||
|
|
46
54
|
img.getAttribute("data-actualsrc");
|
|
47
55
|
const src = img.getAttribute("src");
|
|
48
|
-
if (lazySrc && (!src || src.startsWith("data:") || src.length < 10 || SCRIPT_IMAGE_SOURCE.test(src))) {
|
|
49
|
-
img.setAttribute("src", lazySrc);
|
|
56
|
+
if (isSafeSource(lazySrc) && (!src || src.startsWith("data:") || src.length < 10 || SCRIPT_IMAGE_SOURCE.test(src))) {
|
|
57
|
+
img.setAttribute("src", lazySrc.trim());
|
|
50
58
|
}
|
|
51
59
|
const lazySrcset = img.getAttribute("data-lazy-srcset") || img.getAttribute("data-srcset");
|
|
52
60
|
const srcset = img.getAttribute("srcset");
|
|
53
|
-
if (lazySrcset && (!srcset || srcset.startsWith("data:"))) {
|
|
54
|
-
img.setAttribute("srcset", lazySrcset);
|
|
61
|
+
if (isSafeSource(lazySrcset) && (!srcset || srcset.startsWith("data:"))) {
|
|
62
|
+
img.setAttribute("srcset", lazySrcset.trim());
|
|
55
63
|
}
|
|
56
64
|
}
|
|
57
65
|
const sources = doc.querySelectorAll("source");
|
|
58
66
|
for (const source of sources) {
|
|
59
67
|
const lazySrcset = source.getAttribute("data-lazy-srcset") || source.getAttribute("data-srcset");
|
|
60
|
-
if (lazySrcset) {
|
|
61
|
-
source.setAttribute("srcset", lazySrcset);
|
|
68
|
+
if (isSafeSource(lazySrcset)) {
|
|
69
|
+
source.setAttribute("srcset", lazySrcset.trim());
|
|
62
70
|
}
|
|
63
71
|
}
|
|
64
72
|
}
|
|
@@ -58,17 +58,27 @@ function extractFeedIconUrls(parsed, baseUrl) {
|
|
|
58
58
|
return result;
|
|
59
59
|
}
|
|
60
60
|
function cleanImageUrl(url) {
|
|
61
|
-
return url.trim().replace(
|
|
61
|
+
return url.trim().replace(/&(?:#038|amp);/g, "&");
|
|
62
62
|
}
|
|
63
63
|
function isValidImageUrl(url) {
|
|
64
64
|
if (typeof url !== "string")
|
|
65
65
|
return false;
|
|
66
66
|
const trimmed = url.trim();
|
|
67
|
-
|
|
67
|
+
let parsed;
|
|
68
|
+
try {
|
|
69
|
+
parsed = new URL(trimmed);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:")
|
|
75
|
+
return false;
|
|
76
|
+
const host = parsed.hostname.toLowerCase();
|
|
77
|
+
if (host === "pixel.wp.com" || host.endsWith(".pixel.wp.com"))
|
|
78
|
+
return false;
|
|
79
|
+
const pathAndQuery = (parsed.pathname + parsed.search).toLowerCase();
|
|
80
|
+
if (pathAndQuery.includes("/1x1") || pathAndQuery.includes("pixel") || pathAndQuery.includes("tracking") || pathAndQuery.includes("beacon")) {
|
|
68
81
|
return false;
|
|
69
|
-
if (trimmed.includes("/1x1") || trimmed.includes("pixel.wp.com") || trimmed.includes("tracking") || trimmed.includes("beacon")) {
|
|
70
|
-
if (trimmed.includes("pixel") || trimmed.includes("1x1"))
|
|
71
|
-
return false;
|
|
72
82
|
}
|
|
73
83
|
return true;
|
|
74
84
|
}
|
|
@@ -22,7 +22,15 @@ function decodeOnce(text) {
|
|
|
22
22
|
const TITLE_MARKUP = /<\/?(?:b|i|u|em|strong|mark|span|small|sub|sup|font|a|br)\b[^>]*>/gi;
|
|
23
23
|
/** A title as one line of text: entities decoded, inline markup removed, line breaks and runs of spaces collapsed to one space, trimmed. */
|
|
24
24
|
export function plainTitle(text) {
|
|
25
|
-
|
|
25
|
+
let cleaned = decodeEntities(text);
|
|
26
|
+
if (!cleaned)
|
|
27
|
+
return undefined;
|
|
28
|
+
let previous;
|
|
29
|
+
do {
|
|
30
|
+
previous = cleaned;
|
|
31
|
+
cleaned = cleaned.replace(TITLE_MARKUP, tag => (/^<br/i.test(tag) ? " " : ""));
|
|
32
|
+
} while (cleaned !== previous);
|
|
33
|
+
cleaned = cleaned.replace(/\s+/g, " ").trim();
|
|
26
34
|
return cleaned || undefined;
|
|
27
35
|
}
|
|
28
36
|
export function decodeEntities(text) {
|
package/server/dist/mcp/http.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { Router } from "express";
|
|
2
|
+
import rateLimit from "express-rate-limit";
|
|
2
3
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
3
4
|
import { requireBearerToken } from "../auth/middleware.js";
|
|
4
5
|
import { requireCapability } from "../auth/capabilities.js";
|
|
5
6
|
import { createMcpServerForUser } from "./server.js";
|
|
7
|
+
const mcpLimiter = rateLimit({
|
|
8
|
+
windowMs: 60 * 1000,
|
|
9
|
+
limit: 240,
|
|
10
|
+
standardHeaders: true,
|
|
11
|
+
legacyHeaders: false,
|
|
12
|
+
});
|
|
6
13
|
export const mcpRouter = Router();
|
|
14
|
+
mcpRouter.use(mcpLimiter);
|
|
7
15
|
// Stateless mode: a fresh McpServer + transport per request, scoped to the
|
|
8
16
|
// user resolved from the bearer token. This is what makes the same endpoint
|
|
9
17
|
// safely usable by many different users/clients from anywhere.
|
|
@@ -21,7 +21,12 @@ function oauthError(res, status, error, description) {
|
|
|
21
21
|
res.status(status).json({ error, ...(description ? { error_description: description } : {}) });
|
|
22
22
|
}
|
|
23
23
|
function sameResource(value) {
|
|
24
|
-
|
|
24
|
+
if (typeof value !== "string")
|
|
25
|
+
return false;
|
|
26
|
+
let normalized = value;
|
|
27
|
+
while (normalized.endsWith("/"))
|
|
28
|
+
normalized = normalized.slice(0, -1);
|
|
29
|
+
return normalized === MCP_RESOURCE;
|
|
25
30
|
}
|
|
26
31
|
const LOOPBACK_HOSTS = new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
27
32
|
/** https anywhere (optionally limited to OAUTH_ALLOWED_REDIRECT_HOSTS) or plain http on this machine. */
|
|
@@ -190,6 +195,7 @@ oauthPublicRouter.post("/oauth/revoke", tokenLimiter, ...formBodies, (req, res)
|
|
|
190
195
|
export const oauthAuthorizeRouter = Router();
|
|
191
196
|
const publicOrigin = new URL(issuer).origin;
|
|
192
197
|
const loginLimiter = rateLimit({ windowMs: 15 * 60 * 1000, limit: 10, standardHeaders: true, legacyHeaders: false });
|
|
198
|
+
const authorizeLimiter = rateLimit({ windowMs: 15 * 60 * 1000, limit: 100, standardHeaders: true, legacyHeaders: false });
|
|
193
199
|
const formOnly = express.urlencoded({ extended: false, limit: "20kb" });
|
|
194
200
|
/**
|
|
195
201
|
* The consent and login forms only ever post from our own pages. Browsers state that themselves in
|
|
@@ -271,7 +277,7 @@ function mayUseMcp(req, res, userId) {
|
|
|
271
277
|
page(res, 403, renderError(req, "noAccess", getUserCapabilities(userId).manageUrl));
|
|
272
278
|
return false;
|
|
273
279
|
}
|
|
274
|
-
oauthAuthorizeRouter.get("/oauth/authorize", (req, res) => {
|
|
280
|
+
oauthAuthorizeRouter.get("/oauth/authorize", authorizeLimiter, (req, res) => {
|
|
275
281
|
const parsed = parseAuthorization(req.query);
|
|
276
282
|
if (parsed.kind === "fatal") {
|
|
277
283
|
page(res, 400, renderError(req, parsed.message));
|
|
@@ -332,7 +338,7 @@ oauthAuthorizeRouter.post("/oauth/authorize/login", sameOriginOnly, loginLimiter
|
|
|
332
338
|
req.session = { userId: user.id, securityVersion: securityVersion(user.id) };
|
|
333
339
|
res.redirect(303, returnTo);
|
|
334
340
|
});
|
|
335
|
-
oauthAuthorizeRouter.post("/oauth/authorize/decision", sameOriginOnly, formOnly, (req, res) => {
|
|
341
|
+
oauthAuthorizeRouter.post("/oauth/authorize/decision", authorizeLimiter, sameOriginOnly, formOnly, (req, res) => {
|
|
336
342
|
const body = (req.body ?? {});
|
|
337
343
|
const parsed = parseAuthorization(body);
|
|
338
344
|
if (parsed.kind === "fatal") {
|
package/server/dist/server.js
CHANGED
|
@@ -71,6 +71,34 @@ app.use(cookieSession({
|
|
|
71
71
|
sameSite: "lax",
|
|
72
72
|
secure: config.publicUrl.startsWith("https://"),
|
|
73
73
|
}));
|
|
74
|
+
// CSRF defense for cookie-authenticated mutating requests:
|
|
75
|
+
// Bearer-token requests (native apps, MCP, API clients) are immune to browser cross-site forgery.
|
|
76
|
+
app.use((req, res, next) => {
|
|
77
|
+
if (req.method === "GET" || req.method === "HEAD" || req.method === "OPTIONS")
|
|
78
|
+
return next();
|
|
79
|
+
if (req.headers.authorization)
|
|
80
|
+
return next();
|
|
81
|
+
const site = req.headers["sec-fetch-site"];
|
|
82
|
+
if (site === "cross-site") {
|
|
83
|
+
res.status(403).json({ error: "csrf_forbidden" });
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const origin = req.headers.origin;
|
|
87
|
+
if (origin) {
|
|
88
|
+
try {
|
|
89
|
+
const publicOrigin = new URL(config.publicUrl).origin;
|
|
90
|
+
if (new URL(origin).origin !== publicOrigin) {
|
|
91
|
+
res.status(403).json({ error: "csrf_forbidden" });
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
res.status(403).json({ error: "csrf_forbidden" });
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
next();
|
|
101
|
+
});
|
|
74
102
|
// Generic API rate limit as defense in depth on top of the stricter
|
|
75
103
|
// per-route limiter on /api/auth/login.
|
|
76
104
|
app.use("/api", rateLimit({
|