Skip to content

Build your personal knowledge base with Trilium Notes

fix(core): validate dates instead of ignoring broken guards

Elian Doran9b2d12d423d4
diff --git a/apps/desktop/src/services/security_settings.spec.ts b/apps/desktop/src/services/security_settings.spec.ts
index 3c744715d8..98ef0df2df 100644
--- a/apps/desktop/src/services/security_settings.spec.ts
+++ b/apps/desktop/src/services/security_settings.spec.ts
@@ -51,8 +51,8 @@ function invoke(channel: string, enabled: boolean) {
}
describe("security_settings", () => {
- // Handlers (and the module-level "suppress for session" flag) are registered
- // once; the suppression test runs last because it latches that flag on.
+ // Register the shared handlers once. The suppression test isolates its own
+ // module instance, so it no longer needs to run last.
beforeAll(() => {
securitySettings.registerSecurityIpcHandlers();
});
@@ -113,10 +113,16 @@ describe("security_settings", () => {
});
});
- // Must run last: ticking "don't ask again" latches the module-level
- // suppression flag for the rest of the process.
describe("don't-ask-again suppression", () => {
it("suppresses all subsequent dialogs once the checkbox is ticked", async () => {
+ // Ticking "don't ask again" latches a module-level flag that never clears.
+ // Re-import a throwaway copy of the module so that latch lives only on this
+ // isolated instance — it can't leak into other tests (or other spec files
+ // sharing the Vitest worker), so this test no longer has to run last.
+ vi.resetModules();
+ const isolated = await import("./security_settings.js");
+ isolated.registerSecurityIpcHandlers();
+
h.showMessageBox.mockResolvedValue({ response: 1, checkboxChecked: true });
expect(await invoke("security-set-backend-scripting", true)).toBe(true);
@@ -124,6 +130,9 @@ describe("security_settings", () => {
expect(await invoke("security-set-sql-console", true)).toBe(false);
expect(await invoke("security-set-backend-scripting", false)).toBe(false);
expect(h.showMessageBox).not.toHaveBeenCalled();
+
+ // Restore the shared, unlatched handlers so run order stays irrelevant.
+ securitySettings.registerSecurityIpcHandlers();
});
});
});
diff --git a/packages/trilium-core/src/services/utils/date.spec.ts b/packages/trilium-core/src/services/utils/date.spec.ts
index 6fa844eac9..ab70698371 100644
--- a/packages/trilium-core/src/services/utils/date.spec.ts
+++ b/packages/trilium-core/src/services/utils/date.spec.ts
@@ -46,12 +46,10 @@ describe("date utils", () => {
expect(parsed.toISOString()).toBe("2020-01-02T03:04:05.000Z");
});
- it("returns an Invalid Date for an unparseable string rather than throwing", () => {
- // Date.parse() returns NaN (it does not throw), so the catch path is
- // never hit and the result is an Invalid Date instance.
- const parsed = date.parseDateTime("not-a-date");
- expect(parsed).toBeInstanceOf(Date);
- expect(Number.isNaN(parsed.getTime())).toBe(true);
+ it("throws for an unparseable string", () => {
+ // Date.parse() returns NaN (it does not throw), so parseDateTime detects
+ // the NaN explicitly and raises a descriptive error.
+ expect(() => date.parseDateTime("not-a-date")).toThrow("Can't parse date from 'not-a-date'");
});
});
@@ -115,6 +113,13 @@ describe("date utils", () => {
"Invalid local date time format"
);
});
+
+ it("rejects a value that matches the format but is not a real date", () => {
+ // Passes the regex (right shape) but month 13 / impossible time => unparseable.
+ expect(date.validateLocalDateTime("2023-13-45 99:99:99.999+0200")).toContain(
+ "cannot be parsed"
+ );
+ });
});
describe("validateUtcDateTime", () => {
@@ -141,6 +146,11 @@ describe("date utils", () => {
// entirely wrong shape
expect(date.validateUtcDateTime("garbage")).toContain("Invalid UTC date time format");
});
+
+ it("rejects a value that matches the format but is not a real date", () => {
+ // Passes the regex (right shape) but month 13 / impossible time => unparseable.
+ expect(date.validateUtcDateTime("2023-13-45 99:99:99.999Z")).toContain("cannot be parsed");
+ });
});
describe("localNowDateTime", () => {
diff --git a/packages/trilium-core/src/services/utils/date.ts b/packages/trilium-core/src/services/utils/date.ts
index 5c5923b317..a31c297a05 100644
--- a/packages/trilium-core/src/services/utils/date.ts
+++ b/packages/trilium-core/src/services/utils/date.ts
@@ -44,12 +44,12 @@ function utcDateTimeStr(date: Date) {
* also is assumed to be GMT time (as indicated by the "Z" at the end), *not* local time
*/
function parseDateTime(str: string) {
- try {
- return new Date(Date.parse(str));
- } catch (e: any) {
- /* v8 ignore next -- Date.parse() returns NaN for bad input instead of throwing, so this catch is unreachable */
- throw new Error(`Can't parse date from '${str}': ${e.stack}`);
+ // Date.parse() returns NaN for bad input instead of throwing, so check for it explicitly.
+ const time = Date.parse(str);
+ if (Number.isNaN(time)) {
+ throw new Error(`Can't parse date from '${str}'`);
}
+ return new Date(time);
}
function parseLocalDate(str: string) {
@@ -72,11 +72,9 @@ function validateLocalDateTime(str: string | null | undefined) {
return `Invalid local date time format in '${str}'. Correct format shoud follow example: '2023-08-21 23:38:51.110+0200'`;
}
- /* v8 ignore start -- dayjs() always returns a truthy object, so this guard is unreachable */
- if (!dayjs(str, LOCAL_DATETIME_FORMAT)) {
+ if (!dayjs(str, LOCAL_DATETIME_FORMAT).isValid()) {
return `Date '${str}' appears to be in the correct format, but cannot be parsed. It likely represents an invalid date.`;
}
- /* v8 ignore stop */
}
function validateUtcDateTime(str: string | undefined) {
@@ -88,11 +86,9 @@ function validateUtcDateTime(str: string | undefined) {
return `Invalid UTC date time format in '${str}'. Correct format shoud follow example: '2023-08-21 23:38:51.110Z'`;
}
- /* v8 ignore start -- dayjs() always returns a truthy object, so this guard is unreachable */
- if (!dayjs(str, UTC_DATETIME_FORMAT)) {
+ if (!dayjs(str, UTC_DATETIME_FORMAT).isValid()) {
return `Date '${str}' appears to be in the correct format, but cannot be parsed. It likely represents an invalid date.`;
}
- /* v8 ignore stop */
}
function getLocalNowDateTime() {