upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/rpc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/rpc.cpp')
-rw-r--r--src/rpc.cpp27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/rpc.cpp b/src/rpc.cpp
index 285f11b..3db8b94 100644
--- a/src/rpc.cpp
+++ b/src/rpc.cpp
@@ -681,7 +681,32 @@ static bool HandleLine(Miner* m, const std::string& rawLine,
681 int& blocksFoundThisSession) 681 int& blocksFoundThisSession)
682{ 682{
683 json req; 683 json req;
684 try { req = json::parse(rawLine); } 684 try {
685 // Fast pre-parse depth check to prevent stack overflow in json::parse.
686 // Legitimate Stratum requests have a depth of at most 4.
687 int depth = 0;
688 bool in_string = false;
689 bool escape = false;
690 for (char c : rawLine) {
691 if (in_string) {
692 if (escape) escape = false;
693 else if (c == '\\') escape = true;
694 else if (c == '"') in_string = false;
695 } else {
696 if (c == '"') in_string = true;
697 else if (c == '{' || c == '[') {
698 if (++depth > 20) {
699 LogPrint("worker", "[worker] JSON too deeply nested from %s\n",
700 m->address.empty() ? "(unauth)" : m->address.c_str());
701 return true;
702 }
703 } else if (c == '}' || c == ']') {
704 depth--;
705 }
706 }
707 }
708 req = json::parse(rawLine);
709 }
685 catch (...) { 710 catch (...) {
686 LogPrint("worker", "[worker] JSON parse error from %s: %.80s\n", 711 LogPrint("worker", "[worker] JSON parse error from %s: %.80s\n",
687 m->address.empty() ? "(unauth)" : m->address.c_str(), 712 m->address.empty() ? "(unauth)" : m->address.c_str(),