Skip to content

Commit

Permalink
Use libxml2-wasm
Browse files Browse the repository at this point in the history
Trying to use this to resolve the vuln.

Closes: #2158
  • Loading branch information
hockeybuggy committed Oct 17, 2024
1 parent 68abbce commit 3f9439f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 255 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"eslint-plugin-react": "^7.37.1",
"jest": "^29.7.0",
"jest-puppeteer": "^10.1.2",
"libxmljs": "^1.0.11",
"libxml2-wasm": "^0.4.1",
"prettier": "^3.2.5",
"puppeteer": "^23.6.0",
"sass": "^1.71.1",
Expand Down
43 changes: 25 additions & 18 deletions prebuild_tests/sitemap.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { open } from "node:fs/promises";
import type { XMLDocument } from "libxmljs";
import { parseXmlAsync } from "libxmljs";
import fs from "node:fs";
import { XmlDocument } from "libxml2-wasm";

async function loadSitemap() {
const file = await open("./public/sitemap.xml");

const document = parseXmlAsync(await file.readFile());
const document = XmlDocument.fromString(
fs.readFileSync("./public/sitemap.xml").toString()
);

return document;
}

function extractUrlsFromSitemap(sitemap: XMLDocument) {
function extractUrlsFromSitemap(sitemap: XmlDocument) {
const urlElements = sitemap.find("*");
const urls = urlElements.map((ele) => {
// const name = ele.find("*")[0].childNodes()[0].text();
const children = ele.find("*");
const name = children[0].childNodes()[0].text();
const name = children[0].find("*")[0].content;
let lastmod = null;
if (children.length > 1) {
lastmod = children[1].childNodes()[0].text();
lastmod = children[1].find("*")[0].content;
}
return { name, lastmod };
});
Expand All @@ -29,16 +27,20 @@ function extractUrlsFromSitemap(sitemap: XMLDocument) {
describe("Sitemap", () => {
it("should have a version and encoding", async () => {
const subject = await loadSitemap();
expect(subject.version()).toEqual("1.0");
expect(subject.encoding()).toEqual("UTF-8");
expect(subject.get("version")).toEqual("1.0");
expect(subject.get("encoding")).toEqual("UTF-8");

subject.dispose();
});

it("should have a schema listed", async () => {
const subject = await loadSitemap();
const urlsetNode = subject.root()!;
expect(urlsetNode!.namespace()!.href()).toEqual(
"http://www.sitemaps.org/schemas/sitemap/0.9",
const urlsetNode = subject.root!;
expect(urlsetNode!.namespaceUri).toEqual(
"http://www.sitemaps.org/schemas/sitemap/0.9"
);

subject.dispose();
});

it("should have pages for the index", async () => {
Expand All @@ -48,6 +50,8 @@ describe("Sitemap", () => {
const urlNames = urls.map((url) => url.name);
// index page
expect(urlNames).toContain("https://hockeybuggy.com");

subject.dispose();
});

it("should have pages for the blog", async () => {
Expand All @@ -64,7 +68,7 @@ describe("Sitemap", () => {
expect(blogPosts.length).toBeGreaterThan(10);
// All the blog posts have a lastmod date
expect(
blogPosts.map((post) => post.lastmod).every((post) => post !== null),
blogPosts.map((post) => post.lastmod).every((post) => post !== null)
).toBe(true);

// tags pages
Expand All @@ -78,10 +82,12 @@ describe("Sitemap", () => {
expect(urlNames).toContain("https://hockeybuggy.com/blog/categories");
const blogCategoriesPattern = /\/blog\/categories/;
const blogCategoriesPages = urls.filter((url) =>
blogCategoriesPattern.test(url.name),
blogCategoriesPattern.test(url.name)
);
// There are category pages. 4 is an arbitary number.
expect(blogCategoriesPages.length).toBeGreaterThan(4);

subject.dispose();
});

it("should have pages for the project", async () => {
Expand All @@ -94,9 +100,10 @@ describe("Sitemap", () => {

const projectPagePattern = /\/project\/\w*/;
const projectPages = urls.filter((url) =>
projectPagePattern.test(url.name),
projectPagePattern.test(url.name)
);
// There are post pages. 5 is an arbitary number.
expect(projectPages.length).toBeGreaterThan(5);
subject.dispose();
});
});
Loading

0 comments on commit 3f9439f

Please sign in to comment.