mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-30 22:44:41 +01:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
28fcb64720 | ||
|
|
8a62b70b1d | ||
|
|
22c18d1981 | ||
|
|
e5332d4491 | ||
|
|
9f9547eb2d | ||
|
|
b2da325a32 |
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@jackyzha0/quartz": patch
|
||||
---
|
||||
|
||||
When a build stops on an error, Quartz stops esbuild before exiting. esbuild runs as a child process that writes to the same terminal; left running, it could print "fatal error: all goroutines are asleep - deadlock!" and a long goroutine dump after Quartz's error message.
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@jackyzha0/quartz": patch
|
||||
---
|
||||
|
||||
Security: frontmatter can no longer run code during a build. A note starting with `---js` or `---javascript` had its frontmatter evaluated as JavaScript (gray-matter's default engine), so building content you didn't write, such as a pull request preview, could run arbitrary code. Quartz now refuses JavaScript frontmatter with an error naming the file. Invalid frontmatter (for example broken YAML) is now reported as a problem in that file instead of as a Quartz bug.
|
||||
Generated
+4
-4
@@ -37,7 +37,7 @@
|
||||
"@quartz-community/graph": "^1.0.0",
|
||||
"@quartz-community/hard-line-breaks": "^1.0.0",
|
||||
"@quartz-community/latex": "^1.0.0",
|
||||
"@quartz-community/note-properties": "^1.0.0",
|
||||
"@quartz-community/note-properties": "^1.1.1",
|
||||
"@quartz-community/obsidian-flavored-markdown": "^2.0.0",
|
||||
"@quartz-community/og-image": "^1.0.0",
|
||||
"@quartz-community/ox-hugo": "^1.0.0",
|
||||
@@ -2892,9 +2892,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@quartz-community/note-properties": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@quartz-community/note-properties/-/note-properties-1.1.0.tgz",
|
||||
"integrity": "sha512-TYpEDTO9Im3zZBqKpsQHFbSABj/FPswPSBBhle9FSiNUQpIOSZk6lj6Yw3LHz2mpa01w3Cvag5MGtugYe+HYEQ==",
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/@quartz-community/note-properties/-/note-properties-1.1.1.tgz",
|
||||
"integrity": "sha512-kURVT2PTQZTcRZxiT7o/gerI/8n0EhVogLaMznviJXYKSSvenE+8uKlPxJtZr1nbTJOn73UNBr5iNMhQIycQtg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@quartz-community/types": "^1.0.0",
|
||||
|
||||
+1
-1
@@ -67,7 +67,7 @@
|
||||
"@quartz-community/graph": "^1.0.0",
|
||||
"@quartz-community/hard-line-breaks": "^1.0.0",
|
||||
"@quartz-community/latex": "^1.0.0",
|
||||
"@quartz-community/note-properties": "^1.0.0",
|
||||
"@quartz-community/note-properties": "^1.1.1",
|
||||
"@quartz-community/obsidian-flavored-markdown": "^2.0.0",
|
||||
"@quartz-community/og-image": "^1.0.0",
|
||||
"@quartz-community/ox-hugo": "^1.0.0",
|
||||
|
||||
@@ -428,6 +428,8 @@ export async function handleBuild(argv) {
|
||||
`${styleText("red", "Failed to build Quartz.")} Check for syntax errors in your configuration or plugins.`,
|
||||
)
|
||||
console.log(`Reason: ${styleText("gray", err.message ?? String(err))}`)
|
||||
// stop esbuild's child process first: it shares our stderr and outlives us otherwise
|
||||
void esbuild.stop()
|
||||
process.exit(1)
|
||||
})
|
||||
release()
|
||||
|
||||
@@ -7,8 +7,14 @@ import { VFile } from "vfile"
|
||||
import { Element, Root } from "hast"
|
||||
import { visit } from "unist-util-visit"
|
||||
import { BUILTINS, instantiateBuiltins, officialPluginName, planBuiltins, PlanInput } from "."
|
||||
import { Frontmatter, normalizeFrontmatter, extractLinksFromValue } from "./frontmatter"
|
||||
import {
|
||||
Frontmatter,
|
||||
normalizeFrontmatter,
|
||||
extractLinksFromValue,
|
||||
readFrontmatterData,
|
||||
} from "./frontmatter"
|
||||
import { Links } from "./links"
|
||||
import { isContentError } from "../../util/pluginErrors"
|
||||
import { Drafts, isDraft } from "./drafts"
|
||||
import {
|
||||
ContentIndex,
|
||||
@@ -210,6 +216,32 @@ describe("frontmatter built-in", () => {
|
||||
)
|
||||
})
|
||||
|
||||
test("refuses JavaScript frontmatter instead of executing it", async () => {
|
||||
const g = globalThis as { __quartzFrontmatterRan?: boolean }
|
||||
delete g.__quartzFrontmatterRan
|
||||
for (const lang of ["js", "javascript"]) {
|
||||
const source = `---${lang}\n{ title: "x", ran: (globalThis.__quartzFrontmatterRan = true) }\n---\nbody\n`
|
||||
assert.throws(() => readFrontmatterData(source), /JavaScript frontmatter/)
|
||||
await assert.rejects(parse(source), /JavaScript frontmatter/)
|
||||
// also when a site sets `language: js` (the type allows only yaml/toml, a YAML config does not)
|
||||
assert.throws(
|
||||
() =>
|
||||
readFrontmatterData(`---\n{ ran: (globalThis.__quartzFrontmatterRan = true) }\n---\n`, {
|
||||
language: lang as "yaml",
|
||||
}),
|
||||
/JavaScript frontmatter/,
|
||||
)
|
||||
}
|
||||
assert.strictEqual(g.__quartzFrontmatterRan, undefined)
|
||||
})
|
||||
|
||||
test("invalid frontmatter is reported as a content error", () => {
|
||||
assert.throws(
|
||||
() => readFrontmatterData("---\ntitle: [unclosed\n---\nbody\n"),
|
||||
(err: Error) => isContentError(err) && err.message.startsWith("Invalid frontmatter: "),
|
||||
)
|
||||
})
|
||||
|
||||
test("a permalink is the page's address, not an alias (Obsidian Publish)", () => {
|
||||
const data: Record<string, unknown> = { permalink: "short", aliases: ["old-name"] }
|
||||
assert.deepStrictEqual(normalizeFrontmatter(data, "x").aliases, ["old-name"])
|
||||
|
||||
@@ -5,6 +5,7 @@ import toml from "toml"
|
||||
import { QuartzTransformerPlugin } from "../types"
|
||||
import { QuartzPluginData } from "../vfile"
|
||||
import { FrontmatterConfig } from "../../cfg"
|
||||
import { ContentError } from "../../util/pluginErrors"
|
||||
import {
|
||||
FilePath,
|
||||
FullSlug,
|
||||
@@ -130,20 +131,39 @@ export function normalizeFrontmatter(
|
||||
return { aliases: aliasSlugs }
|
||||
}
|
||||
|
||||
/**
|
||||
* gray-matter adds custom engines to its defaults, which include a `javascript`/`js` engine that
|
||||
* `eval`s the frontmatter (`---js`). Content is data, not code: refuse those languages.
|
||||
*/
|
||||
const refuseJavaScript = (): never => {
|
||||
throw new ContentError(
|
||||
"JavaScript frontmatter (---js / ---javascript) is not supported: Quartz does not execute code from content. Use YAML or TOML frontmatter.",
|
||||
)
|
||||
}
|
||||
|
||||
/** Parse a file's frontmatter with the configured delimiters and language (`{}` when it has none). */
|
||||
export function readFrontmatterData(
|
||||
source: Buffer | string,
|
||||
options: Pick<FrontmatterConfig, "delimiters" | "language"> = {},
|
||||
): Record<string, unknown> {
|
||||
const { data } = matter(Buffer.isBuffer(source) ? source : Buffer.from(source), {
|
||||
delimiters: options.delimiters ?? frontmatterDefaults.delimiters,
|
||||
language: options.language ?? frontmatterDefaults.language,
|
||||
engines: {
|
||||
yaml: (s) => yaml.load(s, { schema: yaml.JSON_SCHEMA }) as object,
|
||||
toml: (s) => toml.parse(s) as object,
|
||||
},
|
||||
})
|
||||
return data
|
||||
try {
|
||||
const { data } = matter(Buffer.isBuffer(source) ? source : Buffer.from(source), {
|
||||
delimiters: options.delimiters ?? frontmatterDefaults.delimiters,
|
||||
language: options.language ?? frontmatterDefaults.language,
|
||||
engines: {
|
||||
yaml: (s) => yaml.load(s, { schema: yaml.JSON_SCHEMA }) as object,
|
||||
toml: (s) => toml.parse(s) as object,
|
||||
javascript: refuseJavaScript,
|
||||
js: refuseJavaScript,
|
||||
},
|
||||
})
|
||||
return data
|
||||
} catch (err) {
|
||||
if (err instanceof ContentError) throw err
|
||||
throw new ContentError(`Invalid frontmatter: ${err instanceof Error ? err.message : err}`, {
|
||||
cause: err,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const Frontmatter: QuartzTransformerPlugin<FrontmatterConfig> = (userOpts) => {
|
||||
|
||||
@@ -3,7 +3,13 @@ import assert from "node:assert"
|
||||
import { unified, type Plugin } from "unified"
|
||||
import remarkParse from "remark-parse"
|
||||
import { VFile } from "vfile"
|
||||
import { attributePluggables, pluginErrorInfo, PluginError } from "./pluginErrors"
|
||||
import {
|
||||
attributePluggables,
|
||||
pluginErrorInfo,
|
||||
PluginError,
|
||||
ContentError,
|
||||
isContentError,
|
||||
} from "./pluginErrors"
|
||||
import {
|
||||
recordDiagnostic,
|
||||
resetDiagnostics,
|
||||
@@ -133,6 +139,19 @@ describe("describePluginError", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("content errors", () => {
|
||||
test("are recognised through a plugin attribution", () => {
|
||||
const origin = { builtin: true, displayName: "frontmatter", source: "frontmatter" }
|
||||
const wrapped = new PluginError(new ContentError("Invalid frontmatter: bad"), {
|
||||
origin,
|
||||
stage: "markdown",
|
||||
} as never)
|
||||
assert.strictEqual(isContentError(wrapped), true)
|
||||
assert.strictEqual(isContentError(new PluginError(new Error("x"), { origin } as never)), false)
|
||||
assert.strictEqual(isContentError(undefined), false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("unresolved links report", () => {
|
||||
beforeEach(() => resetDiagnostics())
|
||||
|
||||
|
||||
@@ -38,6 +38,28 @@ export class PluginError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An error in a file's content (e.g. invalid frontmatter), not in Quartz or a plugin: `trace()` tells
|
||||
* the user to fix the file instead of reporting a bug. `quartzContent` survives module copies.
|
||||
*/
|
||||
export class ContentError extends Error {
|
||||
readonly quartzContent = true
|
||||
constructor(message: string, options?: { cause?: unknown }) {
|
||||
super(message, options)
|
||||
this.name = "ContentError"
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether `err` or one of its causes (e.g. inside a {@link PluginError}) is a {@link ContentError}. */
|
||||
export function isContentError(err: unknown): boolean {
|
||||
let e = err
|
||||
for (let depth = 0; depth < 10 && e && typeof e === "object"; depth++) {
|
||||
if ((e as { quartzContent?: unknown }).quartzContent === true) return true
|
||||
e = (e as { cause?: unknown }).cause
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export function pluginErrorInfo(err: unknown): PluginErrorInfo | undefined {
|
||||
const info = (err as { quartzPlugin?: PluginErrorInfo } | null)?.quartzPlugin
|
||||
return info && typeof info === "object" && info.origin ? info : undefined
|
||||
|
||||
+23
-4
@@ -1,7 +1,8 @@
|
||||
import { styleText } from "util"
|
||||
import process from "process"
|
||||
import esbuild from "esbuild"
|
||||
import { isMainThread } from "workerpool"
|
||||
import { pluginErrorInfo, PluginErrorInfo } from "./pluginErrors"
|
||||
import { isContentError, pluginErrorInfo, PluginErrorInfo } from "./pluginErrors"
|
||||
import { knownProblemsFor } from "./diagnostics"
|
||||
|
||||
const rootFile = /.*at file:/
|
||||
@@ -96,13 +97,20 @@ export function trace(msg: string, err: Error) {
|
||||
styleText("red", ` ${msg}`) +
|
||||
(err.message.length > 0 ? `: ${err.message}` : ""),
|
||||
)
|
||||
if (info) {
|
||||
const contentError = isContentError(err)
|
||||
if (contentError) {
|
||||
// the file needs fixing, not Quartz or a plugin: no plugin details or stack trace
|
||||
lines.push("")
|
||||
lines.push(
|
||||
styleText("gray", " This is a problem in the file's content: fix it and build again."),
|
||||
)
|
||||
} else if (info) {
|
||||
lines.push("")
|
||||
lines.push(...describePluginError(info))
|
||||
}
|
||||
lines.push("")
|
||||
|
||||
let reachedEndOfLegibleTrace = false
|
||||
let reachedEndOfLegibleTrace = contentError
|
||||
for (const line of stack.split("\n").slice(1)) {
|
||||
if (reachedEndOfLegibleTrace) {
|
||||
break
|
||||
@@ -123,6 +131,17 @@ export function trace(msg: string, err: Error) {
|
||||
} else {
|
||||
// print and exit
|
||||
console.error(traceMsg)
|
||||
process.exit(1)
|
||||
exitBuild(1)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exit the build process. esbuild's service is a child process that shares our stderr; exiting
|
||||
* while it has requests in flight (bundling component scripts, the CLI's build context) left it
|
||||
* running without its parent, and it printed "fatal error: all goroutines are asleep - deadlock!"
|
||||
* and a goroutine dump after Quartz's error. Stop it first.
|
||||
*/
|
||||
export function exitBuild(code: number): never {
|
||||
void esbuild.stop()
|
||||
process.exit(code)
|
||||
}
|
||||
Reference in new issue
Block a user