Skip to content

Commit 8f2028f

Browse files
authored
Limit default from and to API parameters (#2384)
When not provided, we previously used unrestricted `from` and `to` parameters when reading from the DB. This can create issues when accidentally reading too much data. We now limit this to the last 24 hours, unless explicitly set by the caller. Note that when reading a lot of data, it's recommended to use postgres and read from a replica to ensure there's no impact on the running eclair instance. Fixes #2383
1 parent 4e3b377 commit 8f2028f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

eclair-node/src/main/scala/fr/acinq/eclair/api/directives/ExtraDirectives.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import fr.acinq.eclair.payment.Bolt11Invoice
3030
import fr.acinq.eclair.{MilliSatoshi, ShortChannelId, TimestampSecond}
3131

3232
import scala.concurrent.Future
33+
import scala.concurrent.duration.DurationInt
3334
import scala.util.{Failure, Success}
3435

3536
trait ExtraDirectives extends Directives {
@@ -42,8 +43,9 @@ trait ExtraDirectives extends Directives {
4243
val nodeIdFormParam: NameReceptacle[PublicKey] = "nodeId".as[PublicKey]
4344
val nodeIdsFormParam: NameUnmarshallerReceptacle[List[PublicKey]] = "nodeIds".as[List[PublicKey]](pubkeyListUnmarshaller)
4445
val paymentHashFormParam: NameUnmarshallerReceptacle[ByteVector32] = "paymentHash".as[ByteVector32](sha256HashUnmarshaller)
45-
val fromFormParam: NameDefaultUnmarshallerReceptacle[TimestampSecond] = "from".as[TimestampSecond](timestampSecondUnmarshaller).?(TimestampSecond.min)
46-
val toFormParam: NameDefaultUnmarshallerReceptacle[TimestampSecond] = "to".as[TimestampSecond](timestampSecondUnmarshaller).?(TimestampSecond.max)
46+
// we limit default values to avoid accidentally reading too much data from the DB
47+
val fromFormParam: NameDefaultUnmarshallerReceptacle[TimestampSecond] = "from".as[TimestampSecond](timestampSecondUnmarshaller).?(TimestampSecond.now() - 1.day)
48+
val toFormParam: NameDefaultUnmarshallerReceptacle[TimestampSecond] = "to".as[TimestampSecond](timestampSecondUnmarshaller).?(TimestampSecond.now())
4749
val amountMsatFormParam: NameReceptacle[MilliSatoshi] = "amountMsat".as[MilliSatoshi]
4850
val invoiceFormParam: NameReceptacle[Bolt11Invoice] = "invoice".as[Bolt11Invoice]
4951
val routeFormatFormParam: NameUnmarshallerReceptacle[RouteFormat] = "format".as[RouteFormat](routeFormatUnmarshaller)

eclair-node/src/test/scala/fr/acinq/eclair/api/ApiServiceSpec.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,10 @@ class ApiServiceSpec extends AnyFunSuite with ScalatestRouteTest with IdiomaticM
10961096
check {
10971097
assert(handled)
10981098
assert(status == OK)
1099-
eclair.audit(TimestampSecond.min, TimestampSecond.max)(any[Timeout]).wasCalled(once)
1099+
// The default is to get data for the last day.
1100+
val from = TimestampSecond.now() - 1.day
1101+
val to = TimestampSecond.now()
1102+
eclair.audit(argThat[TimestampSecond](t => from - 1.minute <= t && t <= from + 1.minute), argThat[TimestampSecond](t => to - 1.minute <= t && t <= to + 1.minute))(any[Timeout]).wasCalled(once)
11001103
}
11011104

11021105
Post("/audit", FormData("from" -> TimestampSecond.min.toLong.toString, "to" -> TimestampSecond.max.toLong.toString)) ~>
@@ -1105,7 +1108,7 @@ class ApiServiceSpec extends AnyFunSuite with ScalatestRouteTest with IdiomaticM
11051108
check {
11061109
assert(handled)
11071110
assert(status == OK)
1108-
eclair.audit(TimestampSecond.min, TimestampSecond.max)(any[Timeout]).wasCalled(twice)
1111+
eclair.audit(TimestampSecond.min, TimestampSecond.max)(any[Timeout]).wasCalled(once)
11091112
}
11101113

11111114
Post("/audit", FormData("from" -> 123456.toString, "to" -> 654321.toString)) ~>

0 commit comments

Comments
 (0)