gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] branch master updated (9ad7d47e -> a9d85711)


From: gnunet
Subject: [libeufin] branch master updated (9ad7d47e -> a9d85711)
Date: Mon, 12 Feb 2024 16:22:58 +0100

This is an automated email from the git hooks/post-receive script.

antoine pushed a change to branch master
in repository libeufin.

    from 9ad7d47e again
     new b8a29f97 Fix BodyLimitAndDecompression plugin
     new a9d85711 Remove automatic EBICS fetch date range for now

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 bank/src/main/kotlin/tech/libeufin/bank/Error.kt   |  5 ++
 bank/src/main/kotlin/tech/libeufin/bank/Main.kt    | 55 +++++++++++++---------
 bank/src/test/kotlin/SecurityTest.kt               | 26 +++++++++-
 bank/src/test/kotlin/WireGatewayApiTest.kt         |  2 +-
 common/src/main/kotlin/Client.kt                   | 19 ++------
 .../main/kotlin/tech/libeufin/nexus/EbicsFetch.kt  | 20 +++-----
 6 files changed, 72 insertions(+), 55 deletions(-)

diff --git a/bank/src/main/kotlin/tech/libeufin/bank/Error.kt 
b/bank/src/main/kotlin/tech/libeufin/bank/Error.kt
index eacf6a08..7fd4c3b5 100644
--- a/bank/src/main/kotlin/tech/libeufin/bank/Error.kt
+++ b/bank/src/main/kotlin/tech/libeufin/bank/Error.kt
@@ -120,6 +120,11 @@ fun badRequest(
     detail: String? = null
 ): LibeufinException = libeufinError(HttpStatusCode.BadRequest, hint, error, 
detail)
 
+fun unsupportedMediaType(
+    hint: String, 
+    error: TalerErrorCode = TalerErrorCode.END,
+): LibeufinException = libeufinError(HttpStatusCode.UnsupportedMediaType, 
hint, error)
+
 
 /* ----- Currency checks ----- */
 
diff --git a/bank/src/main/kotlin/tech/libeufin/bank/Main.kt 
b/bank/src/main/kotlin/tech/libeufin/bank/Main.kt
index f91d2cdd..f0d8ac89 100644
--- a/bank/src/main/kotlin/tech/libeufin/bank/Main.kt
+++ b/bank/src/main/kotlin/tech/libeufin/bank/Main.kt
@@ -70,32 +70,41 @@ val bodyPlugin = 
createApplicationPlugin("BodyLimitAndDecompression") {
         transformBody { body ->
             val bytes = ByteArray(MAX_BODY_LENGTH.toInt() + 1)
             var read = 0;
-            if (call.request.headers[HttpHeaders.ContentEncoding] == 
"deflate") {
-                // Decompress and check decompressed length
-                val inflater = Inflater()
-                while (!body.isClosedForRead) {
-                    body.read { buf ->
-                        inflater.setInput(buf)
-                        try {
-                            read += inflater.inflate(bytes, read, bytes.size - 
read)
-                        } catch (e: DataFormatException) {
-                            logger.error("Deflated request failed to inflate: 
${e.message}")
-                            throw badRequest(
-                                "Could not inflate request",
-                                TalerErrorCode.GENERIC_COMPRESSION_INVALID
-                            )
+            when (val encoding = 
call.request.headers[HttpHeaders.ContentEncoding])  {
+                "deflate" -> {
+                    // Decompress and check decompressed length
+                    val inflater = Inflater()
+                    while (!body.isClosedForRead) {
+                        body.read { buf ->
+                            inflater.setInput(buf)
+                            try {
+                                read += inflater.inflate(bytes, read, 
bytes.size - read)
+                            } catch (e: DataFormatException) {
+                                logger.error("Deflated request failed to 
inflate: ${e.message}")
+                                throw badRequest(
+                                    "Could not inflate request",
+                                    TalerErrorCode.GENERIC_COMPRESSION_INVALID
+                                )
+                            }
                         }
+                        if (read > MAX_BODY_LENGTH)
+                            throw badRequest("Decompressed body is 
suspiciously big > $MAX_BODY_LENGTH B")
                     }
-                    if (read > MAX_BODY_LENGTH)
-                        throw badRequest("Decompressed body is suspiciously 
big")
-                }
-            } else {
-                // Check body length
-                while (!body.isClosedForRead) {
-                    read += body.readAvailable(bytes, read, bytes.size - read)
-                    if (read > MAX_BODY_LENGTH)
-                        throw badRequest("Body is suspiciously big")
                 }
+                null -> {
+                    // Check body length
+                    while (true) {
+                        val new = body.readAvailable(bytes, read, bytes.size - 
read)
+                        if (new == -1) break; // Channel is closed
+                        read += new
+                        if (read > MAX_BODY_LENGTH)
+                            throw badRequest("Body is suspiciously big > 
$MAX_BODY_LENGTH B")
+                    }
+                } 
+                else -> throw unsupportedMediaType(
+                    "Content encoding '$encoding' not supported, expected 
plain or deflate",
+                    TalerErrorCode.GENERIC_COMPRESSION_INVALID
+                )
             }
             ByteReadChannel(bytes, 0, read)
         }
diff --git a/bank/src/test/kotlin/SecurityTest.kt 
b/bank/src/test/kotlin/SecurityTest.kt
index b3c5f5ac..b5559983 100644
--- a/bank/src/test/kotlin/SecurityTest.kt
+++ b/bank/src/test/kotlin/SecurityTest.kt
@@ -20,14 +20,30 @@
 import io.ktor.client.plugins.*
 import io.ktor.client.request.*
 import io.ktor.client.statement.*
+import io.ktor.http.*
 import io.ktor.http.content.*
 import io.ktor.server.engine.*
 import io.ktor.server.testing.*
 import kotlin.test.*
 import kotlinx.coroutines.*
+import kotlinx.serialization.json.*
 import org.junit.Test
 import tech.libeufin.bank.*
 import tech.libeufin.common.*
+import tech.libeufin.common.*
+import java.io.ByteArrayOutputStream
+import java.util.zip.DeflaterOutputStream
+
+inline fun <reified B> HttpRequestBuilder.jsonDeflate(b: B) {
+    val json = Json.encodeToString(kotlinx.serialization.serializer<B>(), b);
+    contentType(ContentType.Application.Json)
+    headers.set(HttpHeaders.ContentEncoding, "deflate")
+    val bos = ByteArrayOutputStream()
+    val ios = DeflaterOutputStream(bos)
+    ios.write(json.toByteArray())
+    ios.finish()
+    setBody(bos.toByteArray())
+}
 
 class SecurityTest {
     @Test
@@ -49,10 +65,16 @@ class SecurityTest {
 
         // Check body too big even after compression
         client.postA("/accounts/merchant/transactions") {
-            json(valid_req, deflate = true) {
+            jsonDeflate(obj(valid_req) {
                 "payto_uri" to 
"$exchangePayto?message=payout${"A".repeat(4100)}"
-            }
+            })
         }.assertBadRequest()
+
+        // Check uknown encoding
+        client.postA("/accounts/merchant/transactions") {
+            headers.set(HttpHeaders.ContentEncoding, "unknown")
+            json(valid_req)
+        }.assertStatus(HttpStatusCode.UnsupportedMediaType, 
TalerErrorCode.GENERIC_COMPRESSION_INVALID)
     }
 }
 
diff --git a/bank/src/test/kotlin/WireGatewayApiTest.kt 
b/bank/src/test/kotlin/WireGatewayApiTest.kt
index ec20ec15..ba1f3360 100644
--- a/bank/src/test/kotlin/WireGatewayApiTest.kt
+++ b/bank/src/test/kotlin/WireGatewayApiTest.kt
@@ -220,7 +220,7 @@ class WireGatewayApiTest {
         // Giving debt allowance and checking the OK case.
         setMaxDebt("merchant", "KUDOS:1000")
         
client.postA("/accounts/exchange/taler-wire-gateway/admin/add-incoming") {
-            json(valid_req, deflate = true)
+            json(valid_req)
         }.assertOk()
 
         // Trigger conflict due to reused reserve_pub
diff --git a/common/src/main/kotlin/Client.kt b/common/src/main/kotlin/Client.kt
index 785e3f32..608a11b4 100644
--- a/common/src/main/kotlin/Client.kt
+++ b/common/src/main/kotlin/Client.kt
@@ -24,7 +24,6 @@ import kotlinx.serialization.json.*
 import io.ktor.client.request.*
 import io.ktor.client.statement.*
 import java.io.ByteArrayOutputStream
-import java.util.zip.DeflaterOutputStream
 import kotlin.test.assertEquals
 
 /* ----- Json DSL ----- */
@@ -46,27 +45,17 @@ class JsonBuilder(from: JsonObject) {
 
 /* ----- Json body helper ----- */
 
-inline fun <reified B> HttpRequestBuilder.json(b: B, deflate: Boolean = false) 
{
+inline fun <reified B> HttpRequestBuilder.json(b: B) {
     val json = Json.encodeToString(kotlinx.serialization.serializer<B>(), b);
     contentType(ContentType.Application.Json)
-    if (deflate) {
-        headers.set("Content-Encoding", "deflate")
-        val bos = ByteArrayOutputStream()
-        val ios = DeflaterOutputStream(bos)
-        ios.write(json.toByteArray())
-        ios.finish()
-        setBody(bos.toByteArray())
-    } else {
-        setBody(json)
-    }
+    setBody(json)
 }
 
 inline fun HttpRequestBuilder.json(
-    from: JsonObject = JsonObject(emptyMap()), 
-    deflate: Boolean = false, 
+    from: JsonObject = JsonObject(emptyMap()),
     builderAction: JsonBuilder.() -> Unit
 ) {
-    json(obj(from, builderAction), deflate)
+    json(obj(from, builderAction))
 }
 
 inline suspend fun <reified B> HttpResponse.json(): B =
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsFetch.kt 
b/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsFetch.kt
index 41ceb334..978c11de 100644
--- a/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsFetch.kt
+++ b/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsFetch.kt
@@ -363,22 +363,14 @@ private suspend fun fetchDocuments(
     ctx: FetchContext,
     docs: List<Document>
 ): Boolean {
-    /**
-     * Getting the least execution between the latest incoming
-     * and outgoing payments.  This way, if ingesting outgoing
-     * (incoming) payments crashed, we make sure we request from
-     * the last successful outgoing (incoming) payment execution
-     * time, to obtain again from the bank those payments that did
-     * not make it to the database due to the crash.
-     */
-    val lastIncomingTime = db.incomingPaymentLastExecTime()
-    val lastOutgoingTime = db.outgoingPaymentLastExecTime()
-    val requestFrom: Instant? = minTimestamp(lastIncomingTime, 
lastOutgoingTime)
-
-    val lastExecutionTime: Instant? = ctx.pinnedStart ?: requestFrom
+    val lastExecutionTime: Instant? = ctx.pinnedStart;
     return docs.all { doc ->
         try {
-            logger.info("Fetching '${doc.fullDescription()}' from timestamp: 
$lastExecutionTime")
+            if (lastExecutionTime != null) {
+                logger.info("Fetching new '${doc.fullDescription()}'")
+            } else {
+                logger.info("Fetching '${doc.fullDescription()}' from 
timestamp: $lastExecutionTime")
+            }
             val doc = doc.doc()
             // downloading the content
             val content = downloadHelper(ctx, lastExecutionTime, doc)

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]