@@ -71,7 +71,7 @@ static void CurlCleanup(CURL * curl, struct curl_slist *headerList);
7171static HttpResult CurlReturnError (CURL * curl , struct curl_slist * headerList ,
7272 CURLcode curlCode , const char * errorMsg );
7373static const char * HttpRequestMethodToString (HttpMethod method );
74- static char * RedactSensitiveStrings (char * s );
74+ static char * RedactSensitiveJson (char * s );
7575
7676#define CURL_SETOPT (curl , opt , value ) do { \
7777 curlCode = curl_easy_setopt((curl), (opt), (value)); \
@@ -464,7 +464,7 @@ HttpCommonNoThrows(HttpMethod method, const char *url, const char *postData, con
464464
465465 ereport (INFO , (errmsg ("making %s request to URL %s%s" ,
466466 HttpRequestMethodToString (method ), url ,
467- postDataInfo ? RedactSensitiveStrings (postDataInfo -> data ) : "" )));
467+ postDataInfo ? RedactSensitiveJson (postDataInfo -> data ) : "" )));
468468 }
469469
470470 if (!CheckMinCurlVersion (curl_version_info (CURLVERSION_NOW )))
@@ -531,7 +531,7 @@ HttpCommonNoThrows(HttpMethod method, const char *url, const char *postData, con
531531 if (HttpClientTraceTraffic && message_level_is_interesting (INFO ))
532532 {
533533 ereport (INFO , (errmsg ("received response with status code %ld, body: %s" ,
534- res .status , res .body ? RedactSensitiveStrings (res .body ) : "<empty>" )));
534+ res .status , res .body ? RedactSensitiveJson (res .body ) : "<empty>" )));
535535 }
536536
537537 return res ;
@@ -558,37 +558,90 @@ HttpRequestMethodToString(HttpMethod method)
558558 }
559559}
560560
561+ #include <string.h>
562+ #include <ctype.h>
561563
564+ /*
565+ * RedactSensitiveJson
566+ * In-place redaction of token-looking values in JSON-ish text.
567+ */
562568static char *
563- RedactSensitiveStrings (char * s )
569+ RedactSensitiveJson (char * input )
564570{
565- const char * patterns [] = {
566- "\"token\":" , "\"access-token\":" , "\"session-token\":" ,
567- "\"authorization\":" , "\"Authorization\":" , "Bearer "
571+ char * copyOfinput = pstrdup (input );
572+
573+ const char * keys [] = {
574+ "\"access_token\"" ,
575+ "\"refresh_token\"" ,
576+ "\"id_token\"" ,
577+ "\"session_token\"" ,
578+ "\"token\"" ,
579+ "\"access-token\"" ,
580+ "\"authorization\"" ,
581+ "\"Authorization\""
568582 };
583+ const int keyCount = sizeof (keys ) / sizeof (keys [0 ]);
569584
570- const int patternCount = sizeof (patterns ) / sizeof (patterns [0 ]);
571-
572- for (int i = 0 ; i < patternCount ; i ++ )
585+ for (int i = 0 ; i < keyCount ; i ++ )
573586 {
574- char * p = s ;
587+ const char * key = keys [i ];
588+ char * p = copyOfinput ;
575589
576- while ((p = strstr (p , patterns [ i ] )) != NULL )
590+ while ((p = strstr (p , key )) != NULL )
577591 {
578- p += strlen (patterns [i ]);
592+ /* Move to the colon after the key */
593+ char * colon = strchr (p + strlen (key ), ':' );
594+
595+ if (colon == NULL )
596+ {
597+ /* No colon? then this isn't a key-value pair, skip */
598+ p += strlen (key );
599+ continue ;
600+ }
601+
602+ char * v = colon + 1 ; /* start of value (maybe spaces /
603+ * quote) */
604+
605+ /* Skip whitespace */
606+ while (* v && isspace ((unsigned char ) * v ))
607+ v ++ ;
579608
580- /* Skip whitespace and possible quotes */
581- while (* p == ' ' || * p == '\"' )
582- p ++ ;
609+ int quoted = 0 ;
583610
584- /* Now redact until whitespace, quote or comma */
585- while (* p && * p != '\"' && * p != ',' && * p != '\n' && * p != ' ' )
611+ if (* v == '"' )
586612 {
587- * p = '*' ;
588- p ++ ;
613+ quoted = 1 ;
614+ v ++ ; /* move to first character of value */
589615 }
616+
617+ char * q = v ;
618+
619+ if (quoted )
620+ {
621+ /* Redact until the closing quote */
622+ while (* q && * q != '"' )
623+ {
624+ * q = '*' ;
625+ q ++ ;
626+ }
627+ }
628+ else
629+ {
630+ /* Redact until comma, closing brace, or whitespace */
631+ while (* q &&
632+ * q != ',' &&
633+ * q != '}' &&
634+ !isspace ((unsigned char ) * q ))
635+ {
636+ * q = '*' ;
637+ q ++ ;
638+ }
639+ }
640+
641+ /* Continue search after the value we just redacted */
642+ p = q ;
590643 }
591644 }
592645
593- return s ;
646+ return copyOfinput ;
594647}
0 commit comments