repro_encoded_path_separator.py (3177B)
1 # This file is part of GNU Taler 2 # (C) 2026 Taler Systems S.A. 3 # 4 # GNU Taler is free software; you can redistribute it and/or modify it under the 5 # terms of the GNU General Public License as published by the Free Software 6 # Foundation; either version 3, or (at your option) any later version. 7 # 8 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 9 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 10 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 11 # 12 # You should have received a copy of the GNU General Public License along with 13 # GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 14 """The terminal asks for its configuration at `/private%2Fpos`. 15 16 Starts a merchant backend that routes on exact paths, points the terminal at it 17 through the login form, and prints every request that arrived with the path 18 separator percent-encoded. 19 20 cd /tmp/taler-pos-sim/driver && . ../env.sh 21 python3 ../findings/repro_encoded_path_separator.py 22 23 Exits 0 when the encoded request reproduces, 1 when it does not. 24 """ 25 26 import os 27 import sys 28 import threading 29 import time 30 from pathlib import Path 31 32 HERE = Path(__file__).resolve().parent 33 sys.path.insert(0, str(HERE.parent / "driver")) 34 sys.path.insert(0, str(HERE.parent / "backend")) 35 36 import mockd 37 from device import Device, serial_for 38 from pos import Pos 39 40 SERIAL = serial_for() 41 PORT = int(os.environ.get("POS_MOCK_PORT", "19443")) 42 INSTANCE = "mockshop" 43 44 45 def main() -> int: 46 print(f"1. a merchant backend on https://localhost:{PORT}, routing on exact paths") 47 server, state = mockd.build_server("healthy", PORT, INSTANCE, 48 f"localhost:{PORT}") 49 threading.Thread(target=server.serve_forever, daemon=True).start() 50 time.sleep(1) 51 52 dev = Device(SERIAL) 53 app = Pos(dev) 54 dev.adb("reverse", f"tcp:{PORT}", f"tcp:{PORT}", check=False) 55 56 print("2. provisioning the terminal against it through the login form") 57 app.fresh_start() 58 try: 59 app.provision_by_form(f"localhost:{PORT}", INSTANCE, "mock-secret", 60 expect_success=False) 61 finally: 62 pass 63 64 print("3. waiting for the configuration request") 65 deadline = time.monotonic() + 45 66 while time.monotonic() < deadline: 67 if any("private" in p for p in state.paths()): 68 break 69 time.sleep(2) 70 time.sleep(3) 71 72 print() 73 print("paths the terminal asked for:") 74 for path in state.paths(): 75 print(f" {path}") 76 encoded = state.encoded_slash_requests() 77 server.shutdown() 78 server.server_close() 79 80 print() 81 if encoded: 82 print(f"REPRODUCED: {len(encoded)} request(s) with an encoded path " 83 f"separator, e.g. {encoded[0]}") 84 print("A reverse proxy that does not decode encoded slashes -- Apache " 85 "with its default AllowEncodedSlashes Off -- answers 404 for " 86 "that, and the terminal cannot be configured at all.") 87 return 0 88 print("not reproduced: every path arrived with real separators.") 89 return 1 90 91 92 if __name__ == "__main__": 93 sys.exit(main())