Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix [5071]: Post date can be altered by computer's date #5072

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
12 changes: 12 additions & 0 deletions src/lib/api/get-server-date.ts

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but how will this addapt to the timezone of the client?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe get server date and timezone info from client, then apply the difference on server date?

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Will get the current time on the server
* because the client time is not accurate
* and can make user manipulate the time of the post
*/
export const getServerDate = async () => {
const createdAt = await fetch('https://worldtimeapi.org/api/timezone/Etc/UTC')
const response = await createdAt.json()
const date = new Date(response.datetime)

return date.toISOString()
}
4 changes: 4 additions & 0 deletions src/lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {isNative} from 'platform/detection'
import {ImageModel} from 'state/models/media/image'
import {LinkMeta} from '../link-meta/link-meta'
import {safeDeleteAsync} from '../media/manip'
import {getServerDate} from './get-server-date'
import {uploadBlob} from './upload-blob'

export {uploadBlob}
Expand Down Expand Up @@ -268,13 +269,16 @@ export async function post(agent: BskyAgent, opts: PostOpts) {
let res
try {
opts.onStateChange?.('Posting...')
const date = await getServerDate()

res = await agent.post({
text: rt.text,
facets: rt.facets,
reply,
embed,
langs,
labels,
createdAt: date,
})
} catch (e: any) {
logger.error(`Failed to create post`, {
Expand Down