Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Encode SARIF file locations correctly for repository-relative and native absolute paths, including Unicode, spaces, percent signs and URI delimiters, across scan, prompt-lint and text-audit reports.

## 0.1.0

- Added the first productized CLI: `scan`, `doctor`, and `init`.
Expand Down
3 changes: 2 additions & 1 deletion src/prompt/promptYamlLint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "node:fs";
import { artifactUri } from "../report/artifactUri.js";
import type { ReportFormat } from "../types.js";

interface PromptCheck {
Expand Down Expand Up @@ -179,7 +180,7 @@ function formatPromptSarif(report: PromptLintReport): object {
locations: [
{
physicalLocation: {
artifactLocation: { uri: report.file }
artifactLocation: { uri: artifactUri(report.file) }
}
}
]
Expand Down
8 changes: 8 additions & 0 deletions src/report/artifactUri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import path from "node:path";
import { pathToFileURL } from "node:url";

/** SARIF locations are URI references, not raw filesystem paths. */
export function artifactUri(file: string): string {
if (path.isAbsolute(file)) return pathToFileURL(file).href;
return file.replaceAll("\\", "/").split("/").map(encodeURIComponent).join("/");
}
3 changes: 2 additions & 1 deletion src/report/sarif.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Report } from "../types.js";
import { artifactUri } from "./artifactUri.js";

export function formatSarif(report: Report): object {
const rules = Array.from(new Map(report.findings.map((finding) => [
Expand Down Expand Up @@ -37,7 +38,7 @@ export function formatSarif(report: Report): object {
? [
{
physicalLocation: {
artifactLocation: { uri: finding.file },
artifactLocation: { uri: artifactUri(finding.file) },
region: { startLine: finding.line ?? 1 }
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/text/textAudit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import { artifactUri } from "../report/artifactUri.js";
import type { ReportFormat } from "../types.js";

interface TextAuditCheck {
Expand Down Expand Up @@ -417,7 +418,7 @@ function formatSarif(report: TextAuditReport): unknown {
locations: [
{
physicalLocation: {
artifactLocation: { uri: report.target.replaceAll("\\", "/") },
artifactLocation: { uri: artifactUri(report.target) },
region: { startLine: 1 }
}
}
Expand Down
77 changes: 77 additions & 0 deletions tests/sarif.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import { runCli } from "../src/cli.js";
import { formatPromptLintReport } from "../src/prompt/promptYamlLint.js";
import { renderReport } from "../src/report/write.js";
import { formatTextAuditReport } from "../src/text/textAudit.js";
import type { Report } from "../src/types.js";

function firstUri(serialized: string): string {
return JSON.parse(serialized).runs[0].results[0].locations[0].physicalLocation.artifactLocation.uri as string;
}

const renderers = {
scan(file: string): string {
const report: Report = {
tool: { name: "synthetic", version: "0.0.0" }, root: "/synthetic", generatedAt: "2026-01-01T00:00:00Z",
score: 80, grade: "B", summary: { critical: 0, high: 0, medium: 1, low: 0, info: 0, total: 1 }, facts: {},
findings: [{ id: "synthetic.rule", title: "Synthetic finding", severity: "medium", file, line: 7,
why: "Synthetic reason", next: "Review synthetic fixture", scanner: "synthetic" }]
};
return renderReport(report, "sarif");
},
prompt(file: string): string {
return formatPromptLintReport({ file, score: 0, results: [{ status: "FAIL", check: "synthetic", message: "Synthetic", weight: 1 }] }, "sarif");
},
text(file: string): string {
return formatTextAuditReport({ tool: "agent-reliability-kit text-audit", profile: "synthetic", sourceRepo: "synthetic",
target: file, title: "Synthetic", score: 0, passed: 0, total: 1, redacted: "Synthetic",
results: [{ status: "WARN", check: "synthetic", message: "Synthetic" }] }, "sarif");
}
};

describe.each(Object.entries(renderers))("%s SARIF file locations", (_name, render) => {
it("preserves ordinary repository-relative paths", () => {
expect(firstUri(render(".github/workflows/ci.yml"))).toBe(".github/workflows/ci.yml");
});
it("roundtrips Unicode and URI delimiters as literal filename characters", () => {
const file = "docs/资料 #50%25/review?.yml";
const uri = firstUri(render(file));
expect(uri).toBe("docs/%E8%B5%84%E6%96%99%20%2350%2525/review%3F.yml");
const resolved = new URL(uri, "file:///synthetic/");
expect(resolved.search).toBe("");
expect(resolved.hash).toBe("");
expect(decodeURIComponent(resolved.pathname)).toBe("/synthetic/" + file);
});
it("keeps a first-segment colon from becoming a URI scheme", () => {
const resolved = new URL(firstUri(render("report:version.yml")), "file:///synthetic/");
expect(resolved.protocol).toBe("file:");
expect(decodeURIComponent(resolved.pathname)).toBe("/synthetic/report:version.yml");
});
it("roundtrips native absolute paths through file URIs", () => {
const file = path.join(os.tmpdir(), "synthetic 资料 #50%25.yml");
const uri = firstUri(render(file));
expect(new URL(uri).protocol).toBe("file:");
expect(fileURLToPath(uri)).toBe(file);
});
});

describe("SARIF CLI locations", () => {
it.each(["prompt-lint", "text-audit"])("serializes an actual %s input filename without changing its identity", (command) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "ark-sarif-"));
const file = path.join(root, "synthetic 资料 #50%25.yml");
fs.writeFileSync(file, "# Synthetic incomplete fixture\n", "utf8");
const stdout: string[] = [];
const stderr: string[] = [];
const profile = command === "text-audit" ? ["--profile", "agents-md"] : [];
const code = runCli([command, file, ...profile, "--format", "sarif", "--min-score", "0"], {
cwd: root, stdout: (message = "") => stdout.push(message), stderr: (message = "") => stderr.push(message)
});
expect(stderr).toEqual([]);
expect(code).toBe(0);
expect(fileURLToPath(firstUri(stdout.join("\n")))).toBe(file);
});
});
Loading