| index 4369034..89d136c 100644 |

| index 2fd836d..9c26d0e 100644 |

| index 481e1b6..d19309b 100644 |

| index 2bfb394..2579b05 100644 |

| index 3a66a6d..b938936 100644 |

| index 73d3ea8..6bddbeb 100644 |

| index d9764e6..e397699 100644 | ||
| --- a/plugin.json | ||
| +++ b/plugin.json | ||
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | "title": "ntfy Push & Pub/Sub Channel Connector", |
| 7 | 7 | "summary": "Zero-dependency ntfy connector with two faces: an MCP tool (ntfy_send) so the agent can push alerts and report summaries to your phone, and a supervised channel daemon that subscribes to a topic's NDJSON stream, holds every unknown sender in a default-deny pairing queue, forwards only approved senders' messages to a SCORPIOX BOT session through POST /inbox, and publishes the agent's newest reply back to the publish topic. Setup, pairing approval and a live daemon log live behind node authentication; TLS verification is fail-closed against a bundled root store with an SPKI pin option for self-hosted relays.", |
| 8 | 8 | "category": "network", |
| 9 | - "version": "1.0.1", | |
| 9 | + "version": "1.0.2", | |
| 10 | 10 | "license": "MIT", |
| 11 | 11 | "kind": "hybrid", |
| 12 | 12 | "mcp": true, |
| index cdddcd5..e312a11 100644 | ||
| --- a/src/plugin_ntfy.c | ||
| +++ b/src/plugin_ntfy.c | ||
| @@ -100,7 +100,7 @@ | ||
| 100 | 100 | |
| 101 | 101 | #define PLUGIN_SLUG "ntfy" |
| 102 | 102 | #define PLUGIN_ID "plugin_ntfy" |
| 103 | -#define PLUGIN_VER "1.0.1" | |
| 103 | +#define PLUGIN_VER "1.0.2" | |
| 104 | 104 | #define SELF_SOURCE_ID "scorpiox-bot-plugin-ntfy" |
| 105 | 105 | |
| 106 | 106 | /* The tag every notification THIS plugin publishes carries. A second, |
| index 8513327..1fa2ad3 100644 | ||
| --- a/src/sx_http.c | ||
| +++ b/src/sx_http.c | ||
| @@ -532,16 +532,36 @@ int sx_conn_open(sx_conn *c, const sx_url *url, const char *bearer, | ||
| 532 | 532 | { |
| 533 | 533 | long long deadline = sx_now_ms() + (timeout_ms > 0 ? timeout_ms : 10000); |
| 534 | 534 | int r = 0; |
| 535 | + int hs_want = 2; /* send the hello first: a fresh socket is writable */ | |
| 535 | 536 | for (;;) { |
| 536 | - if (!mbedtls_ssl_check_pending(c->ssl) && | |
| 537 | - !sx_wait_readable(c->fd, deadline)) { | |
| 538 | - set_err(err, errsz, "tls handshake timed out"); | |
| 539 | - sx_conn_close(c); | |
| 540 | - return -1; | |
| 537 | + /* mbedTLS is driven by WANT_READ/WANT_WRITE, not by a single | |
| 538 | + * read-set. A handshake step that only has to *write* (the | |
| 539 | + * ClientHello, the Finished) leaves the socket non-readable; if we | |
| 540 | + * waited only on the read set, mbedtls_ssl_handshake() would return | |
| 541 | + * WANT_WRITE and we would then block in select() forever on a | |
| 542 | + * server that correctly says nothing until it has our hello. That | |
| 543 | + * is the bug: the ClientHello never left the process (tcpdump on | |
| 544 | + * the real peer sees SYN, then a FIN 15 s later) and every ntfy | |
| 545 | + * call died as "tls handshake timed out". */ | |
| 546 | + if (mbedtls_ssl_check_pending(c->ssl)) { | |
| 547 | + /* nothing to wait for, mbedtls_ssl_read() has buffered data */ | |
| 548 | + } else if (hs_want == 2) { | |
| 549 | + if (!sx_wait_writable(c->fd, deadline)) { | |
| 550 | + set_err(err, errsz, "tls handshake timed out (write)"); | |
| 551 | + sx_conn_close(c); | |
| 552 | + return -1; | |
| 553 | + } | |
| 554 | + } else if (hs_want == 1) { | |
| 555 | + if (!sx_wait_readable(c->fd, deadline)) { | |
| 556 | + set_err(err, errsz, "tls handshake timed out"); | |
| 557 | + sx_conn_close(c); | |
| 558 | + return -1; | |
| 559 | + } | |
| 541 | 560 | } |
| 542 | 561 | r = mbedtls_ssl_handshake(c->ssl); |
| 543 | - if (r != MBEDTLS_ERR_SSL_WANT_READ && r != MBEDTLS_ERR_SSL_WANT_WRITE) | |
| 544 | - break; | |
| 562 | + if (r == MBEDTLS_ERR_SSL_WANT_WRITE) hs_want = 2; | |
| 563 | + else if (r == MBEDTLS_ERR_SSL_WANT_READ) hs_want = 1; | |
| 564 | + else break; | |
| 545 | 565 | } |
| 546 | 566 | if (r != 0) { |
| 547 | 567 | char e[128]; |
| index a1b3d53..79e7ded 100644 | ||
| --- a/tests/mockd.py | ||
| +++ b/tests/mockd.py | ||
| @@ -26,6 +26,8 @@ import re | ||
| 26 | 26 | import shutil |
| 27 | 27 | import socket |
| 28 | 28 | import socketserver |
| 29 | +import ssl | |
| 30 | +import subprocess | |
| 29 | 31 | import threading |
| 30 | 32 | import time |
| 31 | 33 | import uuid |
| @@ -401,12 +403,50 @@ class Handler(BaseHTTPRequestHandler): | ||
| 401 | 403 | return self._json(200, out) |
| 402 | 404 | |
| 403 | 405 | return self._json(404, {"code": 404, "error": "not found"}) |
| 404 | - | |
| 405 | - | |
| 406 | 406 | class Server(ThreadingHTTPServer): |
| 407 | 407 | daemon_threads = True |
| 408 | 408 | allow_reuse_address = True |
| 409 | 409 | |
| 410 | + def handle_error(self, request, client_address): | |
| 411 | + # A client that aborts the handshake (which is exactly what a | |
| 412 | + # failing-closed TLS client does when it refuses our self-signed | |
| 413 | + # leaf) lands here as an SSLError. It is a result, not a crash. | |
| 414 | + return | |
| 415 | + | |
| 416 | + | |
| 417 | +def serve_tls(port, backend_cert_dir, bind="127.0.0.1"): | |
| 418 | + """Same handler over TLS. | |
| 419 | + | |
| 420 | + The leaf is self-signed for 127.0.0.1, so the plugin's bundled root store | |
| 421 | + never trusts it: it can only be accepted through an explicit SPKI pin, | |
| 422 | + and only rejected (never silently downgraded) without one. That is the | |
| 423 | + property the pinning tests assert. ThreadingHTTPServer wraps the listener, | |
| 424 | + so every accepted connection gets its own SSL socket and its own thread -- | |
| 425 | + the long-lived NDJSON stream never shares a record buffer with a handshake. | |
| 426 | + | |
| 427 | + `bind` lets the suite stand a second front on another loopback address with | |
| 428 | + the SAME certificate. Reaching that leaf at an address outside its only IP | |
| 429 | + SAN is how the hostname-mismatch rule gets tested without DNS: this | |
| 430 | + container has no /etc/hosts, so a name would only exercise the resolver | |
| 431 | + error path and prove nothing about verification. | |
| 432 | + """ | |
| 433 | + os.makedirs(backend_cert_dir, exist_ok=True) | |
| 434 | + key = os.path.join(backend_cert_dir, "mock-key.pem") | |
| 435 | + crt = os.path.join(backend_cert_dir, "mock-cert.pem") | |
| 436 | + if not (os.path.exists(key) and os.path.exists(crt)): | |
| 437 | + subprocess.run( | |
| 438 | + ["openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes", | |
| 439 | + "-keyout", key, "-out", crt, "-days", "2", | |
| 440 | + "-subj", "/CN=127.0.0.1/O=SCORPIOX BOT test", | |
| 441 | + "-addext", "subjectAltName=IP:127.0.0.1"], | |
| 442 | + check=True, capture_output=True) | |
| 443 | + ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) | |
| 444 | + ctx.load_cert_chain(crt, key) | |
| 445 | + srv = Server((bind, port), Handler) | |
| 446 | + srv.socket = ctx.wrap_socket(srv.socket, server_side=True) | |
| 447 | + threading.Thread(target=srv.serve_forever, kwargs={"poll_interval": 0.2}, | |
| 448 | + daemon=True).start() | |
| 449 | + | |
| 410 | 450 | |
| 411 | 451 | def main(): |
| 412 | 452 | import argparse |
| @@ -414,12 +454,26 @@ def main(): | ||
| 414 | 454 | ap = argparse.ArgumentParser() |
| 415 | 455 | ap.add_argument("--port", type=int, default=18199) |
| 416 | 456 | ap.add_argument("--root", default="/tmp/sx-ntfy-mock") |
| 457 | + ap.add_argument("--tls-port", type=int, default=0, | |
| 458 | + help="also serve HTTPS on this port with a self-signed leaf") | |
| 459 | + ap.add_argument("--tls-mismatch-port", type=int, default=0, | |
| 460 | + help="a second HTTPS front on 127.0.0.2 with the SAME " | |
| 461 | + "127.0.0.1-only leaf, i.e. a real hostname mismatch") | |
| 417 | 462 | args = ap.parse_args() |
| 418 | 463 | |
| 419 | 464 | os.makedirs(args.root, exist_ok=True) |
| 420 | 465 | STATE = State(args.root) |
| 466 | + if args.tls_port: | |
| 467 | + serve_tls(args.tls_port, os.path.join(args.root, "tls")) | |
| 468 | + if args.tls_mismatch_port: | |
| 469 | + serve_tls(args.tls_mismatch_port, os.path.join(args.root, "tls"), | |
| 470 | + bind="127.0.0.2") | |
| 421 | 471 | srv = Server(("127.0.0.1", args.port), Handler) |
| 422 | - print("mockd listening on 127.0.0.1:%d root=%s" % (args.port, args.root), | |
| 472 | + print("mockd listening on 127.0.0.1:%d root=%s%s%s" % | |
| 473 | + (args.port, args.root, | |
| 474 | + " tls=127.0.0.1:%d" % args.tls_port if args.tls_port else "", | |
| 475 | + " tls-mismatch=127.0.0.2:%d" % args.tls_mismatch_port | |
| 476 | + if args.tls_mismatch_port else ""), | |
| 423 | 477 | flush=True) |
| 424 | 478 | try: |
| 425 | 479 | srv.serve_forever() |
| index 3349313..c7e47a7 100755 | ||
| --- a/tests/run.sh | ||
| +++ b/tests/run.sh | ||
| @@ -471,6 +471,104 @@ import sys,re | ||
| 471 | 471 | s=open('src/ui.html',encoding='utf-8').read() |
| 472 | 472 | sys.exit(1 if re.search('[\U0001F000-\U0001FAFF\u2600-\u27BF]',s) else 0)"; then pass "no unicode emoji in ui.html"; else fail "unicode emoji in ui.html"; fi |
| 473 | 473 | |
| 474 | +# ---------------------------------------------------------------- transport security | |
| 475 | +section "transport security - HTTPS, the bundled root store, SPKI pinning" | |
| 476 | +# sx_http.c is the highest-stakes code in the binary: it is the only thing | |
| 477 | +# standing between somebody else's ntfy topic and this node's /inbox. The | |
| 478 | +# offline mock can speak TLS as well, with a leaf the bundled root store will | |
| 479 | +# NEVER trust -- so the only way HTTPS can succeed at all here is an explicit | |
| 480 | +# SPKI pin. That makes both halves of the promise testable with zero internet: | |
| 481 | +# "it fails closed" and "a private CA works when pinned". | |
| 482 | +TLSPORT=$((PORT + 1)) | |
| 483 | +TLSPLAIN=$((PORT + 2)) | |
| 484 | +TLSMMSMPORT=$((PORT + 3)) | |
| 485 | +TLSPID="" | |
| 486 | +tls_cleanup() { if [ -n "$TLSPID" ] && kill -0 "$TLSPID" 2>/dev/null; then kill "$TLSPID" 2>/dev/null; wait "$TLSPID" 2>/dev/null; fi; return 0; } | |
| 487 | +trap 'tls_cleanup' EXIT | |
| 488 | +python3 tests/mockd.py --port "$TLSPLAIN" --tls-port "$TLSPORT" \ | |
| 489 | + --tls-mismatch-port "$TLSMMSMPORT" --root "$WORK/mocktls" \ | |
| 490 | + > "$WORK/mockd.tls.out" 2>&1 & | |
| 491 | +TLSPID=$! | |
| 492 | +i=0 | |
| 493 | +while [ "$i" -lt 60 ]; do | |
| 494 | + grep -q "tls-mismatch=127.0.0.2:$TLSMMSMPORT" "$WORK/mockd.tls.out" 2>/dev/null && break | |
| 495 | + i=$((i + 1)); sleep 0.1 | |
| 496 | +done | |
| 497 | +if grep -q "tls-mismatch=" "$WORK/mockd.tls.out" 2>/dev/null; then | |
| 498 | + pass "mock ntfy also speaks TLS on 127.0.0.1:$TLSPORT (+ mismatch front :$TLSMMSMPORT)" | |
| 499 | +else | |
| 500 | + fail "mock TLS front never came up" | |
| 501 | + sed 's/^/ /' "$WORK/mockd.tls.out" | head -5 | |
| 502 | +fi | |
| 503 | +# Read the TLS mock's own capture: a publish that silently fell back to plain | |
| 504 | +# http would land on $PORT instead, which is what the downgrade check sees. | |
| 505 | +tls_published_has() { | |
| 506 | + curl -s -m 3 "http://127.0.0.1:$TLSPLAIN/__publishes" 2>/dev/null | grep -F -q -- "$1" | |
| 507 | +} | |
| 508 | +pin_of() { | |
| 509 | + openssl x509 -in "$1" -pubkey -noout 2>/dev/null \ | |
| 510 | + | openssl pkey -pubin -outform der 2>/dev/null \ | |
| 511 | + | openssl dgst -sha256 -hex 2>/dev/null | awk '{print $NF}' | |
| 512 | +} | |
| 513 | +PIN_REAL=$(pin_of "$WORK/mocktls/tls/mock-cert.pem") | |
| 514 | +if [ -n "$PIN_REAL" ] && [ "${#PIN_REAL}" -eq 64 ]; then | |
| 515 | + pass "computed the leaf SPKI pin with openssl" | |
| 516 | +else | |
| 517 | + fail "could not compute the leaf SPKI pin" | |
| 518 | + PIN_REAL="0000000000000000000000000000000000000000000000000000000000000000" | |
| 519 | +fi | |
| 520 | + | |
| 521 | +HTTPS_URL="https://127.0.0.1:$TLSPORT" | |
| 522 | +post "action=save" "{\"server_url\":\"$HTTPS_URL\",\"topic\":\"stin\",\"publish_topic\":\"stout\",\"allowed_pins\":\"\"}" >/dev/null | |
| 523 | + | |
| 524 | +# 1) An untrusted leaf and no pin must be REFUSED -- not accepted, and above | |
| 525 | +# all not quietly retried over plain http. | |
| 526 | +R=$(post "action=send" '{"message":"should never publish unauthenticated"}') | |
| 527 | +json_ok "https to an untrusted certificate is refused" "$R" "d['ok']==False" | |
| 528 | +check "the refusal names the certificate" 'certificate' "$R" | |
| 529 | +check_not "it did not reach the TLS mock" 'should never publish unauthenticated' \ | |
| 530 | + "$(curl -s -m3 http://127.0.0.1:$TLSPLAIN/__publishes)" | |
| 531 | +check_not "and it did not downgrade to the plain mock" 'should never publish unauthenticated' \ | |
| 532 | + "$(curl -s -m3 http://127.0.0.1:$PORT/__publishes)" | |
| 533 | + | |
| 534 | +# 2) A pin that does not match is still a refusal. | |
| 535 | +post "action=save" "{\"allowed_pins\":\"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\"}" >/dev/null | |
| 536 | +R=$(post "action=send" '{"message":"wrong pin"}') | |
| 537 | +json_ok "a non-matching pin is refused" "$R" "d['ok']==False" | |
| 538 | + | |
| 539 | +# 3) The right pin is accepted -- the path a self-hosted relay on a private CA | |
| 540 | +# takes, so it has to work, and the pin the plugin derives from its own | |
| 541 | +# peer certificate has to agree with the one openssl derives. | |
| 542 | +post "action=save" "{\"allowed_pins\":\"$PIN_REAL\"}" >/dev/null | |
| 543 | +R=$(post "action=send" '{"message":"pinned publish ok","title":"PinTest"}') | |
| 544 | +json_ok "the correct SPKI pin is accepted" "$R" "d['ok']==True" | |
| 545 | +wait_for 5 "the pinned publish reached the TLS mock" tls_published_has 'pinned publish ok' | |
| 546 | + | |
| 547 | +# 4) A pin never becomes a bypass for hostname checking. The same leaf, | |
| 548 | +# reached on 127.0.0.2 (outside its only IP SAN), must be refused even with | |
| 549 | +# its own correct pin. 127.0.0.2 rather than a hostname because this | |
| 550 | +# container resolves nothing -- a name only exercises the resolver error. | |
| 551 | +post "action=save" "{\"server_url\":\"https://127.0.0.2:$TLSMMSMPORT\",\"allowed_pins\":\"$PIN_REAL\"}" >/dev/null | |
| 552 | +R=$(post "action=send" '{"message":"hostname mismatch"}') | |
| 553 | +json_ok "a pin cannot override a hostname mismatch" "$R" "d['ok']==False" | |
| 554 | +check "and says so" 'hostname' "$R" | |
| 555 | +post "action=save" "{\"server_url\":\"$HTTPS_URL\"}" >/dev/null | |
| 556 | + | |
| 557 | +# 5) The only no-verify mode is the explicit, loud environment opt-in. | |
| 558 | +R=$(post "action=send" '{"message":"insecure mode opt-in"}' NTFY_TLS_INSECURE=1) | |
| 559 | +json_ok "NTFY_TLS_INSECURE=1 is the documented explicit opt-in" "$R" "d['ok']==True" | |
| 560 | + | |
| 561 | +# 6) Plain http to a non-loopback server is refused: a bearer token would ride | |
| 562 | +# in the clear. save reports ok (forms fill in stages) plus the verdict -- | |
| 563 | +# the verdict is the refusal. | |
| 564 | +R=$(post "action=save" '{"server_url":"http://ntfy.example.invalid","topic":"tin"}') | |
| 565 | +json_ok "plain http to a non-loopback server is refused" "$R" \ | |
| 566 | + "d['valid']==False and 'plain http' in d.get('validation','')" | |
| 567 | + | |
| 568 | +# put the transport back to the offline plain mock for anything that follows | |
| 569 | +post "action=save" "{\"server_url\":\"http://127.0.0.1:$PORT\",\"topic\":\"tin\",\"publish_topic\":\"tout\",\"allowed_pins\":\"\"}" >/dev/null | |
| 570 | +tls_cleanup; TLSPID="" | |
| 571 | + | |
| 474 | 572 | # ---------------------------------------------------------------- hardening |
| 475 | 573 | section "builds - ASan + fortify + PE header" |
| 476 | 574 | if [ ! -x tests/asan/plugin_ntfy_asan ]; then make asan >"$WORK/asan.build" 2>&1 || true; fi |
| 477 | 575 |
| index 4369034..89d136c 100644 | index 4369034..89d136c 100644 |

| index 2fd836d..9c26d0e 100644 | index 2fd836d..9c26d0e 100644 |

| index 481e1b6..d19309b 100644 | index 481e1b6..d19309b 100644 |

| index 2bfb394..2579b05 100644 | index 2bfb394..2579b05 100644 |

| index 3a66a6d..b938936 100644 | index 3a66a6d..b938936 100644 |

| index 73d3ea8..6bddbeb 100644 | index 73d3ea8..6bddbeb 100644 |

| index d9764e6..e397699 100644 | index d9764e6..e397699 100644 | ||
| --- a/plugin.json | --- a/plugin.json | ||
| +++ b/plugin.json | +++ b/plugin.json | ||
| @@ -6,7 +6,7 @@ | @@ -6,7 +6,7 @@ | ||
| 6 | "title": "ntfy Push & Pub/Sub Channel Connector", | 6 | "title": "ntfy Push & Pub/Sub Channel Connector", |
| 7 | "summary": "Zero-dependency ntfy connector with two faces: an MCP tool (ntfy_send) so the agent can push alerts and report summaries to your phone, and a supervised channel daemon that subscribes to a topic's NDJSON stream, holds every unknown sender in a default-deny pairing queue, forwards only approved senders' messages to a SCORPIOX BOT session through POST /inbox, and publishes the agent's newest reply back to the publish topic. Setup, pairing approval and a live daemon log live behind node authentication; TLS verification is fail-closed against a bundled root store with an SPKI pin option for self-hosted relays.", | 7 | "summary": "Zero-dependency ntfy connector with two faces: an MCP tool (ntfy_send) so the agent can push alerts and report summaries to your phone, and a supervised channel daemon that subscribes to a topic's NDJSON stream, holds every unknown sender in a default-deny pairing queue, forwards only approved senders' messages to a SCORPIOX BOT session through POST /inbox, and publishes the agent's newest reply back to the publish topic. Setup, pairing approval and a live daemon log live behind node authentication; TLS verification is fail-closed against a bundled root store with an SPKI pin option for self-hosted relays.", |
| 8 | "category": "network", | 8 | "category": "network", |
| 9 | - "version": "1.0.1", | 9 | + "version": "1.0.2", |
| 10 | "license": "MIT", | 10 | "license": "MIT", |
| 11 | "kind": "hybrid", | 11 | "kind": "hybrid", |
| 12 | "mcp": true, | 12 | "mcp": true, |
| index cdddcd5..e312a11 100644 | index cdddcd5..e312a11 100644 | ||
| --- a/src/plugin_ntfy.c | --- a/src/plugin_ntfy.c | ||
| +++ b/src/plugin_ntfy.c | +++ b/src/plugin_ntfy.c | ||
| @@ -100,7 +100,7 @@ | @@ -100,7 +100,7 @@ | ||
| 100 | 100 | ||
| 101 | #define PLUGIN_SLUG "ntfy" | 101 | #define PLUGIN_SLUG "ntfy" |
| 102 | #define PLUGIN_ID "plugin_ntfy" | 102 | #define PLUGIN_ID "plugin_ntfy" |
| 103 | -#define PLUGIN_VER "1.0.1" | 103 | +#define PLUGIN_VER "1.0.2" |
| 104 | #define SELF_SOURCE_ID "scorpiox-bot-plugin-ntfy" | 104 | #define SELF_SOURCE_ID "scorpiox-bot-plugin-ntfy" |
| 105 | 105 | ||
| 106 | /* The tag every notification THIS plugin publishes carries. A second, | 106 | /* The tag every notification THIS plugin publishes carries. A second, |
| index 8513327..1fa2ad3 100644 | index 8513327..1fa2ad3 100644 | ||
| --- a/src/sx_http.c | --- a/src/sx_http.c | ||
| +++ b/src/sx_http.c | +++ b/src/sx_http.c | ||
| @@ -532,16 +532,36 @@ int sx_conn_open(sx_conn *c, const sx_url *url, const char *bearer, | @@ -532,16 +532,36 @@ int sx_conn_open(sx_conn *c, const sx_url *url, const char *bearer, | ||
| 532 | { | 532 | { |
| 533 | long long deadline = sx_now_ms() + (timeout_ms > 0 ? timeout_ms : 10000); | 533 | long long deadline = sx_now_ms() + (timeout_ms > 0 ? timeout_ms : 10000); |
| 534 | int r = 0; | 534 | int r = 0; |
| 535 | + int hs_want = 2; /* send the hello first: a fresh socket is writable */ | ||
| 535 | for (;;) { | 536 | for (;;) { |
| 536 | - if (!mbedtls_ssl_check_pending(c->ssl) && | 537 | + /* mbedTLS is driven by WANT_READ/WANT_WRITE, not by a single |
| 537 | - !sx_wait_readable(c->fd, deadline)) { | 538 | + * read-set. A handshake step that only has to *write* (the |
| 538 | - set_err(err, errsz, "tls handshake timed out"); | 539 | + * ClientHello, the Finished) leaves the socket non-readable; if we |
| 539 | - sx_conn_close(c); | 540 | + * waited only on the read set, mbedtls_ssl_handshake() would return |
| 540 | - return -1; | 541 | + * WANT_WRITE and we would then block in select() forever on a |
| 542 | + * server that correctly says nothing until it has our hello. That | ||
| 543 | + * is the bug: the ClientHello never left the process (tcpdump on | ||
| 544 | + * the real peer sees SYN, then a FIN 15 s later) and every ntfy | ||
| 545 | + * call died as "tls handshake timed out". */ | ||
| 546 | + if (mbedtls_ssl_check_pending(c->ssl)) { | ||
| 547 | + /* nothing to wait for, mbedtls_ssl_read() has buffered data */ | ||
| 548 | + } else if (hs_want == 2) { | ||
| 549 | + if (!sx_wait_writable(c->fd, deadline)) { | ||
| 550 | + set_err(err, errsz, "tls handshake timed out (write)"); | ||
| 551 | + sx_conn_close(c); | ||
| 552 | + return -1; | ||
| 553 | + } | ||
| 554 | + } else if (hs_want == 1) { | ||
| 555 | + if (!sx_wait_readable(c->fd, deadline)) { | ||
| 556 | + set_err(err, errsz, "tls handshake timed out"); | ||
| 557 | + sx_conn_close(c); | ||
| 558 | + return -1; | ||
| 559 | + } | ||
| 541 | } | 560 | } |
| 542 | r = mbedtls_ssl_handshake(c->ssl); | 561 | r = mbedtls_ssl_handshake(c->ssl); |
| 543 | - if (r != MBEDTLS_ERR_SSL_WANT_READ && r != MBEDTLS_ERR_SSL_WANT_WRITE) | 562 | + if (r == MBEDTLS_ERR_SSL_WANT_WRITE) hs_want = 2; |
| 544 | - break; | 563 | + else if (r == MBEDTLS_ERR_SSL_WANT_READ) hs_want = 1; |
| 564 | + else break; | ||
| 545 | } | 565 | } |
| 546 | if (r != 0) { | 566 | if (r != 0) { |
| 547 | char e[128]; | 567 | char e[128]; |
| index a1b3d53..79e7ded 100644 | index a1b3d53..79e7ded 100644 | ||
| --- a/tests/mockd.py | --- a/tests/mockd.py | ||
| +++ b/tests/mockd.py | +++ b/tests/mockd.py | ||
| @@ -26,6 +26,8 @@ import re | @@ -26,6 +26,8 @@ import re | ||
| 26 | import shutil | 26 | import shutil |
| 27 | import socket | 27 | import socket |
| 28 | import socketserver | 28 | import socketserver |
| 29 | +import ssl | ||
| 30 | +import subprocess | ||
| 29 | import threading | 31 | import threading |
| 30 | import time | 32 | import time |
| 31 | import uuid | 33 | import uuid |
| @@ -401,12 +403,50 @@ class Handler(BaseHTTPRequestHandler): | @@ -401,12 +403,50 @@ class Handler(BaseHTTPRequestHandler): | ||
| 401 | return self._json(200, out) | 403 | return self._json(200, out) |
| 402 | 404 | ||
| 403 | return self._json(404, {"code": 404, "error": "not found"}) | 405 | return self._json(404, {"code": 404, "error": "not found"}) |
| 404 | - | ||
| 405 | - | ||
| 406 | class Server(ThreadingHTTPServer): | 406 | class Server(ThreadingHTTPServer): |
| 407 | daemon_threads = True | 407 | daemon_threads = True |
| 408 | allow_reuse_address = True | 408 | allow_reuse_address = True |
| 409 | 409 | ||
| 410 | + def handle_error(self, request, client_address): | ||
| 411 | + # A client that aborts the handshake (which is exactly what a | ||
| 412 | + # failing-closed TLS client does when it refuses our self-signed | ||
| 413 | + # leaf) lands here as an SSLError. It is a result, not a crash. | ||
| 414 | + return | ||
| 415 | + | ||
| 416 | + | ||
| 417 | +def serve_tls(port, backend_cert_dir, bind="127.0.0.1"): | ||
| 418 | + """Same handler over TLS. | ||
| 419 | + | ||
| 420 | + The leaf is self-signed for 127.0.0.1, so the plugin's bundled root store | ||
| 421 | + never trusts it: it can only be accepted through an explicit SPKI pin, | ||
| 422 | + and only rejected (never silently downgraded) without one. That is the | ||
| 423 | + property the pinning tests assert. ThreadingHTTPServer wraps the listener, | ||
| 424 | + so every accepted connection gets its own SSL socket and its own thread -- | ||
| 425 | + the long-lived NDJSON stream never shares a record buffer with a handshake. | ||
| 426 | + | ||
| 427 | + `bind` lets the suite stand a second front on another loopback address with | ||
| 428 | + the SAME certificate. Reaching that leaf at an address outside its only IP | ||
| 429 | + SAN is how the hostname-mismatch rule gets tested without DNS: this | ||
| 430 | + container has no /etc/hosts, so a name would only exercise the resolver | ||
| 431 | + error path and prove nothing about verification. | ||
| 432 | + """ | ||
| 433 | + os.makedirs(backend_cert_dir, exist_ok=True) | ||
| 434 | + key = os.path.join(backend_cert_dir, "mock-key.pem") | ||
| 435 | + crt = os.path.join(backend_cert_dir, "mock-cert.pem") | ||
| 436 | + if not (os.path.exists(key) and os.path.exists(crt)): | ||
| 437 | + subprocess.run( | ||
| 438 | + ["openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes", | ||
| 439 | + "-keyout", key, "-out", crt, "-days", "2", | ||
| 440 | + "-subj", "/CN=127.0.0.1/O=SCORPIOX BOT test", | ||
| 441 | + "-addext", "subjectAltName=IP:127.0.0.1"], | ||
| 442 | + check=True, capture_output=True) | ||
| 443 | + ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) | ||
| 444 | + ctx.load_cert_chain(crt, key) | ||
| 445 | + srv = Server((bind, port), Handler) | ||
| 446 | + srv.socket = ctx.wrap_socket(srv.socket, server_side=True) | ||
| 447 | + threading.Thread(target=srv.serve_forever, kwargs={"poll_interval": 0.2}, | ||
| 448 | + daemon=True).start() | ||
| 449 | + | ||
| 410 | 450 | ||
| 411 | def main(): | 451 | def main(): |
| 412 | import argparse | 452 | import argparse |
| @@ -414,12 +454,26 @@ def main(): | @@ -414,12 +454,26 @@ def main(): | ||
| 414 | ap = argparse.ArgumentParser() | 454 | ap = argparse.ArgumentParser() |
| 415 | ap.add_argument("--port", type=int, default=18199) | 455 | ap.add_argument("--port", type=int, default=18199) |
| 416 | ap.add_argument("--root", default="/tmp/sx-ntfy-mock") | 456 | ap.add_argument("--root", default="/tmp/sx-ntfy-mock") |
| 457 | + ap.add_argument("--tls-port", type=int, default=0, | ||
| 458 | + help="also serve HTTPS on this port with a self-signed leaf") | ||
| 459 | + ap.add_argument("--tls-mismatch-port", type=int, default=0, | ||
| 460 | + help="a second HTTPS front on 127.0.0.2 with the SAME " | ||
| 461 | + "127.0.0.1-only leaf, i.e. a real hostname mismatch") | ||
| 417 | args = ap.parse_args() | 462 | args = ap.parse_args() |
| 418 | 463 | ||
| 419 | os.makedirs(args.root, exist_ok=True) | 464 | os.makedirs(args.root, exist_ok=True) |
| 420 | STATE = State(args.root) | 465 | STATE = State(args.root) |
| 466 | + if args.tls_port: | ||
| 467 | + serve_tls(args.tls_port, os.path.join(args.root, "tls")) | ||
| 468 | + if args.tls_mismatch_port: | ||
| 469 | + serve_tls(args.tls_mismatch_port, os.path.join(args.root, "tls"), | ||
| 470 | + bind="127.0.0.2") | ||
| 421 | srv = Server(("127.0.0.1", args.port), Handler) | 471 | srv = Server(("127.0.0.1", args.port), Handler) |
| 422 | - print("mockd listening on 127.0.0.1:%d root=%s" % (args.port, args.root), | 472 | + print("mockd listening on 127.0.0.1:%d root=%s%s%s" % |
| 473 | + (args.port, args.root, | ||
| 474 | + " tls=127.0.0.1:%d" % args.tls_port if args.tls_port else "", | ||
| 475 | + " tls-mismatch=127.0.0.2:%d" % args.tls_mismatch_port | ||
| 476 | + if args.tls_mismatch_port else ""), | ||
| 423 | flush=True) | 477 | flush=True) |
| 424 | try: | 478 | try: |
| 425 | srv.serve_forever() | 479 | srv.serve_forever() |
| index 3349313..c7e47a7 100755 | index 3349313..c7e47a7 100755 | ||
| --- a/tests/run.sh | --- a/tests/run.sh | ||
| +++ b/tests/run.sh | +++ b/tests/run.sh | ||
| @@ -471,6 +471,104 @@ import sys,re | @@ -471,6 +471,104 @@ import sys,re | ||
| 471 | s=open('src/ui.html',encoding='utf-8').read() | 471 | s=open('src/ui.html',encoding='utf-8').read() |
| 472 | sys.exit(1 if re.search('[\U0001F000-\U0001FAFF\u2600-\u27BF]',s) else 0)"; then pass "no unicode emoji in ui.html"; else fail "unicode emoji in ui.html"; fi | 472 | sys.exit(1 if re.search('[\U0001F000-\U0001FAFF\u2600-\u27BF]',s) else 0)"; then pass "no unicode emoji in ui.html"; else fail "unicode emoji in ui.html"; fi |
| 473 | 473 | ||
| 474 | +# ---------------------------------------------------------------- transport security | ||
| 475 | +section "transport security - HTTPS, the bundled root store, SPKI pinning" | ||
| 476 | +# sx_http.c is the highest-stakes code in the binary: it is the only thing | ||
| 477 | +# standing between somebody else's ntfy topic and this node's /inbox. The | ||
| 478 | +# offline mock can speak TLS as well, with a leaf the bundled root store will | ||
| 479 | +# NEVER trust -- so the only way HTTPS can succeed at all here is an explicit | ||
| 480 | +# SPKI pin. That makes both halves of the promise testable with zero internet: | ||
| 481 | +# "it fails closed" and "a private CA works when pinned". | ||
| 482 | +TLSPORT=$((PORT + 1)) | ||
| 483 | +TLSPLAIN=$((PORT + 2)) | ||
| 484 | +TLSMMSMPORT=$((PORT + 3)) | ||
| 485 | +TLSPID="" | ||
| 486 | +tls_cleanup() { if [ -n "$TLSPID" ] && kill -0 "$TLSPID" 2>/dev/null; then kill "$TLSPID" 2>/dev/null; wait "$TLSPID" 2>/dev/null; fi; return 0; } | ||
| 487 | +trap 'tls_cleanup' EXIT | ||
| 488 | +python3 tests/mockd.py --port "$TLSPLAIN" --tls-port "$TLSPORT" \ | ||
| 489 | + --tls-mismatch-port "$TLSMMSMPORT" --root "$WORK/mocktls" \ | ||
| 490 | + > "$WORK/mockd.tls.out" 2>&1 & | ||
| 491 | +TLSPID=$! | ||
| 492 | +i=0 | ||
| 493 | +while [ "$i" -lt 60 ]; do | ||
| 494 | + grep -q "tls-mismatch=127.0.0.2:$TLSMMSMPORT" "$WORK/mockd.tls.out" 2>/dev/null && break | ||
| 495 | + i=$((i + 1)); sleep 0.1 | ||
| 496 | +done | ||
| 497 | +if grep -q "tls-mismatch=" "$WORK/mockd.tls.out" 2>/dev/null; then | ||
| 498 | + pass "mock ntfy also speaks TLS on 127.0.0.1:$TLSPORT (+ mismatch front :$TLSMMSMPORT)" | ||
| 499 | +else | ||
| 500 | + fail "mock TLS front never came up" | ||
| 501 | + sed 's/^/ /' "$WORK/mockd.tls.out" | head -5 | ||
| 502 | +fi | ||
| 503 | +# Read the TLS mock's own capture: a publish that silently fell back to plain | ||
| 504 | +# http would land on $PORT instead, which is what the downgrade check sees. | ||
| 505 | +tls_published_has() { | ||
| 506 | + curl -s -m 3 "http://127.0.0.1:$TLSPLAIN/__publishes" 2>/dev/null | grep -F -q -- "$1" | ||
| 507 | +} | ||
| 508 | +pin_of() { | ||
| 509 | + openssl x509 -in "$1" -pubkey -noout 2>/dev/null \ | ||
| 510 | + | openssl pkey -pubin -outform der 2>/dev/null \ | ||
| 511 | + | openssl dgst -sha256 -hex 2>/dev/null | awk '{print $NF}' | ||
| 512 | +} | ||
| 513 | +PIN_REAL=$(pin_of "$WORK/mocktls/tls/mock-cert.pem") | ||
| 514 | +if [ -n "$PIN_REAL" ] && [ "${#PIN_REAL}" -eq 64 ]; then | ||
| 515 | + pass "computed the leaf SPKI pin with openssl" | ||
| 516 | +else | ||
| 517 | + fail "could not compute the leaf SPKI pin" | ||
| 518 | + PIN_REAL="0000000000000000000000000000000000000000000000000000000000000000" | ||
| 519 | +fi | ||
| 520 | + | ||
| 521 | +HTTPS_URL="https://127.0.0.1:$TLSPORT" | ||
| 522 | +post "action=save" "{\"server_url\":\"$HTTPS_URL\",\"topic\":\"stin\",\"publish_topic\":\"stout\",\"allowed_pins\":\"\"}" >/dev/null | ||
| 523 | + | ||
| 524 | +# 1) An untrusted leaf and no pin must be REFUSED -- not accepted, and above | ||
| 525 | +# all not quietly retried over plain http. | ||
| 526 | +R=$(post "action=send" '{"message":"should never publish unauthenticated"}') | ||
| 527 | +json_ok "https to an untrusted certificate is refused" "$R" "d['ok']==False" | ||
| 528 | +check "the refusal names the certificate" 'certificate' "$R" | ||
| 529 | +check_not "it did not reach the TLS mock" 'should never publish unauthenticated' \ | ||
| 530 | + "$(curl -s -m3 http://127.0.0.1:$TLSPLAIN/__publishes)" | ||
| 531 | +check_not "and it did not downgrade to the plain mock" 'should never publish unauthenticated' \ | ||
| 532 | + "$(curl -s -m3 http://127.0.0.1:$PORT/__publishes)" | ||
| 533 | + | ||
| 534 | +# 2) A pin that does not match is still a refusal. | ||
| 535 | +post "action=save" "{\"allowed_pins\":\"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\"}" >/dev/null | ||
| 536 | +R=$(post "action=send" '{"message":"wrong pin"}') | ||
| 537 | +json_ok "a non-matching pin is refused" "$R" "d['ok']==False" | ||
| 538 | + | ||
| 539 | +# 3) The right pin is accepted -- the path a self-hosted relay on a private CA | ||
| 540 | +# takes, so it has to work, and the pin the plugin derives from its own | ||
| 541 | +# peer certificate has to agree with the one openssl derives. | ||
| 542 | +post "action=save" "{\"allowed_pins\":\"$PIN_REAL\"}" >/dev/null | ||
| 543 | +R=$(post "action=send" '{"message":"pinned publish ok","title":"PinTest"}') | ||
| 544 | +json_ok "the correct SPKI pin is accepted" "$R" "d['ok']==True" | ||
| 545 | +wait_for 5 "the pinned publish reached the TLS mock" tls_published_has 'pinned publish ok' | ||
| 546 | + | ||
| 547 | +# 4) A pin never becomes a bypass for hostname checking. The same leaf, | ||
| 548 | +# reached on 127.0.0.2 (outside its only IP SAN), must be refused even with | ||
| 549 | +# its own correct pin. 127.0.0.2 rather than a hostname because this | ||
| 550 | +# container resolves nothing -- a name only exercises the resolver error. | ||
| 551 | +post "action=save" "{\"server_url\":\"https://127.0.0.2:$TLSMMSMPORT\",\"allowed_pins\":\"$PIN_REAL\"}" >/dev/null | ||
| 552 | +R=$(post "action=send" '{"message":"hostname mismatch"}') | ||
| 553 | +json_ok "a pin cannot override a hostname mismatch" "$R" "d['ok']==False" | ||
| 554 | +check "and says so" 'hostname' "$R" | ||
| 555 | +post "action=save" "{\"server_url\":\"$HTTPS_URL\"}" >/dev/null | ||
| 556 | + | ||
| 557 | +# 5) The only no-verify mode is the explicit, loud environment opt-in. | ||
| 558 | +R=$(post "action=send" '{"message":"insecure mode opt-in"}' NTFY_TLS_INSECURE=1) | ||
| 559 | +json_ok "NTFY_TLS_INSECURE=1 is the documented explicit opt-in" "$R" "d['ok']==True" | ||
| 560 | + | ||
| 561 | +# 6) Plain http to a non-loopback server is refused: a bearer token would ride | ||
| 562 | +# in the clear. save reports ok (forms fill in stages) plus the verdict -- | ||
| 563 | +# the verdict is the refusal. | ||
| 564 | +R=$(post "action=save" '{"server_url":"http://ntfy.example.invalid","topic":"tin"}') | ||
| 565 | +json_ok "plain http to a non-loopback server is refused" "$R" \ | ||
| 566 | + "d['valid']==False and 'plain http' in d.get('validation','')" | ||
| 567 | + | ||
| 568 | +# put the transport back to the offline plain mock for anything that follows | ||
| 569 | +post "action=save" "{\"server_url\":\"http://127.0.0.1:$PORT\",\"topic\":\"tin\",\"publish_topic\":\"tout\",\"allowed_pins\":\"\"}" >/dev/null | ||
| 570 | +tls_cleanup; TLSPID="" | ||
| 571 | + | ||
| 474 | # ---------------------------------------------------------------- hardening | 572 | # ---------------------------------------------------------------- hardening |
| 475 | section "builds - ASan + fortify + PE header" | 573 | section "builds - ASan + fortify + PE header" |
| 476 | if [ ! -x tests/asan/plugin_ntfy_asan ]; then make asan >"$WORK/asan.build" 2>&1 || true; fi | 574 | if [ ! -x tests/asan/plugin_ntfy_asan ]; then make asan >"$WORK/asan.build" 2>&1 || true; fi |
| 477 | 575 |