@jant/core 1.2.0 → 1.3.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/bin/commands/site/import.js +2 -1
- package/dist/{app-CbOZ2Irh.js → app-C8dETvgL.js} +1429 -681
- package/dist/client/.vite/manifest.json +72 -83
- package/dist/client/_assets/chunks/{confirm-CMe01lne.js → confirm-BQebcuNB.js} +1 -1
- package/dist/client/_assets/chunks/{create-editor-hWapNE40.js → create-editor-B8KuszlS.js} +38 -38
- package/dist/client/_assets/chunks/footnote-rail-DUrqz0Py.js +2 -0
- package/dist/client/_assets/chunks/{jant-locale-picker-2L4rkDaz.js → jant-locale-picker-CLq0iKDH.js} +1 -1
- package/dist/client/_assets/chunks/lit-BN4mrI3j.js +2 -0
- package/dist/client/_assets/chunks/{settings-paths-BBVT8zKj.js → settings-paths-_fry4yWW.js} +1 -1
- package/dist/client/_assets/chunks/{slug-format-BuCPjSrB.js → slug-format-bcJ2ozr4.js} +1 -1
- package/dist/client/_assets/chunks/{sortable-list-DJXwkaOv.js → sortable-list-Bz2Na2E9.js} +2 -2
- package/dist/client/_assets/chunks/{unsafe-html-CWos3v6N.js → unsafe-html-OJ9m154w.js} +1 -1
- package/dist/client/_assets/chunks/unsafe-svg-DVJ4NPcb.js +1 -0
- package/dist/client/_assets/{client-D8Rrsd73.css → client-CoNHvipV.css} +1 -1
- package/dist/client/_assets/{client-auth-CsUGM718.js → client-auth-CbnZkLJ6.js} +99 -129
- package/dist/client/_assets/client-author-2WYBkgni.css +2 -0
- package/dist/client/_assets/{client-compose-BB4U7Wz0.js → client-compose-DYtDlYm-.js} +248 -244
- package/dist/client/_assets/{client-manage-CI9-eh-8.js → client-manage-BODyYP5Z.js} +157 -157
- package/dist/client/_assets/client-pq6sVS7y.js +354 -0
- package/dist/client/_assets/{client-settings-BnGPuqwc.js → client-settings-DDpe_tXJ.js} +166 -166
- package/dist/client/_assets/client-share-B9OsXx9p.js +149 -0
- package/dist/{constants-DEgyqWDl.js → constants-C2CdWAqt.js} +55 -5
- package/dist/index.js +1 -1
- package/dist/node.js +2 -2
- package/package.json +2 -1
- package/src/client/components/compose-types.ts +5 -1
- package/src/client/components/jant-compose-dialog.ts +25 -4
- package/src/client/components/jant-media-lightbox.ts +18 -3
- package/src/client/components/jant-nav-manager.ts +2 -0
- package/src/client/components/jant-post-menu.ts +42 -89
- package/src/client/components/nav-manager-types.ts +2 -0
- package/src/client/components/settings-types.ts +1 -1
- package/src/client/list-views.ts +382 -0
- package/src/client/media-lightbox-events.ts +12 -0
- package/src/client/runtime-paths.ts +12 -0
- package/src/client/share-capture.ts +63 -0
- package/src/client/share-launch.ts +36 -9
- package/src/client/tiptap/create-editor.ts +4 -0
- package/src/client/tiptap/embed-node.ts +3 -1
- package/src/client/tiptap/html-block-node.ts +3 -1
- package/src/client/tiptap/image-node.ts +3 -1
- package/src/client/view-confirm.ts +32 -4
- package/src/client.ts +1 -0
- package/src/db/migrations/0037_round_post.sql +16 -0
- package/src/db/migrations/0038_flashy_changeling.sql +15 -0
- package/src/db/migrations/0039_mixed_sister_grimm.sql +1 -0
- package/src/db/migrations/meta/0037_snapshot.json +2932 -0
- package/src/db/migrations/meta/0038_snapshot.json +2924 -0
- package/src/db/migrations/meta/0039_snapshot.json +2931 -0
- package/src/db/migrations/meta/_journal.json +21 -0
- package/src/db/migrations/pg/0035_flimsy_outlaw_kid.sql +3 -0
- package/src/db/migrations/pg/0036_milky_wolverine.sql +5 -0
- package/src/db/migrations/pg/0037_fuzzy_hammerhead.sql +1 -0
- package/src/db/migrations/pg/meta/0035_snapshot.json +3773 -0
- package/src/db/migrations/pg/meta/0036_snapshot.json +3766 -0
- package/src/db/migrations/pg/meta/0037_snapshot.json +3772 -0
- package/src/db/migrations/pg/meta/_journal.json +21 -0
- package/src/db/pg/schema.ts +1 -0
- package/src/db/post-visibility.ts +37 -6
- package/src/db/schema.ts +5 -2
- package/src/i18n/locales/glossary.zh-Hans.yml +4 -0
- package/src/i18n/locales/glossary.zh-Hant.yml +4 -0
- package/src/i18n/locales/public/en.po +21 -7
- package/src/i18n/locales/public/en.ts +1 -1
- package/src/i18n/locales/public/zh-Hans.po +20 -6
- package/src/i18n/locales/public/zh-Hans.ts +1 -1
- package/src/i18n/locales/public/zh-Hant.po +20 -6
- package/src/i18n/locales/public/zh-Hant.ts +1 -1
- package/src/i18n/locales/settings/en.po +10 -0
- package/src/i18n/locales/settings/en.ts +1 -1
- package/src/i18n/locales/settings/zh-Hans.po +10 -0
- package/src/i18n/locales/settings/zh-Hans.ts +1 -1
- package/src/i18n/locales/settings/zh-Hant.po +10 -0
- package/src/i18n/locales/settings/zh-Hant.ts +1 -1
- package/src/lib/api-public-posts.ts +10 -0
- package/src/lib/feed-policy.ts +2 -2
- package/src/lib/filter-dimensions.ts +11 -4
- package/src/lib/hugo-markdown.ts +7 -0
- package/src/lib/icons.ts +2 -0
- package/src/lib/link-preview.ts +499 -0
- package/src/lib/list-views.ts +117 -0
- package/src/lib/markdown-manager.ts +40 -1
- package/src/lib/navigation.ts +1 -1
- package/src/lib/share-card.ts +4 -3
- package/src/lib/thread-query.ts +6 -2
- package/src/lib/timeline.ts +12 -11
- package/src/lib/url-fetch.ts +291 -74
- package/src/lib/view-buffer.ts +2 -2
- package/src/lib/view-counting.ts +7 -6
- package/src/lib/view-token.ts +29 -7
- package/src/lib/view.ts +17 -0
- package/src/middleware/view-counter.ts +10 -5
- package/src/preset.css +5 -3
- package/src/routes/api/search.ts +1 -1
- package/src/routes/dash/settings.tsx +1 -1
- package/src/routes/feed/sitemap.ts +1 -1
- package/src/routes/pages/archive.tsx +25 -19
- package/src/routes/pages/collection.tsx +3 -3
- package/src/routes/pages/page.tsx +16 -4
- package/src/routes/pages/search.tsx +3 -3
- package/src/routes/view-confirm.ts +23 -15
- package/src/services/collection.ts +1 -1
- package/src/services/export-theme/assets/client-site.js +19 -19
- package/src/services/export-theme/layouts/archive/list.html +4 -2
- package/src/services/export-theme/layouts/partials/collection-members.html +4 -3
- package/src/services/export-theme/layouts/partials/featured-members.html +3 -3
- package/src/services/export-theme/layouts/partials/head.html +2 -0
- package/src/services/export-theme/layouts/partials/latest-members.html +4 -4
- package/src/services/export-theme/layouts/partials/post-card.html +1 -9
- package/src/services/export-theme/layouts/partials/reply.html +1 -9
- package/src/services/export-theme/layouts/partials/smart-collection-members.html +4 -3
- package/src/services/export-theme/layouts/post/list.html +1 -9
- package/src/services/export-theme/styles/main.css +2 -26
- package/src/services/export.ts +10 -3
- package/src/services/mcp.ts +5 -5
- package/src/services/navigation.ts +9 -4
- package/src/services/post.ts +102 -112
- package/src/services/search.ts +22 -7
- package/src/services/smart-collection.ts +3 -3
- package/src/services/stats.ts +12 -5
- package/src/services/thread.ts +6 -2
- package/src/styles/components-author.css +69 -7
- package/src/styles/tokens.css +28 -29
- package/src/styles/ui-author.css +140 -353
- package/src/styles/ui.css +34 -32
- package/src/types/constants.ts +68 -4
- package/src/types/entities.ts +1 -0
- package/src/types/views.ts +2 -0
- package/src/ui/compose/ComposeDialog.tsx +20 -1
- package/src/ui/dash/StatusBadge.tsx +11 -0
- package/src/ui/dash/appearance/FontThemeContent.tsx +1 -1
- package/src/ui/dash/appearance/NavigationContent.tsx +8 -0
- package/src/ui/dash/settings/StatsContent.tsx +29 -29
- package/src/ui/feed/LinkCard.tsx +19 -2
- package/src/ui/feed/PostStatusBadges.tsx +19 -3
- package/src/ui/feed/TimelineFeed.tsx +3 -0
- package/src/ui/font-themes.ts +32 -15
- package/src/ui/pages/ArchivePage.tsx +1 -0
- package/src/ui/shared/PostFooter.tsx +1 -22
- package/src/ui/shared/custom-icons.ts +5 -5
- package/dist/client/_assets/chunks/footnote-rail-9zwl6jtV.js +0 -2
- package/dist/client/_assets/chunks/lit-Bw_voPoW.js +0 -2
- package/dist/client/_assets/chunks/runtime-paths-y0_gHowM.js +0 -1
- package/dist/client/_assets/chunks/unsafe-svg-CUrgo4Xg.js +0 -1
- package/dist/client/_assets/client-BO8GOFfT.js +0 -354
- package/dist/client/_assets/client-author-QPm1whbO.css +0 -2
- package/dist/client/_assets/client-share-C59mOLJ1.js +0 -149
package/src/lib/navigation.ts
CHANGED
|
@@ -169,7 +169,7 @@ export async function getNavigationData(
|
|
|
169
169
|
collectionNavIds.length > 0
|
|
170
170
|
? await c.var.services.navItems.getCollectionFreshness(collectionNavIds, {
|
|
171
171
|
status: "published",
|
|
172
|
-
|
|
172
|
+
listedForReader: !isAuthenticated,
|
|
173
173
|
})
|
|
174
174
|
: undefined;
|
|
175
175
|
|
package/src/lib/share-card.ts
CHANGED
|
@@ -21,6 +21,7 @@ import type { Context } from "hono";
|
|
|
21
21
|
import { renderSVG } from "uqr";
|
|
22
22
|
import type { Bindings, Media, Post, PostView } from "../types.js";
|
|
23
23
|
import type { AppVariables } from "../types/app-context.js";
|
|
24
|
+
import { isOpenableByReader } from "../types/constants.js";
|
|
24
25
|
import { buildMediaMap } from "./media-helpers.js";
|
|
25
26
|
import {
|
|
26
27
|
ARTICLE_SUMMARY_MAX_BLOCKS,
|
|
@@ -208,18 +209,18 @@ export interface ShareCardSite {
|
|
|
208
209
|
* the address its QR code points at.
|
|
209
210
|
*
|
|
210
211
|
* @param post - Post record
|
|
211
|
-
* @returns True for published posts
|
|
212
|
+
* @returns True for published posts a reader can open: everything but Private
|
|
212
213
|
*
|
|
213
214
|
* @example
|
|
214
215
|
* ```ts
|
|
215
|
-
* canShareAsImage({ status: "published", visibility: "
|
|
216
|
+
* canShareAsImage({ status: "published", visibility: "unlisted" }); // true
|
|
216
217
|
* canShareAsImage({ status: "draft", visibility: "public" }); // false
|
|
217
218
|
* ```
|
|
218
219
|
*/
|
|
219
220
|
export function canShareAsImage(
|
|
220
221
|
post: Pick<Post, "status" | "visibility">,
|
|
221
222
|
): boolean {
|
|
222
|
-
return post.status === "published" && post.visibility
|
|
223
|
+
return post.status === "published" && isOpenableByReader(post.visibility);
|
|
223
224
|
}
|
|
224
225
|
|
|
225
226
|
/**
|
package/src/lib/thread-query.ts
CHANGED
|
@@ -38,7 +38,11 @@ export type ThreadSelectionParse =
|
|
|
38
38
|
| { kind: "empty" };
|
|
39
39
|
|
|
40
40
|
const READER_VISIBILITIES = ["public", "featured", "hidden", "any"] as const;
|
|
41
|
-
const AUTHOR_VISIBILITIES = [
|
|
41
|
+
const AUTHOR_VISIBILITIES = [
|
|
42
|
+
...READER_VISIBILITIES,
|
|
43
|
+
"unlisted",
|
|
44
|
+
"private",
|
|
45
|
+
] as const;
|
|
42
46
|
|
|
43
47
|
/** The `visibility` values an author's Thread list takes. */
|
|
44
48
|
export const THREAD_AUTHOR_VISIBILITIES = AUTHOR_VISIBILITIES;
|
|
@@ -88,7 +92,7 @@ export function queryReader(query: object): ParamReader {
|
|
|
88
92
|
* refused.
|
|
89
93
|
*
|
|
90
94
|
* @param read - Reads one parameter; `undefined` when absent
|
|
91
|
-
* @param options.audience - A reader may not name `private`
|
|
95
|
+
* @param options.audience - A reader may not name `unlisted` or `private`
|
|
92
96
|
* @param options.loadCollections - Loads the collection vocabulary, called only
|
|
93
97
|
* when a collection is named
|
|
94
98
|
* @returns The selection, or `empty` when a named collection doesn't exist
|
package/src/lib/timeline.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
TimelineItemView,
|
|
14
14
|
} from "../types.js";
|
|
15
15
|
import type { AppVariables } from "../types/app-context.js";
|
|
16
|
+
import { isListedForReader } from "../types/constants.js";
|
|
16
17
|
import { buildMediaMap } from "./media-helpers.js";
|
|
17
18
|
import { createMediaContext, resolveDraftTailId, toPostView } from "./view.js";
|
|
18
19
|
import { getViewLang } from "./view-language.js";
|
|
@@ -411,7 +412,7 @@ export async function assembleTimeline(
|
|
|
411
412
|
const page = Math.max(1, options?.page ?? 1);
|
|
412
413
|
const offset = (page - 1) * pageSize;
|
|
413
414
|
|
|
414
|
-
const
|
|
415
|
+
const listedForReader = !(options?.isAuthenticated ?? false);
|
|
415
416
|
|
|
416
417
|
// Count + list are independent — run in parallel
|
|
417
418
|
const lang = viewLangFilter(c);
|
|
@@ -421,14 +422,14 @@ export async function assembleTimeline(
|
|
|
421
422
|
status: "published",
|
|
422
423
|
excludeReplies: true,
|
|
423
424
|
excludeLatestHidden: true,
|
|
424
|
-
|
|
425
|
+
listedForReader,
|
|
425
426
|
lang,
|
|
426
427
|
}),
|
|
427
428
|
c.var.services.posts.list({
|
|
428
429
|
status: "published",
|
|
429
430
|
excludeReplies: true,
|
|
430
431
|
excludeLatestHidden: true,
|
|
431
|
-
|
|
432
|
+
listedForReader,
|
|
432
433
|
lang,
|
|
433
434
|
limit: pageSize,
|
|
434
435
|
offset,
|
|
@@ -461,7 +462,7 @@ export async function assembleTimelineItem(
|
|
|
461
462
|
threadRootId: string,
|
|
462
463
|
options?: { isAuthenticated?: boolean },
|
|
463
464
|
): Promise<TimelineItemView | null> {
|
|
464
|
-
const
|
|
465
|
+
const listedForReader = !(options?.isAuthenticated ?? false);
|
|
465
466
|
const post = await c.var.services.posts.getById(threadRootId);
|
|
466
467
|
|
|
467
468
|
if (
|
|
@@ -469,7 +470,7 @@ export async function assembleTimelineItem(
|
|
|
469
470
|
post.replyToId !== null ||
|
|
470
471
|
post.status !== "published" ||
|
|
471
472
|
post.visibility === "latest_hidden" ||
|
|
472
|
-
(
|
|
473
|
+
(listedForReader && !isListedForReader(post.visibility))
|
|
473
474
|
) {
|
|
474
475
|
return null;
|
|
475
476
|
}
|
|
@@ -492,19 +493,19 @@ export async function assembleFeaturedTimeline(
|
|
|
492
493
|
const pageSize = c.var.appConfig.pageSize;
|
|
493
494
|
const page = Math.max(1, options?.page ?? 1);
|
|
494
495
|
const offset = (page - 1) * pageSize;
|
|
495
|
-
const
|
|
496
|
+
const listedForReader = !(options?.isAuthenticated ?? false);
|
|
496
497
|
|
|
497
498
|
const lang = viewLangFilter(c);
|
|
498
499
|
|
|
499
500
|
const [totalCount, rootIds] = await Promise.all([
|
|
500
501
|
c.var.services.posts.countFeaturedThreadRoots({
|
|
501
502
|
status: "published",
|
|
502
|
-
|
|
503
|
+
listedForReader,
|
|
503
504
|
lang,
|
|
504
505
|
}),
|
|
505
506
|
c.var.services.posts.listFeaturedThreadRootIds({
|
|
506
507
|
status: "published",
|
|
507
|
-
|
|
508
|
+
listedForReader,
|
|
508
509
|
lang,
|
|
509
510
|
limit: pageSize,
|
|
510
511
|
offset,
|
|
@@ -562,7 +563,7 @@ export async function assembleCollectionTimeline(
|
|
|
562
563
|
const pageSize = c.var.appConfig.pageSize;
|
|
563
564
|
const page = Math.max(1, options.page ?? 1);
|
|
564
565
|
const offset = (page - 1) * pageSize;
|
|
565
|
-
const
|
|
566
|
+
const listedForReader = !(options.isAuthenticated ?? false);
|
|
566
567
|
const lang = viewLangFilter(c);
|
|
567
568
|
|
|
568
569
|
const [totalCount, rootIds] = await Promise.all([
|
|
@@ -570,7 +571,7 @@ export async function assembleCollectionTimeline(
|
|
|
570
571
|
options.collectionIds,
|
|
571
572
|
{
|
|
572
573
|
status: "published",
|
|
573
|
-
|
|
574
|
+
listedForReader,
|
|
574
575
|
lang,
|
|
575
576
|
},
|
|
576
577
|
),
|
|
@@ -578,7 +579,7 @@ export async function assembleCollectionTimeline(
|
|
|
578
579
|
options.collectionIds,
|
|
579
580
|
{
|
|
580
581
|
status: "published",
|
|
581
|
-
|
|
582
|
+
listedForReader,
|
|
582
583
|
lang,
|
|
583
584
|
sortOrder: options.sortOrder,
|
|
584
585
|
limit: pageSize,
|
package/src/lib/url-fetch.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Safe remote URL fetching
|
|
2
|
+
* Safe remote URL fetching on the server.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* attacker-influenced, so every fetch passes through an SSRF
|
|
9
|
-
* reader that caps size and time.
|
|
4
|
+
* Two callers fetch URLs an author supplied. Image sideloading rehosts the
|
|
5
|
+
* `<img>` sources of an article pasted from another site (a browser `fetch` of
|
|
6
|
+
* a third-party image is blocked by CORS for most hosts). Link previews read
|
|
7
|
+
* the `<head>` of the page a Link post points at, and the images it names.
|
|
8
|
+
* Either URL is attacker-influenced, so every fetch passes through an SSRF
|
|
9
|
+
* guard and a bounded reader that caps size and time.
|
|
10
10
|
*
|
|
11
11
|
* Two layers guard the address. {@link assertPublicHttpUrl} refuses a URL whose
|
|
12
12
|
* host is a private name or IP literal, in any of the forms an IPv6 address can
|
|
13
|
-
* carry an IPv4 one. On Node,
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
13
|
+
* carry an IPv4 one. On Node, every fetch here also resolves each host and
|
|
14
|
+
* refuses it when any address it resolves to is private, which catches names
|
|
15
|
+
* like `127.0.0.1.nip.io`. A Worker's fetch can't reach a private network at
|
|
16
|
+
* all, so it skips the lookup. A host that answers a public address to the
|
|
17
17
|
* check and a private one to the fetch a moment later (DNS rebinding) is out of
|
|
18
18
|
* reach of both; that needs network-level egress control.
|
|
19
19
|
*/
|
|
@@ -21,9 +21,53 @@
|
|
|
21
21
|
import { ValidationError } from "./errors.js";
|
|
22
22
|
|
|
23
23
|
/** A browser-like UA — many CDNs serving article images block unknown bots. */
|
|
24
|
-
const
|
|
24
|
+
const IMAGE_USER_AGENT =
|
|
25
25
|
"Mozilla/5.0 (compatible; Jant image sideloader) AppleWebKit/537.36";
|
|
26
26
|
|
|
27
|
+
/** The UA a page is read with for a Link preview. */
|
|
28
|
+
const PAGE_USER_AGENT =
|
|
29
|
+
"Mozilla/5.0 (compatible; Jant link preview; +https://jant.me) AppleWebKit/537.36";
|
|
30
|
+
|
|
31
|
+
/** What a fetch is for, which names it in the errors it throws. */
|
|
32
|
+
export type FetchTarget = "image" | "page";
|
|
33
|
+
|
|
34
|
+
const FETCH_ERRORS: Record<
|
|
35
|
+
FetchTarget,
|
|
36
|
+
{
|
|
37
|
+
invalid: string;
|
|
38
|
+
protocol: string;
|
|
39
|
+
credentials: string;
|
|
40
|
+
privateAddress: string;
|
|
41
|
+
unresolved: string;
|
|
42
|
+
timeout: string;
|
|
43
|
+
failed: string;
|
|
44
|
+
redirects: string;
|
|
45
|
+
}
|
|
46
|
+
> = {
|
|
47
|
+
image: {
|
|
48
|
+
invalid: "That doesn't look like a valid image URL.",
|
|
49
|
+
protocol: "Only http and https image URLs can be fetched.",
|
|
50
|
+
credentials: "Image URLs can't include credentials.",
|
|
51
|
+
privateAddress: "That image URL points to a private address.",
|
|
52
|
+
unresolved: "Couldn't find the image's host.",
|
|
53
|
+
timeout: "Timed out fetching the image.",
|
|
54
|
+
failed: "Couldn't fetch the image",
|
|
55
|
+
redirects: "Too many redirects fetching the image.",
|
|
56
|
+
},
|
|
57
|
+
page: {
|
|
58
|
+
invalid: "That doesn't look like a valid URL.",
|
|
59
|
+
protocol: "Only http and https pages can be fetched.",
|
|
60
|
+
credentials: "Page URLs can't include credentials.",
|
|
61
|
+
privateAddress: "That URL points to a private address.",
|
|
62
|
+
unresolved: "Couldn't find the page's host.",
|
|
63
|
+
timeout: "Timed out fetching the page.",
|
|
64
|
+
failed: "Couldn't fetch the page",
|
|
65
|
+
redirects: "Too many redirects fetching the page.",
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const IMAGE_TOO_LARGE = "That image is too large.";
|
|
70
|
+
|
|
27
71
|
export interface FetchedImage {
|
|
28
72
|
bytes: Uint8Array;
|
|
29
73
|
/** Lowercased content-type with parameters stripped, or null if absent. */
|
|
@@ -39,6 +83,22 @@ export interface FetchImageBytesOptions {
|
|
|
39
83
|
maxRedirects?: number;
|
|
40
84
|
}
|
|
41
85
|
|
|
86
|
+
export interface FetchedHtmlHead {
|
|
87
|
+
/** The document up to its `</head>` or `<body>`, or up to `maxBytes`. */
|
|
88
|
+
html: string;
|
|
89
|
+
/** The URL the document came from, after redirects. */
|
|
90
|
+
url: URL;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface FetchHtmlHeadOptions {
|
|
94
|
+
/** Stop reading (without failing) once this many bytes have arrived. */
|
|
95
|
+
maxBytes: number;
|
|
96
|
+
/** Abort the whole request after this many milliseconds. */
|
|
97
|
+
timeoutMs: number;
|
|
98
|
+
/** Maximum redirect hops to follow (each re-validated). Default 5. */
|
|
99
|
+
maxRedirects?: number;
|
|
100
|
+
}
|
|
101
|
+
|
|
42
102
|
/**
|
|
43
103
|
* Validate that a string is a public http(s) URL safe to fetch server-side.
|
|
44
104
|
*
|
|
@@ -47,25 +107,30 @@ export interface FetchImageBytesOptions {
|
|
|
47
107
|
* literals (including the `169.254.169.254` cloud-metadata address).
|
|
48
108
|
*
|
|
49
109
|
* @param raw - The candidate URL string
|
|
110
|
+
* @param target - What the URL is fetched for, which names it in the error
|
|
50
111
|
* @returns The parsed {@link URL}
|
|
51
112
|
* @example
|
|
52
113
|
* ```ts
|
|
53
114
|
* const url = assertPublicHttpUrl("https://example.com/photo.jpg");
|
|
54
115
|
* ```
|
|
55
116
|
*/
|
|
56
|
-
export function assertPublicHttpUrl(
|
|
117
|
+
export function assertPublicHttpUrl(
|
|
118
|
+
raw: string,
|
|
119
|
+
target: FetchTarget = "image",
|
|
120
|
+
): URL {
|
|
121
|
+
const errors = FETCH_ERRORS[target];
|
|
57
122
|
let url: URL;
|
|
58
123
|
try {
|
|
59
124
|
url = new URL(raw);
|
|
60
125
|
} catch {
|
|
61
|
-
throw new ValidationError(
|
|
126
|
+
throw new ValidationError(errors.invalid);
|
|
62
127
|
}
|
|
63
128
|
|
|
64
129
|
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
65
|
-
throw new ValidationError(
|
|
130
|
+
throw new ValidationError(errors.protocol);
|
|
66
131
|
}
|
|
67
132
|
if (url.username || url.password) {
|
|
68
|
-
throw new ValidationError(
|
|
133
|
+
throw new ValidationError(errors.credentials);
|
|
69
134
|
}
|
|
70
135
|
|
|
71
136
|
// A trailing dot names the same host (`localhost.` is `localhost`).
|
|
@@ -75,7 +140,7 @@ export function assertPublicHttpUrl(raw: string): URL {
|
|
|
75
140
|
host.endsWith(".localhost") ||
|
|
76
141
|
isPrivateAddress(host)
|
|
77
142
|
) {
|
|
78
|
-
throw new ValidationError(
|
|
143
|
+
throw new ValidationError(errors.privateAddress);
|
|
79
144
|
}
|
|
80
145
|
|
|
81
146
|
return url;
|
|
@@ -87,6 +152,7 @@ export function assertPublicHttpUrl(raw: string): URL {
|
|
|
87
152
|
* Skipped on Cloudflare Workers, whose fetch can't reach a private network.
|
|
88
153
|
*
|
|
89
154
|
* @param url - A URL already accepted by {@link assertPublicHttpUrl}
|
|
155
|
+
* @param target - What the URL is fetched for, which names it in the error
|
|
90
156
|
* @throws {ValidationError} When the host resolves to a private address or
|
|
91
157
|
* doesn't resolve
|
|
92
158
|
* @example
|
|
@@ -94,23 +160,40 @@ export function assertPublicHttpUrl(raw: string): URL {
|
|
|
94
160
|
* await assertResolvesPublic(new URL("https://127.0.0.1.nip.io/a.png")); // throws
|
|
95
161
|
* ```
|
|
96
162
|
*/
|
|
97
|
-
export async function assertResolvesPublic(
|
|
163
|
+
export async function assertResolvesPublic(
|
|
164
|
+
url: URL,
|
|
165
|
+
target: FetchTarget = "image",
|
|
166
|
+
): Promise<void> {
|
|
98
167
|
if (isCloudflareWorker()) return;
|
|
99
168
|
const host = url.hostname.replace(/^\[|\]$/g, "");
|
|
100
169
|
if (isIpLiteral(host)) return; // Already checked by assertPublicHttpUrl.
|
|
101
170
|
|
|
102
|
-
const
|
|
171
|
+
const errors = FETCH_ERRORS[target];
|
|
172
|
+
const lookup = await loadDnsLookup();
|
|
103
173
|
let addresses: { address: string }[];
|
|
104
174
|
try {
|
|
105
175
|
addresses = await lookup(host, { all: true, verbatim: true });
|
|
106
176
|
} catch {
|
|
107
|
-
throw new ValidationError(
|
|
177
|
+
throw new ValidationError(errors.unresolved);
|
|
108
178
|
}
|
|
109
179
|
if (addresses.some(({ address }) => isPrivateAddress(address))) {
|
|
110
|
-
throw new ValidationError(
|
|
180
|
+
throw new ValidationError(errors.privateAddress);
|
|
111
181
|
}
|
|
112
182
|
}
|
|
113
183
|
|
|
184
|
+
type DnsLookup = (typeof import("node:dns/promises"))["lookup"];
|
|
185
|
+
let dnsLookup: Promise<DnsLookup> | undefined;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* `node:dns/promises`, imported once. A preview resolves its thumbnail and
|
|
189
|
+
* its icon side by side, so two lookups often start together; they share one
|
|
190
|
+
* import rather than each starting their own.
|
|
191
|
+
*/
|
|
192
|
+
function loadDnsLookup(): Promise<DnsLookup> {
|
|
193
|
+
dnsLookup ??= import("node:dns/promises").then(({ lookup }) => lookup);
|
|
194
|
+
return dnsLookup;
|
|
195
|
+
}
|
|
196
|
+
|
|
114
197
|
function isCloudflareWorker(): boolean {
|
|
115
198
|
return (
|
|
116
199
|
typeof navigator !== "undefined" &&
|
|
@@ -122,6 +205,67 @@ function isIpLiteral(host: string): boolean {
|
|
|
122
205
|
return /^\d{1,3}(\.\d{1,3}){3}$/.test(host) || host.includes(":");
|
|
123
206
|
}
|
|
124
207
|
|
|
208
|
+
/**
|
|
209
|
+
* GET a URL, following redirects by hand so that every hop passes the SSRF
|
|
210
|
+
* guard, and return the first response that isn't a redirect.
|
|
211
|
+
*/
|
|
212
|
+
async function fetchFollowingPublicRedirects(
|
|
213
|
+
startUrl: URL,
|
|
214
|
+
input: {
|
|
215
|
+
target: FetchTarget;
|
|
216
|
+
headers: (url: URL) => Record<string, string>;
|
|
217
|
+
signal: AbortController["signal"];
|
|
218
|
+
maxRedirects: number;
|
|
219
|
+
},
|
|
220
|
+
): Promise<{ response: Response; url: URL }> {
|
|
221
|
+
const errors = FETCH_ERRORS[input.target];
|
|
222
|
+
let url = startUrl;
|
|
223
|
+
let response: Response | null = null;
|
|
224
|
+
|
|
225
|
+
for (let hop = 0; hop <= input.maxRedirects; hop++) {
|
|
226
|
+
await assertResolvesPublic(url, input.target);
|
|
227
|
+
response = await fetch(url.toString(), {
|
|
228
|
+
method: "GET",
|
|
229
|
+
redirect: "manual",
|
|
230
|
+
signal: input.signal,
|
|
231
|
+
headers: input.headers(url),
|
|
232
|
+
}).catch((error) => {
|
|
233
|
+
if (input.signal.aborted) {
|
|
234
|
+
throw new ValidationError(errors.timeout);
|
|
235
|
+
}
|
|
236
|
+
throw new ValidationError(
|
|
237
|
+
error instanceof Error
|
|
238
|
+
? `${errors.failed}: ${error.message}`
|
|
239
|
+
: `${errors.failed}.`,
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
if (response.status >= 300 && response.status < 400) {
|
|
244
|
+
const location = response.headers.get("location");
|
|
245
|
+
if (!location) break; // No target — fall through to the (failing) checks.
|
|
246
|
+
if (hop === input.maxRedirects) {
|
|
247
|
+
throw new ValidationError(errors.redirects);
|
|
248
|
+
}
|
|
249
|
+
// Re-validate every hop so a redirect can't escape the SSRF guard.
|
|
250
|
+
url = assertPublicHttpUrl(
|
|
251
|
+
new URL(location, url).toString(),
|
|
252
|
+
input.target,
|
|
253
|
+
);
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (!response) {
|
|
260
|
+
throw new ValidationError(`${errors.failed}.`);
|
|
261
|
+
}
|
|
262
|
+
if (!response.ok) {
|
|
263
|
+
await response.body?.cancel().catch(() => {});
|
|
264
|
+
throw new ValidationError(`${errors.failed} (HTTP ${response.status}).`);
|
|
265
|
+
}
|
|
266
|
+
return { response, url };
|
|
267
|
+
}
|
|
268
|
+
|
|
125
269
|
/**
|
|
126
270
|
* Fetch a remote image with an SSRF-checked redirect chain, a size cap, and a
|
|
127
271
|
* timeout. Reads the body in chunks and aborts the moment it exceeds `maxBytes`
|
|
@@ -142,64 +286,29 @@ export async function fetchImageBytes(
|
|
|
142
286
|
startUrl: URL,
|
|
143
287
|
options: FetchImageBytesOptions,
|
|
144
288
|
): Promise<FetchedImage> {
|
|
145
|
-
const maxRedirects = options.maxRedirects ?? 3;
|
|
146
289
|
const controller = new AbortController();
|
|
147
290
|
const timer = setTimeout(() => controller.abort(), options.timeoutMs);
|
|
148
291
|
|
|
149
292
|
try {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
// common "referer must be same-site" hotlink check.
|
|
165
|
-
Referer: `${url.origin}/`,
|
|
166
|
-
},
|
|
167
|
-
}).catch((error) => {
|
|
168
|
-
if (controller.signal.aborted) {
|
|
169
|
-
throw new ValidationError("Timed out fetching the image.");
|
|
170
|
-
}
|
|
171
|
-
throw new ValidationError(
|
|
172
|
-
error instanceof Error
|
|
173
|
-
? `Couldn't fetch the image: ${error.message}`
|
|
174
|
-
: "Couldn't fetch the image.",
|
|
175
|
-
);
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
if (response.status >= 300 && response.status < 400) {
|
|
179
|
-
const location = response.headers.get("location");
|
|
180
|
-
if (!location) break; // No target — fall through to the (failing) checks.
|
|
181
|
-
if (hop === maxRedirects) {
|
|
182
|
-
throw new ValidationError("Too many redirects fetching the image.");
|
|
183
|
-
}
|
|
184
|
-
// Re-validate every hop so a redirect can't escape the SSRF guard.
|
|
185
|
-
url = assertPublicHttpUrl(new URL(location, url).toString());
|
|
186
|
-
continue;
|
|
187
|
-
}
|
|
188
|
-
break;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (!response) {
|
|
192
|
-
throw new ValidationError("Couldn't fetch the image.");
|
|
193
|
-
}
|
|
194
|
-
if (!response.ok) {
|
|
195
|
-
throw new ValidationError(
|
|
196
|
-
`Couldn't fetch the image (HTTP ${response.status}).`,
|
|
197
|
-
);
|
|
198
|
-
}
|
|
293
|
+
const { response } = await fetchFollowingPublicRedirects(startUrl, {
|
|
294
|
+
target: "image",
|
|
295
|
+
signal: controller.signal,
|
|
296
|
+
maxRedirects: options.maxRedirects ?? 3,
|
|
297
|
+
headers: (url) => ({
|
|
298
|
+
Accept: "image/*,*/*;q=0.8",
|
|
299
|
+
"User-Agent": IMAGE_USER_AGENT,
|
|
300
|
+
// Many CDNs (Douban, WeChat, Bilibili, etc.) reject hotlinked image
|
|
301
|
+
// requests that lack a Referer or carry a foreign one. Sending the
|
|
302
|
+
// image's own origin satisfies the common "referer must be same-site"
|
|
303
|
+
// hotlink check.
|
|
304
|
+
Referer: `${url.origin}/`,
|
|
305
|
+
}),
|
|
306
|
+
});
|
|
199
307
|
|
|
200
308
|
const declared = Number(response.headers.get("content-length"));
|
|
201
309
|
if (Number.isFinite(declared) && declared > options.maxBytes) {
|
|
202
|
-
|
|
310
|
+
await response.body?.cancel().catch(() => {});
|
|
311
|
+
throw new ValidationError(IMAGE_TOO_LARGE);
|
|
203
312
|
}
|
|
204
313
|
|
|
205
314
|
const contentType = normalizeContentType(
|
|
@@ -212,6 +321,114 @@ export async function fetchImageBytes(
|
|
|
212
321
|
}
|
|
213
322
|
}
|
|
214
323
|
|
|
324
|
+
/**
|
|
325
|
+
* Fetch the `<head>` of an HTML page with an SSRF-checked redirect chain and a
|
|
326
|
+
* timeout. Reading stops at `</head>` or `<body>`, or once `maxBytes` have
|
|
327
|
+
* arrived — a truncated head is returned, not refused, because the metadata a
|
|
328
|
+
* preview needs sits near the top. Only HTML is read: a response that declares
|
|
329
|
+
* another content type is refused before its body is touched.
|
|
330
|
+
*
|
|
331
|
+
* The bytes are decoded as UTF-8 whatever the page's charset: callers read
|
|
332
|
+
* attribute values that are URLs and keywords, which are ASCII in any
|
|
333
|
+
* charset a page is likely to use.
|
|
334
|
+
*
|
|
335
|
+
* @param startUrl - A URL already validated by {@link assertPublicHttpUrl}
|
|
336
|
+
* @param options - Read cap, timeout, and redirect budget
|
|
337
|
+
* @returns The head's HTML and the URL it came from after redirects
|
|
338
|
+
* @example
|
|
339
|
+
* ```ts
|
|
340
|
+
* const { html, url } = await fetchHtmlHead(
|
|
341
|
+
* assertPublicHttpUrl("https://example.com/post", "page"),
|
|
342
|
+
* { maxBytes: 512 * 1024, timeoutMs: 3000 },
|
|
343
|
+
* );
|
|
344
|
+
* ```
|
|
345
|
+
*/
|
|
346
|
+
export async function fetchHtmlHead(
|
|
347
|
+
startUrl: URL,
|
|
348
|
+
options: FetchHtmlHeadOptions,
|
|
349
|
+
): Promise<FetchedHtmlHead> {
|
|
350
|
+
const controller = new AbortController();
|
|
351
|
+
const timer = setTimeout(() => controller.abort(), options.timeoutMs);
|
|
352
|
+
|
|
353
|
+
try {
|
|
354
|
+
const { response, url } = await fetchFollowingPublicRedirects(startUrl, {
|
|
355
|
+
target: "page",
|
|
356
|
+
signal: controller.signal,
|
|
357
|
+
maxRedirects: options.maxRedirects ?? 5,
|
|
358
|
+
headers: () => ({
|
|
359
|
+
Accept: "text/html,application/xhtml+xml;q=0.9,*/*;q=0.1",
|
|
360
|
+
"User-Agent": PAGE_USER_AGENT,
|
|
361
|
+
}),
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
const contentType = normalizeContentType(
|
|
365
|
+
response.headers.get("content-type"),
|
|
366
|
+
);
|
|
367
|
+
if (
|
|
368
|
+
contentType &&
|
|
369
|
+
contentType !== "text/html" &&
|
|
370
|
+
contentType !== "application/xhtml+xml"
|
|
371
|
+
) {
|
|
372
|
+
await response.body?.cancel().catch(() => {});
|
|
373
|
+
throw new ValidationError(`${FETCH_ERRORS.page.failed}: not HTML.`);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const html = await readHead(response, options.maxBytes).catch((error) => {
|
|
377
|
+
if (controller.signal.aborted) {
|
|
378
|
+
throw new ValidationError(FETCH_ERRORS.page.timeout);
|
|
379
|
+
}
|
|
380
|
+
throw error;
|
|
381
|
+
});
|
|
382
|
+
return { html, url };
|
|
383
|
+
} finally {
|
|
384
|
+
clearTimeout(timer);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** Where a document's head ends: its closing tag, or the body's opening one. */
|
|
389
|
+
const HEAD_END = /<\/head[\s>]|<body[\s>]/i;
|
|
390
|
+
|
|
391
|
+
/** Longest tail of one chunk that could start a {@link HEAD_END} match. */
|
|
392
|
+
const HEAD_END_OVERLAP = 6;
|
|
393
|
+
|
|
394
|
+
async function readHead(response: Response, maxBytes: number): Promise<string> {
|
|
395
|
+
const decoder = new TextDecoder("utf-8");
|
|
396
|
+
const body = response.body;
|
|
397
|
+
if (!body) {
|
|
398
|
+
const text = decoder.decode(
|
|
399
|
+
new Uint8Array(await response.arrayBuffer()).subarray(0, maxBytes),
|
|
400
|
+
);
|
|
401
|
+
return cutAtHeadEnd(text);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const reader = body.getReader();
|
|
405
|
+
let text = "";
|
|
406
|
+
let total = 0;
|
|
407
|
+
try {
|
|
408
|
+
for (;;) {
|
|
409
|
+
const { done, value } = await reader.read();
|
|
410
|
+
if (done) break;
|
|
411
|
+
if (!value) continue;
|
|
412
|
+
const room = maxBytes - total;
|
|
413
|
+
const chunk = value.byteLength > room ? value.subarray(0, room) : value;
|
|
414
|
+
total += chunk.byteLength;
|
|
415
|
+
const searchFrom = Math.max(0, text.length - HEAD_END_OVERLAP);
|
|
416
|
+
text += decoder.decode(chunk, { stream: true });
|
|
417
|
+
const end = text.slice(searchFrom).search(HEAD_END);
|
|
418
|
+
if (end !== -1) return text.slice(0, searchFrom + end);
|
|
419
|
+
if (total >= maxBytes) break;
|
|
420
|
+
}
|
|
421
|
+
} finally {
|
|
422
|
+
await reader.cancel().catch(() => {});
|
|
423
|
+
}
|
|
424
|
+
return text + decoder.decode();
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function cutAtHeadEnd(text: string): string {
|
|
428
|
+
const end = text.search(HEAD_END);
|
|
429
|
+
return end === -1 ? text : text.slice(0, end);
|
|
430
|
+
}
|
|
431
|
+
|
|
215
432
|
async function readBounded(
|
|
216
433
|
response: Response,
|
|
217
434
|
maxBytes: number,
|
|
@@ -220,7 +437,7 @@ async function readBounded(
|
|
|
220
437
|
if (!body) {
|
|
221
438
|
const buffer = new Uint8Array(await response.arrayBuffer());
|
|
222
439
|
if (buffer.byteLength > maxBytes) {
|
|
223
|
-
throw new ValidationError(
|
|
440
|
+
throw new ValidationError(IMAGE_TOO_LARGE);
|
|
224
441
|
}
|
|
225
442
|
return buffer;
|
|
226
443
|
}
|
|
@@ -235,7 +452,7 @@ async function readBounded(
|
|
|
235
452
|
total += value.byteLength;
|
|
236
453
|
if (total > maxBytes) {
|
|
237
454
|
await reader.cancel().catch(() => {});
|
|
238
|
-
throw new ValidationError(
|
|
455
|
+
throw new ValidationError(IMAGE_TOO_LARGE);
|
|
239
456
|
}
|
|
240
457
|
chunks.push(value);
|
|
241
458
|
}
|
package/src/lib/view-buffer.ts
CHANGED
|
@@ -24,14 +24,14 @@
|
|
|
24
24
|
/** How long a hit waits for company before the buffer is written out. */
|
|
25
25
|
export const VIEW_FLUSH_DELAY_MS = 5_000;
|
|
26
26
|
|
|
27
|
-
/** One reader opening one page, as the counter files it. */
|
|
27
|
+
/** One reader opening one page, or reading one Thread in a list, as the counter files it. */
|
|
28
28
|
export interface ViewHit {
|
|
29
29
|
siteId: string;
|
|
30
30
|
/** The site's calendar day, `YYYY-MM-DD`. */
|
|
31
31
|
day: string;
|
|
32
32
|
/** The site's hour, `YYYY-MM-DDTHH`, for today's chart. */
|
|
33
33
|
hour: string;
|
|
34
|
-
/** A
|
|
34
|
+
/** A Thread's root id, or the path of any other page. */
|
|
35
35
|
page: string;
|
|
36
36
|
/** The referring host, or `""`. */
|
|
37
37
|
referrer: string;
|