diff --git a/src/journal-remote/microhttpd-util.c b/src/journal-remote/microhttpd-util.c index 9e6c36f87d..c1e35b7ed3 100644 --- a/src/journal-remote/microhttpd-util.c +++ b/src/journal-remote/microhttpd-util.c @@ -25,11 +25,13 @@ void microhttpd_logger(void *arg, const char *fmt, va_list ap) { REENABLE_WARNING; } -static int mhd_respond_internal(struct MHD_Connection *connection, - enum MHD_RequestTerminationCode code, - const char *buffer, - size_t size, - enum MHD_ResponseMemoryMode mode) { +int mhd_respond_internal( + struct MHD_Connection *connection, + enum MHD_RequestTerminationCode code, + const char *buffer, + size_t size, + enum MHD_ResponseMemoryMode mode) { + assert(connection); _cleanup_(MHD_destroy_responsep) struct MHD_Response *response @@ -43,29 +45,16 @@ static int mhd_respond_internal(struct MHD_Connection *connection, return MHD_queue_response(connection, code, response); } -int mhd_respond(struct MHD_Connection *connection, - enum MHD_RequestTerminationCode code, - const char *message) { - - const char *fmt; - - fmt = strjoina(message, "\n"); - - return mhd_respond_internal(connection, code, - fmt, strlen(message) + 1, - MHD_RESPMEM_PERSISTENT); -} - int mhd_respond_oom(struct MHD_Connection *connection) { - return mhd_respond(connection, MHD_HTTP_SERVICE_UNAVAILABLE, "Out of memory."); + return mhd_respond(connection, MHD_HTTP_SERVICE_UNAVAILABLE, "Out of memory."); } -int mhd_respondf(struct MHD_Connection *connection, - int error, - enum MHD_RequestTerminationCode code, - const char *format, ...) { +int mhd_respondf_internal( + struct MHD_Connection *connection, + int error, + enum MHD_RequestTerminationCode code, + const char *format, ...) { - const char *fmt; char *m; int r; va_list ap; @@ -76,11 +65,8 @@ int mhd_respondf(struct MHD_Connection *connection, if (error < 0) error = -error; errno = -error; - fmt = strjoina(format, "\n"); va_start(ap, format); - DISABLE_WARNING_FORMAT_NONLITERAL; - r = vasprintf(&m, fmt, ap); - REENABLE_WARNING; + r = vasprintf(&m, format, ap); va_end(ap); if (r < 0) diff --git a/src/journal-remote/microhttpd-util.h b/src/journal-remote/microhttpd-util.h index 140e7f6223..309c39aab0 100644 --- a/src/journal-remote/microhttpd-util.h +++ b/src/journal-remote/microhttpd-util.h @@ -62,17 +62,34 @@ void microhttpd_logger(void *arg, const char *fmt, va_list ap) _printf_(2, 0); /* respond_oom() must be usable with return, hence this form. */ #define respond_oom(connection) log_oom(), mhd_respond_oom(connection) -int mhd_respondf(struct MHD_Connection *connection, - int error, - enum MHD_RequestTerminationCode code, - const char *format, ...) _printf_(4,5); - -int mhd_respond(struct MHD_Connection *connection, +int mhd_respond_internal( + struct MHD_Connection *connection, enum MHD_RequestTerminationCode code, - const char *message); + const char *buffer, + size_t size, + enum MHD_ResponseMemoryMode mode); + +#define mhd_respond(connection, code, message) \ + mhd_respond_internal( \ + connection, code, \ + message "\n", \ + strlen(message) + 1, \ + MHD_RESPMEM_PERSISTENT) int mhd_respond_oom(struct MHD_Connection *connection); +int mhd_respondf_internal( + struct MHD_Connection *connection, + int error, + enum MHD_RequestTerminationCode code, + const char *format, ...) _printf_(4,5); + +#define mhd_respondf(connection, error, code, format, ...) \ + mhd_respondf_internal( \ + connection, error, code, \ + format "\n", \ + ##__VA_ARGS__) + int check_permissions(struct MHD_Connection *connection, int *code, char **hostname); /* Set gnutls internal logging function to a callback which uses our diff --git a/test/units/testsuite-04.journal-gatewayd.sh b/test/units/testsuite-04.journal-gatewayd.sh index 1b55bd39db..fbdb315826 100755 --- a/test/units/testsuite-04.journal-gatewayd.sh +++ b/test/units/testsuite-04.journal-gatewayd.sh @@ -142,3 +142,37 @@ curl -Lfsk https://localhost:19531/machine | jq curl -Lfsk https://localhost:19531/fields/_TRANSPORT kill "$GATEWAYD_PID" + +# Test a couple of error scenarios +GATEWAYD_FILE="$(mktemp /tmp/test-gatewayd-XXX.journal)" + +/usr/lib/systemd/systemd-journal-remote --output="$GATEWAYD_FILE" --getter="journalctl -n5 -o export" +systemd-run --unit="test-gatewayd.service" --socket-property="ListenStream=19531" \ + /usr/lib/systemd/systemd-journal-gatewayd --file="$GATEWAYD_FILE" + +# Call an unsupported endpoint together with some garbage data - gatewayd should not send garbage in return +# See: https://github.com/systemd/systemd/issues/9858 +OUT="$(mktemp)" +for _ in {0..4}; do + curl --fail-with-body -d "plese process this🐱 $RANDOM" -L http://localhost:19531/upload | tee "$OUT" + (! grep '[^[:print:]]' "$OUT") +done +curl --fail-with-body --upload-file "$GATEWAYD_FILE" -L http://localhost:19531/upload | tee "$OUT" +(! grep '[^[:print:]]' "$OUT") +rm -rf "$OUT" + +curl -Lfs http://localhost:19531/browse | grep -qF "Journal" +# Nuke the file behind the /browse endpoint +mv /usr/share/systemd/gatewayd/browse.html /usr/share/systemd/gatewayd/browse.html.bak +(! curl --fail-with-body -L http://localhost:19531/browse) +mv /usr/share/systemd/gatewayd/browse.html.bak /usr/share/systemd/gatewayd/browse.html +curl -Lfs http://localhost:19531/browse | grep -qF "Journal" + +# Nuke the journal file +mv "$GATEWAYD_FILE" "$GATEWAYD_FILE.bak" +(! curl --fail-with-body -L http://localhost:19531/fields/_PID) +mv "$GATEWAYD_FILE.bak" "$GATEWAYD_FILE" +curl -Lfs http://localhost:19531/fields/_PID + +systemctl stop test-gatewayd.{socket,service} +rm -f "$GATEWAYD_FILE"