Skip to content

Commit 476f5ad

Browse files
authored
[codex] Secure Open Graph image endpoint (#1296)
* fix: secure open graph image endpoint Build generated Open Graph image URLs from the current page URL instead of raw title and description text. Validate same-origin document URLs in the Cloudflare image function and extract preview text from fetched HTML metadata. * refactor: simplify og metadata extraction Parse the fetched HTML response directly when extracting Open Graph preview text instead of using callback state from HTMLRewriter. * fix: fetch og page through assets binding Use the Cloudflare Pages ASSETS binding to read same-origin page HTML for OG image rendering instead of issuing a public same-origin fetch from the function.
1 parent 808116c commit 476f5ad

7 files changed

Lines changed: 312 additions & 122 deletions

File tree

apps/docs/__tests__/Meta_.test.res

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,43 @@ let getMetaContent = name => {
1111
}
1212
}
1313

14+
let getMetaPropertyContent = property => {
15+
switch document->WebAPI.Document.querySelector(`meta[property='${property}']`) {
16+
| Value(element) =>
17+
switch element->WebAPI.Element.getAttribute("content") {
18+
| Value(content) => content
19+
| Null => failwith(`expected ${property} meta tag to have content`)
20+
}
21+
| Null => failwith(`expected ${property} meta tag`)
22+
}
23+
}
24+
1425
test("renders DocSearch crawler meta tags", async () => {
15-
let _screen = await render(<Meta />)
26+
let _screen = await render(
27+
<ReactRouter.MemoryRouter initialEntries=["/docs/manual/introduction"]>
28+
<Meta />
29+
</ReactRouter.MemoryRouter>,
30+
)
1631

1732
expect(getMetaContent("docsearch:language"))->toBe("en")
1833
expect(getMetaContent("docsearch:version"))->toBe("v12,latest")
1934
})
35+
36+
test("generates Open Graph image URLs from the current page URL", async () => {
37+
let pagePath = "/docs/manual/introduction?preview=true"
38+
let rootUrl = Env.root_url->Stdlib.String.endsWith("/") ? Env.root_url : Env.root_url ++ "/"
39+
let pageUrl = rootUrl ++ "docs/manual/introduction?preview=true"
40+
41+
let _screen = await render(
42+
<ReactRouter.MemoryRouter initialEntries=[pagePath]>
43+
<Meta
44+
title=?{Some("Raw title should not be in the image URL")}
45+
description="Raw description should not be in the image URL"
46+
/>
47+
</ReactRouter.MemoryRouter>,
48+
)
49+
50+
expect(getMetaPropertyContent("og:image"))->toBe(
51+
`${rootUrl}ogimage/index.png?url=${encodeURIComponent(pageUrl)}`,
52+
)
53+
})

apps/docs/app/routes/BlogArticle.res

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ let make = (props: props) => {
127127
siteName="ReScript Blog"
128128
title={title ++ " | ReScript Blog"}
129129
description=?{description->Nullable.toOption}
130-
ogImage={Util.Url.makeOpenGraphImageUrl(title, description->Nullable.getOr(""))}
131130
/>
132131
<div className="mb-10 md:mb-20">
133132
<BlogHeader

0 commit comments

Comments
 (0)