-
Notifications
You must be signed in to change notification settings - Fork 58
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
chore: add timestamp and ephemeral for opt-in dos validator #1713
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,20 @@ else: | |
{.push raises: [].} | ||
|
||
import | ||
std/[math,times], | ||
chronicles, | ||
chronos, | ||
metrics, | ||
stew/byteutils, | ||
stew/endians2, | ||
libp2p/protocols/pubsub/gossipsub, | ||
libp2p/protocols/pubsub/rpc/messages, | ||
libp2p/protocols/pubsub/errors, | ||
nimcrypto/sha2, | ||
secp256k1 | ||
|
||
const MessageWindowInSec = 5*60 # +- 5 minutes | ||
|
||
import | ||
../../waku/v2/waku_relay/protocol, | ||
../../waku/v2/waku_core | ||
|
@@ -29,9 +33,26 @@ proc msgHash*(pubSubTopic: string, msg: WakuMessage): array[32, byte] = | |
ctx.update(pubsubTopic.toBytes()) | ||
ctx.update(msg.payload) | ||
ctx.update(msg.contentTopic.toBytes()) | ||
ctx.update(msg.timestamp.uint64.toBytes(Endianness.littleEndian)) | ||
ctx.update(if msg.ephemeral: @[1.byte] else: @[0.byte]) | ||
|
||
return ctx.finish() | ||
|
||
proc withinTimeWindow*(msg: WakuMessage): bool = | ||
# Returns true if the message timestamp is: | ||
# abs(now - msg.timestamp) < MessageWindowInSec | ||
let ts = msg.timestamp | ||
let now = getNanosecondTime(getTime().toUnixFloat()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes right, i keep forgeting. fixed 3092a51 |
||
let window = getNanosecondTime(MessageWindowInSec) | ||
|
||
if now > ts: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. open for other ways. cant do abs(now-ts) since im working with uint64 and if I cast to signed, I will lose precision. So its the way I found. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe https://nim-lang.org/docs/times.html#abs%2CDuration from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, mabe it is a completely stupid question, but would losing precision matter? Is the concern that someone could make the difference between TS and now overflow and thus the result of abs() be < window? Can we check for overflow? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ou
mm havent done the maths if matters or not. Difference between Mathematically is just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ouch indeed. Totally assumed it was fixed, way easier da7814a some quick napkin math @vpavlin int64 overflow with thanks all! |
||
if now - ts < window: | ||
return true | ||
else: | ||
if ts - now < window: | ||
return true | ||
return false | ||
|
||
proc addSignedTopicValidator*(w: WakuRelay, topic: PubsubTopic, publicTopicKey: SkPublicKey) = | ||
debug "adding validator to signed topic", topic=topic, publicTopicKey=publicTopicKey | ||
|
||
|
@@ -40,11 +61,13 @@ proc addSignedTopicValidator*(w: WakuRelay, topic: PubsubTopic, publicTopicKey: | |
var outcome = errors.ValidationResult.Reject | ||
|
||
if msg.isOk(): | ||
let msgHash = SkMessage(topic.msgHash(msg.get)) | ||
let recoveredSignature = SkSignature.fromRaw(msg.get.meta) | ||
if recoveredSignature.isOk(): | ||
if recoveredSignature.get.verify(msgHash, publicTopicKey): | ||
outcome = errors.ValidationResult.Accept | ||
if msg.get.timestamp != 0: | ||
if msg.get.withinTimeWindow(): | ||
let msgHash = SkMessage(topic.msgHash(msg.get)) | ||
let recoveredSignature = SkSignature.fromRaw(msg.get.meta) | ||
if recoveredSignature.isOk(): | ||
if recoveredSignature.get.verify(msgHash, publicTopicKey): | ||
outcome = errors.ValidationResult.Accept | ||
|
||
waku_msg_validator_signed_outcome.inc(labelValues = [$outcome]) | ||
return outcome | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should try to avoid importing
std/times
(especially when we usechronos.timer
constructs in the same module). In this case we need it for absolute system time, butwaku_core/time
provides the wrappers for that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed 3092a51