Conversation
ref no-issue - Seen in: https://ghost-foundation.sentry.io/issues/7272201684/?referrer=slack¬ification_uuid=d037b4c9-d06c-421b-9b72-8053ec1affe5&environment=production&alert_rule_id=15548254&alert_type=issue - When an incoming ActivityPub object (fetched via federation) has no published date, the code at post.service.ts:243 produced new Date('') an Invalid Date object. Since Invalid Date is not null/undefined, it passed through the ?? fallback in post.entity.ts:531 and reached MySQL as '0NaN-NaN-NaN NaN:NaN:NaN.NaN', causing ER_BAD_NULL_ERROR.
WalkthroughThis change introduces validation logic for the Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/http/api/views/account.posts.view.ts`:
- Around line 744-746: The DTO mapping for publishedAt directly uses new
Date(object.published) when object.published is truthy, which can yield an
Invalid Date for truthy-but-unparseable strings; update the publishedAt
assignment (in the mapping that produces the API response in account.posts.view)
to mirror Post.createFromData's validation: attempt to parse/construct the Date,
check validity with isNaN(date.getTime()) (or Date.parse) and only use the
parsed Date when valid, otherwise fall back to a safe default (e.g., new Date()
or null) so the API never emits an Invalid Date.
| publishedAt: object.published | ||
| ? new Date(object.published) | ||
| : new Date(), |
There was a problem hiding this comment.
Truthy-but-unparseable published values can still produce Invalid Date here.
Unlike Post.createFromData, this DTO mapping doesn't validate the result of new Date(object.published). If object.published is a non-empty but unparseable string (e.g. "not-a-date"), this will produce an Invalid Date in the API response.
Consider applying the same validation pattern used in the entity:
Suggested fix
- publishedAt: object.published
- ? new Date(object.published)
- : new Date(),
+ publishedAt: object.published && !Number.isNaN(new Date(object.published).getTime())
+ ? new Date(object.published)
+ : new Date(),📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| publishedAt: object.published | |
| ? new Date(object.published) | |
| : new Date(), | |
| publishedAt: object.published && !Number.isNaN(new Date(object.published).getTime()) | |
| ? new Date(object.published) | |
| : new Date(), |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/http/api/views/account.posts.view.ts` around lines 744 - 746, The DTO
mapping for publishedAt directly uses new Date(object.published) when
object.published is truthy, which can yield an Invalid Date for
truthy-but-unparseable strings; update the publishedAt assignment (in the
mapping that produces the API response in account.posts.view) to mirror
Post.createFromData's validation: attempt to parse/construct the Date, check
validity with isNaN(date.getTime()) (or Date.parse) and only use the parsed Date
when valid, otherwise fall back to a safe default (e.g., new Date() or null) so
the API never emits an Invalid Date.
ref no-issue