Blame SOURCES/0021-oidc_child-escape-scopes.patch

0034f4
From ace43c8ce02d19cf536ce35749aa2ed734089189 Mon Sep 17 00:00:00 2001
0034f4
From: Sumit Bose <sbose@redhat.com>
0034f4
Date: Thu, 18 Aug 2022 13:55:21 +0200
0034f4
Subject: [PATCH 21/24] oidc_child: escape scopes
0034f4
MIME-Version: 1.0
0034f4
Content-Type: text/plain; charset=UTF-8
0034f4
Content-Transfer-Encoding: 8bit
0034f4
0034f4
Before using the user provided scopes in the HTTP request they should be
0034f4
properly escaped according to RFC-3986.
0034f4
0034f4
Resolves: https://github.com/SSSD/sssd/issues/6146
0034f4
0034f4
Reviewed-by: Justin Stephenson <jstephen@redhat.com>
0034f4
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
0034f4
(cherry picked from commit 12d5c6344ee304c1f3bc155a76ab37fcd20e78cb)
0034f4
0034f4
Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
0034f4
---
0034f4
 src/oidc_child/oidc_child.c      |  4 ++--
0034f4
 src/oidc_child/oidc_child_curl.c | 35 ++++++++++++++++++++++++++++++++
0034f4
 src/oidc_child/oidc_child_util.h |  2 ++
0034f4
 3 files changed, 39 insertions(+), 2 deletions(-)
0034f4
0034f4
diff --git a/src/oidc_child/oidc_child.c b/src/oidc_child/oidc_child.c
0034f4
index e58afccd3..aeeac3595 100644
0034f4
--- a/src/oidc_child/oidc_child.c
0034f4
+++ b/src/oidc_child/oidc_child.c
0034f4
@@ -119,9 +119,9 @@ static errno_t set_endpoints(struct devicecode_ctx *dc_ctx,
0034f4
     }
0034f4
 
0034f4
     if (scope != NULL && *scope != '\0') {
0034f4
-        dc_ctx->scope = talloc_strdup(dc_ctx, scope);
0034f4
+        dc_ctx->scope = url_encode_string(dc_ctx, scope);
0034f4
         if (dc_ctx->scope == NULL) {
0034f4
-            DEBUG(SSSDBG_CRIT_FAILURE, "Failed to copy scopes.\n");
0034f4
+            DEBUG(SSSDBG_CRIT_FAILURE, "Failed to encode and copy scopes.\n");
0034f4
             ret = ENOMEM;
0034f4
             goto done;
0034f4
         }
0034f4
diff --git a/src/oidc_child/oidc_child_curl.c b/src/oidc_child/oidc_child_curl.c
0034f4
index 20e17a566..df438e007 100644
0034f4
--- a/src/oidc_child/oidc_child_curl.c
0034f4
+++ b/src/oidc_child/oidc_child_curl.c
0034f4
@@ -26,6 +26,41 @@
0034f4
 #include <curl/curl.h>
0034f4
 #include "oidc_child/oidc_child_util.h"
0034f4
 
0034f4
+char *url_encode_string(TALLOC_CTX *mem_ctx, const char *inp)
0034f4
+{
0034f4
+    CURL *curl_ctx = NULL;
0034f4
+    char *tmp;
0034f4
+    char *out = NULL;
0034f4
+
0034f4
+    if (inp == NULL) {
0034f4
+        DEBUG(SSSDBG_TRACE_ALL, "Empty input.\n");
0034f4
+        return NULL;
0034f4
+    }
0034f4
+
0034f4
+    curl_ctx = curl_easy_init();
0034f4
+    if (curl_ctx == NULL) {
0034f4
+        DEBUG(SSSDBG_OP_FAILURE, "Failed to initialize curl.\n");
0034f4
+        return NULL;
0034f4
+    }
0034f4
+
0034f4
+    tmp = curl_easy_escape(curl_ctx, inp, 0);
0034f4
+    if (tmp == NULL) {
0034f4
+        DEBUG(SSSDBG_TRACE_ALL, "curl_easy_escape failed for [%s].\n", inp);
0034f4
+        goto done;
0034f4
+    }
0034f4
+
0034f4
+    out = talloc_strdup(mem_ctx, tmp);
0034f4
+    curl_free(tmp);
0034f4
+    if (out == NULL) {
0034f4
+        DEBUG(SSSDBG_TRACE_ALL, "talloc_strdup failed.\n");
0034f4
+        goto done;
0034f4
+    }
0034f4
+
0034f4
+done:
0034f4
+    curl_easy_cleanup(curl_ctx);
0034f4
+    return (out);
0034f4
+}
0034f4
+
0034f4
 /* The curl write_callback will always append the received data. To start a
0034f4
  * new string call clean_http_data() before the curl request.*/
0034f4
 void clean_http_data(struct devicecode_ctx *dc_ctx)
0034f4
diff --git a/src/oidc_child/oidc_child_util.h b/src/oidc_child/oidc_child_util.h
0034f4
index c781bf1b1..ae5a72bc2 100644
0034f4
--- a/src/oidc_child/oidc_child_util.h
0034f4
+++ b/src/oidc_child/oidc_child_util.h
0034f4
@@ -61,6 +61,8 @@ struct devicecode_ctx {
0034f4
 };
0034f4
 
0034f4
 /* oidc_child_curl.c */
0034f4
+char *url_encode_string(TALLOC_CTX *mem_ctx, const char *inp);
0034f4
+
0034f4
 errno_t init_curl(void *p);
0034f4
 
0034f4
 void clean_http_data(struct devicecode_ctx *dc_ctx);
0034f4
-- 
0034f4
2.37.3
0034f4