Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Fix CreatedAtEventFilter was measuring time in msec
Browse files Browse the repository at this point in the history
  • Loading branch information
morum committed Mar 10, 2023
1 parent db08d8d commit b0189de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.nipsr.relay.filters.FilterType
import javax.enterprise.context.ApplicationScoped

/**
* An event filter that validates the signature of an event.
* An event filter that validates the creation date of an event.
*/
@NIP_22
@ApplicationScoped
Expand All @@ -24,17 +24,21 @@ class CreatedAtEventFilter(
override fun filter(event: Event<*>) : Pair<Boolean, String?> {
val maxCreatedAtDriftMinutes = settings.maxCreatedAtDriftMinutes()
val createdAt = event.created_at
val now = System.currentTimeMillis()
val createdAtDrift = now - createdAt
val createdAtDriftMinutes = createdAtDrift / 1000 / 60
val currentTimeSeconds = System.currentTimeMillis() / 1000
val createdAtDrift = currentTimeSeconds - createdAt
val createdAtDriftMinutes = createdAtDrift / 60
return if(createdAtDriftMinutes <= maxCreatedAtDriftMinutes){
ok()
} else {
false invalid "Event created_at is too far in the past, " +
"the allowed drift is $maxCreatedAtDriftMinutes minutes"
false invalid "The allowed time drift is $maxCreatedAtDriftMinutes minutes " +
"but a difference of $createdAtDriftMinutes minutes was detected."
}
}

override fun type() = FilterType.GLOBAL

}

fun main() {
println(System.currentTimeMillis())
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CreatedAtEventFilterTest {

@Test
fun `should not allow events with created_at outside params`(){
every { event.created_at } returns System.currentTimeMillis() - 1000 * 60 * (maxCreatedAtDriftMinutes + 1)
every { event.created_at } returns System.currentTimeMillis() / 1000 - 60 * (maxCreatedAtDriftMinutes + 1)
val (result, _) = createdAtEventFilter.filter(event)
assertFalse(result)
}
Expand Down

0 comments on commit b0189de

Please sign in to comment.