Skip to content

Commit fda9394

Browse files
committed
Improve handling of invalid comment in fromRfc2822 function
1 parent b90059e commit fda9394

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/datetime/fromRfc2822.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ test("comment", () => {
113113
).toEqual(Temporal.Instant.from("2024-06-07T01:23:45Z"));
114114
});
115115

116+
test("unbalanced comment", () => {
117+
expect(() =>
118+
fromRfc2822("07 Jun 2024 01:23:45 +0000 (comment", Temporal.Instant),
119+
).toThrowError();
120+
expect(() =>
121+
fromRfc2822("07 Jun 2024 (comment)) 01:23:45 +0000", Temporal.Instant),
122+
).toThrowError();
123+
});
124+
116125
test("invalid day of week", () => {
117126
expect(() => {
118127
fromRfc2822("Mot, 07 Jun 2024 01:23:45 +0900", Temporal.PlainDateTime);

src/datetime/fromRfc2822.ts

+6
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ function removeComment(str: string) {
2828
// comment ended
2929
lastNonCommentStarted = m.index + 1;
3030
}
31+
if (commentNestLevel < 0) {
32+
throw new Error("Unbalanced nested comment");
33+
}
3134
}
3235
}
36+
if (commentNestLevel !== 0) {
37+
throw new Error("Unbalanced nested comment");
38+
}
3339
return res;
3440
}
3541

0 commit comments

Comments
 (0)