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
33 changes: 33 additions & 0 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ import {
*/
export class YouMdParserImpl implements YouMdParser {
private readonly maxFileSize: number;
private static readonly ALLOWED_REMOTE_CONTENT_TYPES = [
"text/markdown",
"text/plain",
"text/x-markdown",
"application/markdown",
];

constructor(options?: { maxFileSize?: number }) {
this.maxFileSize = options?.maxFileSize ?? MAX_FILE_SIZE;
Expand Down Expand Up @@ -350,6 +356,33 @@ export class YouMdParserImpl implements YouMdParser {
};
}

const contentTypeHeader = response.headers.get("content-type");
if (contentTypeHeader) {
const normalizedContentType = contentTypeHeader
.split(";")[0]
?.trim()
.toLowerCase();

if (
normalizedContentType &&
!YouMdParserImpl.ALLOWED_REMOTE_CONTENT_TYPES.includes(
normalizedContentType
)
) {
return {
profile: createEmptyProfile(),
success: false,
errors: [
{
code: "NETWORK_ERROR",
message: `Unsupported content type: ${normalizedContentType}`,
},
],
warnings: [],
};
}
}

const maxSize = parseOptions?.maxFileSize ?? this.maxFileSize;
const contentLengthHeader = response.headers.get("content-length");
if (contentLengthHeader) {
Expand Down
45 changes: 45 additions & 0 deletions test/parser/loadFromUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,49 @@ describe("loadFromUrl hardening", () => {
expect(result.success).toBe(false);
expect(result.errors.some((e) => e.code === "FILE_TOO_LARGE")).toBe(true);
});

it("rejects unsupported content types before reading body", async () => {
const parser = createParser();
const textSpy = vi.fn(async () => "---\nschema_version: \"1.1\"\n---\n# Me");

vi.spyOn(globalThis, "fetch").mockResolvedValue({
ok: true,
status: 200,
statusText: "OK",
headers: {
get: (name: string) =>
name.toLowerCase() === "content-type" ? "application/json" : null,
},
text: textSpy,
} as unknown as Response);

const result = await parser.loadFromUrl("https://example.com/profile.md");

expect(result.success).toBe(false);
expect(result.errors.some((e) => e.code === "NETWORK_ERROR")).toBe(true);
expect(result.errors[0]?.message).toContain("Unsupported content type");
expect(textSpy).not.toHaveBeenCalled();
});

it("accepts allowed content types with charset parameters", async () => {
const parser = createParser();

vi.spyOn(globalThis, "fetch").mockResolvedValue({
ok: true,
status: 200,
statusText: "OK",
headers: {
get: (name: string) =>
name.toLowerCase() === "content-type"
? "text/markdown; charset=utf-8"
: null,
},
body: null,
text: vi.fn(async () => "---\nschema_version: \"1.1\"\n---\n# Me"),
} as unknown as Response);

const result = await parser.loadFromUrl("https://example.com/profile.md");

expect(result.success).toBe(true);
});
});
Loading