fix(win): force binary stdio so CGI header framing survives MinGW text mode (v1.1.3)
The shipped windows-x86_64 artifact (1.1.2) was proven live on a real Windows NTFS node (.216, 2026-11-12): the MinGW/MSVC CRT starts stdout in TEXT mode, which rewrites every \n into \r\n, so the CGI header block terminator "\r\n\r\n" left the process as "\r\r\n\r\r\n". scorpiox-server could not find the header/body separator and served the reply mangled: GET /plugin_hello returned 200 with Content-Type: text/html and a truncated body whose first line was the plugin's own header, and curl reported 'Header without colon'. JSON modes survived only because their bodies happen to contain no bare LF. Add the same __attribute__((constructor)) binary-stdio shim the scorpiox-bot-api handlers use (sx_shell.h), bump to 1.1.3, and add four CGI-framing regression checks that fail loudly on any CR CR LF in the framing of html/embed/json/echo output.
AuthorPico <picobot@scorpioplayer.com>
Date2026-11-12 10:56:50
Commitc7e119c8
Parents42a83c2e
Changed files
release-dist.sh+1−1■■
src/plugin_hello.c+14−1■■■■■
tests/run.sh+26−2■■■■■
3 files+41−4
Diff
release-dist.sh+1−1■■
index a6308de..04cef30 100755
--- a/release-dist.sh
+++ b/release-dist.sh
@@ -28,7 +28,7 @@ set -euo pipefail
2828
2929 SLUG="hello"
3030 ID="plugin_hello"
31-VERSION="1.1.2"
31+VERSION="1.1.3"
3232 DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins"
3333
3434 ROOT="$(cd "$(dirname "$0")" && pwd)"
src/plugin_hello.c+14−1■■■■■
index 23f26e5..5579d61 100644
--- a/src/plugin_hello.c
+++ b/src/plugin_hello.c
@@ -27,6 +27,19 @@
2727 #if defined(_WIN32)
2828 # define WIN32_LEAN_AND_MEAN
2929 # include <windows.h>
30+# include <io.h>
31+# include <fcntl.h>
32+/* MinGW/MSVC CRT starts stdout in TEXT mode, which rewrites every \n we emit
33+ * into \r\n. A CGI header block must end with exactly CRLF CRLF; text mode
34+ * turns "\r\n\r\n" into "\r\r\n\r\r\n", scorpiox-server cannot find the
35+ * header/body separator, and the entire reply (headers included) is served as
36+ * a body. Force binary stdio before main() runs — same constructor the
37+ * scorpiox-bot-api handlers use (sx_shell.h). */
38+__attribute__((constructor))
39+static void sx_plugin_binary_stdio(void) {
40+ _setmode(_fileno(stdout), _O_BINARY);
41+ _setmode(_fileno(stdin), _O_BINARY);
42+}
3043 #else
3144 # include <unistd.h>
3245 # include <sys/utsname.h>
@@ -36,7 +49,7 @@
3649
3750 #define PLUGIN_SLUG "hello"
3851 #define PLUGIN_ID "plugin_hello"
39-#define PLUGIN_VER "1.1.2"
52+#define PLUGIN_VER "1.1.3"
4053
4154 /* ------------------------------------------------------------------ */
4255 /* Query string helpers */
tests/run.sh+26−2■■■■■
index 92b10dd..e70fb50 100755
--- a/tests/run.sh
+++ b/tests/run.sh
@@ -62,7 +62,31 @@ DESC=$("$BIN" --meta | sed 's/.*"desc":"\([^"]*\)".*/\1/')
6262 [ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})"
6363 NAME=$("$BIN" --meta | sed 's/.*"name":"\([^"]*\)".*/\1/')
6464 [ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces"
65-check "--version prints version" '1.1.2' "$("$BIN" --version)"
65+check "--version prints version" '1.1.3' "$("$BIN" --version)"
66+
67+# ------------------------------------------------- CGI framing (Windows trap)
68+# MinGW/MSVC CRT stdout defaults to TEXT mode, which rewrites every \n into
69+# \r\n. A CGI header block must end with exactly CRLF CRLF; text mode turns it
70+# into CR CR LF CR CR LF, scorpiox-server cannot find the header/body separator,
71+# and the reply comes back mangled (headers inside the body, wrong Content-Type,
72+# truncated body, curl "Header without colon"). Live-confirmed on a real Windows
73+# NTFS node (2026-11-12) with the shipped 1.1.2 windows-x86_64 artifact.
74+section "CGI header framing (no CR CR LF)"
75+FRAMETMP=$(mktemp)
76+cgi_hex() { od -An -tx1 "$1" | tr -s ' \t\n' ' '; }
77+cgi_frame_check() { # cgi_frame_check <desc> <query>
78+ BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw QUERY_STRING="$2" "$BIN" > "$FRAMETMP"
79+ H=$(cgi_hex "$FRAMETMP")
80+ case " $H " in *" 0d 0d 0a "*) fail "$1: found CR CR LF (text-mode stdout)"; return;; esac
81+ case " $H " in *" 0d 0a 0d 0a "*) pass "$1: exact CRLF CRLF header separator";;
82+ *) fail "$1: no CRLF CRLF separator found";;
83+ esac
84+}
85+cgi_frame_check "html mode framing" ""
86+cgi_frame_check "embed mode framing" "embed=1"
87+cgi_frame_check "json mode framing" "action=info"
88+cgi_frame_check "echo action framing" "action=echo&msg=hi"
89+rm -f "$FRAMETMP"
6690
6791 # ---------------------------------------------------------------- json mode
6892 section "node auth"
@@ -93,7 +117,7 @@ BODY=$(printf '%s' "$INFO" | sed '1,/^\r$/d')
93117 check "info ok:true" '"ok":true' "$BODY"
94118 check "info plugin id" '"plugin":"plugin_hello"' "$BODY"
95119 check "info slug" '"slug":"hello"' "$BODY"
96-check "info version" '"version":"1.1.2"' "$BODY"
120+check "info version" '"version":"1.1.3"' "$BODY"
97121 check "info hostname field" '"hostname"' "$BODY"
98122 check "info os field" '"os"' "$BODY"
99123 check "info arch field" '"arch"' "$BODY"
100124
release-dist.sh+1−1■■
index a6308de..04cef30 100755index a6308de..04cef30 100755
--- a/release-dist.sh--- a/release-dist.sh
+++ b/release-dist.sh+++ b/release-dist.sh
@@ -28,7 +28,7 @@ set -euo pipefail@@ -28,7 +28,7 @@ set -euo pipefail
28 28
29 SLUG="hello"29 SLUG="hello"
30 ID="plugin_hello"30 ID="plugin_hello"
31-VERSION="1.1.2"31+VERSION="1.1.3"
32 DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins"32 DIST_BASE_URL="https://dist.scorpiox.net/bot-plugins"
33 33
34 ROOT="$(cd "$(dirname "$0")" && pwd)"34 ROOT="$(cd "$(dirname "$0")" && pwd)"
src/plugin_hello.c+14−1■■■■■
index 23f26e5..5579d61 100644index 23f26e5..5579d61 100644
--- a/src/plugin_hello.c--- a/src/plugin_hello.c
+++ b/src/plugin_hello.c+++ b/src/plugin_hello.c
@@ -27,6 +27,19 @@@@ -27,6 +27,19 @@
27 #if defined(_WIN32)27 #if defined(_WIN32)
28 # define WIN32_LEAN_AND_MEAN28 # define WIN32_LEAN_AND_MEAN
29 # include <windows.h>29 # include <windows.h>
30+# include <io.h>
31+# include <fcntl.h>
32+/* MinGW/MSVC CRT starts stdout in TEXT mode, which rewrites every \n we emit
33+ * into \r\n. A CGI header block must end with exactly CRLF CRLF; text mode
34+ * turns "\r\n\r\n" into "\r\r\n\r\r\n", scorpiox-server cannot find the
35+ * header/body separator, and the entire reply (headers included) is served as
36+ * a body. Force binary stdio before main() runs — same constructor the
37+ * scorpiox-bot-api handlers use (sx_shell.h). */
38+__attribute__((constructor))
39+static void sx_plugin_binary_stdio(void) {
40+ _setmode(_fileno(stdout), _O_BINARY);
41+ _setmode(_fileno(stdin), _O_BINARY);
42+}
30 #else43 #else
31 # include <unistd.h>44 # include <unistd.h>
32 # include <sys/utsname.h>45 # include <sys/utsname.h>
@@ -36,7 +49,7 @@@@ -36,7 +49,7 @@
36 49
37 #define PLUGIN_SLUG "hello"50 #define PLUGIN_SLUG "hello"
38 #define PLUGIN_ID "plugin_hello"51 #define PLUGIN_ID "plugin_hello"
39-#define PLUGIN_VER "1.1.2"52+#define PLUGIN_VER "1.1.3"
40 53
41 /* ------------------------------------------------------------------ */54 /* ------------------------------------------------------------------ */
42 /* Query string helpers */55 /* Query string helpers */
tests/run.sh+26−2■■■■■
index 92b10dd..e70fb50 100755index 92b10dd..e70fb50 100755
--- a/tests/run.sh--- a/tests/run.sh
+++ b/tests/run.sh+++ b/tests/run.sh
@@ -62,7 +62,31 @@ DESC=$("$BIN" --meta | sed 's/.*"desc":"\([^"]*\)".*/\1/')@@ -62,7 +62,31 @@ DESC=$("$BIN" --meta | sed 's/.*"desc":"\([^"]*\)".*/\1/')
62 [ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})"62 [ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})"
63 NAME=$("$BIN" --meta | sed 's/.*"name":"\([^"]*\)".*/\1/')63 NAME=$("$BIN" --meta | sed 's/.*"name":"\([^"]*\)".*/\1/')
64 [ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces"64 [ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces"
65-check "--version prints version" '1.1.2' "$("$BIN" --version)"65+check "--version prints version" '1.1.3' "$("$BIN" --version)"
66+
67+# ------------------------------------------------- CGI framing (Windows trap)
68+# MinGW/MSVC CRT stdout defaults to TEXT mode, which rewrites every \n into
69+# \r\n. A CGI header block must end with exactly CRLF CRLF; text mode turns it
70+# into CR CR LF CR CR LF, scorpiox-server cannot find the header/body separator,
71+# and the reply comes back mangled (headers inside the body, wrong Content-Type,
72+# truncated body, curl "Header without colon"). Live-confirmed on a real Windows
73+# NTFS node (2026-11-12) with the shipped 1.1.2 windows-x86_64 artifact.
74+section "CGI header framing (no CR CR LF)"
75+FRAMETMP=$(mktemp)
76+cgi_hex() { od -An -tx1 "$1" | tr -s ' \t\n' ' '; }
77+cgi_frame_check() { # cgi_frame_check <desc> <query>
78+ BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw QUERY_STRING="$2" "$BIN" > "$FRAMETMP"
79+ H=$(cgi_hex "$FRAMETMP")
80+ case " $H " in *" 0d 0d 0a "*) fail "$1: found CR CR LF (text-mode stdout)"; return;; esac
81+ case " $H " in *" 0d 0a 0d 0a "*) pass "$1: exact CRLF CRLF header separator";;
82+ *) fail "$1: no CRLF CRLF separator found";;
83+ esac
84+}
85+cgi_frame_check "html mode framing" ""
86+cgi_frame_check "embed mode framing" "embed=1"
87+cgi_frame_check "json mode framing" "action=info"
88+cgi_frame_check "echo action framing" "action=echo&msg=hi"
89+rm -f "$FRAMETMP"
66 90
67 # ---------------------------------------------------------------- json mode91 # ---------------------------------------------------------------- json mode
68 section "node auth"92 section "node auth"
@@ -93,7 +117,7 @@ BODY=$(printf '%s' "$INFO" | sed '1,/^\r$/d')@@ -93,7 +117,7 @@ BODY=$(printf '%s' "$INFO" | sed '1,/^\r$/d')
93 check "info ok:true" '"ok":true' "$BODY"117 check "info ok:true" '"ok":true' "$BODY"
94 check "info plugin id" '"plugin":"plugin_hello"' "$BODY"118 check "info plugin id" '"plugin":"plugin_hello"' "$BODY"
95 check "info slug" '"slug":"hello"' "$BODY"119 check "info slug" '"slug":"hello"' "$BODY"
96-check "info version" '"version":"1.1.2"' "$BODY"120+check "info version" '"version":"1.1.3"' "$BODY"
97 check "info hostname field" '"hostname"' "$BODY"121 check "info hostname field" '"hostname"' "$BODY"
98 check "info os field" '"os"' "$BODY"122 check "info os field" '"os"' "$BODY"
99 check "info arch field" '"arch"' "$BODY"123 check "info arch field" '"arch"' "$BODY"
100124