|
| 1 | +/* |
| 2 | + * ============================================================================ |
| 3 | + * Name : SyncCredentialValidator.java |
| 4 | + * Author : IIAB Project |
| 5 | + * Copyright : Copyright (c) 2026 IIAB Project |
| 6 | + * Description : Domain rule: what counts as a *safe* peer-to-peer sync |
| 7 | + * credential. Closes tech-debt item S1 (rsyncd.conf / rsync:// |
| 8 | + * URL injection from QR-scanned credentials). |
| 9 | + * ============================================================================ |
| 10 | + */ |
| 11 | +package org.iiab.controller.sync.domain; |
| 12 | + |
| 13 | +/** |
| 14 | + * Pure (framework-free) validation of peer-to-peer sync credentials. |
| 15 | + * |
| 16 | + * <p>The sync handshake transports a host IP, port, username and password |
| 17 | + * inside a QR code that the receiving device scans. Those values are |
| 18 | + * <strong>untrusted input</strong>: they are later interpolated into an |
| 19 | + * {@code rsyncd.conf} file (server side) and into a {@code rsync://user@host:port/...} |
| 20 | + * URL (client side). Without validation, a crafted QR code could inject extra |
| 21 | + * {@code rsyncd.conf} directives (e.g. a newline followed by a new module |
| 22 | + * section) or break out of the URL — see tech-debt item <b>S1</b>. |
| 23 | + * |
| 24 | + * <p>This class is the single source of truth for "what is a valid credential". |
| 25 | + * It lives in the domain layer: no {@code android.*}, no networking, no I/O, so |
| 26 | + * it is fully unit-testable on a plain JVM and reusable by any other injection |
| 27 | + * fix (e.g. S4/D2) that needs to validate or quote shell/config input. |
| 28 | + * |
| 29 | + * <p>The policy is intentionally strict and fail-closed. Legitimate credentials |
| 30 | + * are produced by {@code SyncHandshakeHelper.generateSecurePassword()} |
| 31 | + * (12 alphanumeric chars) and a fixed username, so a conservative character set |
| 32 | + * rejects attacks without rejecting any real value. |
| 33 | + */ |
| 34 | +public final class SyncCredentialValidator { |
| 35 | + |
| 36 | + /** Max username length accepted in {@code auth users} / URL userinfo. */ |
| 37 | + private static final int MAX_USERNAME_LEN = 64; |
| 38 | + /** Max password length accepted in the rsync password file. */ |
| 39 | + private static final int MAX_PASSWORD_LEN = 128; |
| 40 | + /** Max host/IP length (DNS label limit is 253). */ |
| 41 | + private static final int MAX_HOST_LEN = 253; |
| 42 | + |
| 43 | + private SyncCredentialValidator() { |
| 44 | + // Static utility; not instantiable. |
| 45 | + } |
| 46 | + |
| 47 | + /** Outcome of a validation, with a machine-readable reason on failure. */ |
| 48 | + public static final class Result { |
| 49 | + public final boolean valid; |
| 50 | + /** Null when {@link #valid}; otherwise a short, log-safe reason. */ |
| 51 | + public final String reason; |
| 52 | + |
| 53 | + private Result(boolean valid, String reason) { |
| 54 | + this.valid = valid; |
| 55 | + this.reason = reason; |
| 56 | + } |
| 57 | + |
| 58 | + public static Result ok() { |
| 59 | + return new Result(true, null); |
| 60 | + } |
| 61 | + |
| 62 | + public static Result fail(String reason) { |
| 63 | + return new Result(false, reason); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Validate a full set of scanned sync credentials. Call this at the parse |
| 69 | + * boundary (QR decode); reject the whole handshake if it returns invalid. |
| 70 | + */ |
| 71 | + public static Result validateCredentials(String ip, int port, String user, String pass) { |
| 72 | + if (!isValidHost(ip)) { |
| 73 | + return Result.fail("invalid host/ip"); |
| 74 | + } |
| 75 | + if (!isValidPort(port)) { |
| 76 | + return Result.fail("invalid port"); |
| 77 | + } |
| 78 | + if (!isValidUsername(user)) { |
| 79 | + return Result.fail("invalid username"); |
| 80 | + } |
| 81 | + if (!isValidPassword(pass)) { |
| 82 | + return Result.fail("invalid password"); |
| 83 | + } |
| 84 | + return Result.ok(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * A username safe for both the {@code rsync://USER@host} URL userinfo and |
| 89 | + * the {@code auth users = USER} directive: a non-empty run of |
| 90 | + * {@code [A-Za-z0-9_-]} starting with a letter, digit or underscore. |
| 91 | + * Matches the fixed {@code iiab_peer} account. |
| 92 | + */ |
| 93 | + public static boolean isValidUsername(String user) { |
| 94 | + if (user == null || user.isEmpty() || user.length() > MAX_USERNAME_LEN) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + for (int i = 0; i < user.length(); i++) { |
| 98 | + char c = user.charAt(i); |
| 99 | + boolean ok = (c >= 'A' && c <= 'Z') |
| 100 | + || (c >= 'a' && c <= 'z') |
| 101 | + || (c >= '0' && c <= '9') |
| 102 | + || c == '_' |
| 103 | + || (c == '-' && i > 0); // '-' allowed, but not as the first char |
| 104 | + if (!ok) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + } |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * A password safe to write as a single line in the rsync password / secrets |
| 113 | + * file: printable ASCII only, no whitespace and no control characters (so it |
| 114 | + * cannot add a second secrets line or a stray {@code rsyncd.conf} directive). |
| 115 | + * The app's generated alphanumeric passwords satisfy this. |
| 116 | + */ |
| 117 | + public static boolean isValidPassword(String pass) { |
| 118 | + if (pass == null || pass.isEmpty() || pass.length() > MAX_PASSWORD_LEN) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + for (int i = 0; i < pass.length(); i++) { |
| 122 | + char c = pass.charAt(i); |
| 123 | + // Printable ASCII excluding space (0x20) and DEL (0x7f). |
| 124 | + if (c <= 0x20 || c >= 0x7f) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + } |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * A host that is either a dotted IPv4 literal or a DNS hostname, using only |
| 133 | + * {@code [A-Za-z0-9.-]}. Rejects anything containing {@code @ : / \\}, |
| 134 | + * whitespace or control characters that could break out of the URL. |
| 135 | + */ |
| 136 | + public static boolean isValidHost(String host) { |
| 137 | + if (host == null || host.isEmpty() || host.length() > MAX_HOST_LEN) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + for (int i = 0; i < host.length(); i++) { |
| 141 | + char c = host.charAt(i); |
| 142 | + boolean ok = (c >= 'A' && c <= 'Z') |
| 143 | + || (c >= 'a' && c <= 'z') |
| 144 | + || (c >= '0' && c <= '9') |
| 145 | + || c == '.' |
| 146 | + || c == '-'; |
| 147 | + if (!ok) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + } |
| 151 | + // Reject leading/trailing dot or dash and empty labels (".." / "a..b"). |
| 152 | + if (host.charAt(0) == '.' || host.charAt(0) == '-' |
| 153 | + || host.charAt(host.length() - 1) == '.' |
| 154 | + || host.charAt(host.length() - 1) == '-' |
| 155 | + || host.contains("..")) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + return true; |
| 159 | + } |
| 160 | + |
| 161 | + /** TCP port in the valid 1..65535 range. */ |
| 162 | + public static boolean isValidPort(int port) { |
| 163 | + return port >= 1 && port <= 65535; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * True if {@code value} is safe to embed verbatim on a single line of an |
| 168 | + * {@code rsyncd.conf} file: no control characters (notably CR/LF, which |
| 169 | + * would let an attacker append new directives or module sections) and no |
| 170 | + * NUL. Use this for app-controlled values such as the shared directory path. |
| 171 | + */ |
| 172 | + public static boolean isSafeConfigValue(String value) { |
| 173 | + if (value == null) { |
| 174 | + return false; |
| 175 | + } |
| 176 | + for (int i = 0; i < value.length(); i++) { |
| 177 | + char c = value.charAt(i); |
| 178 | + if (c == '\n' || c == '\r' || c == '\0' || c < 0x20 && c != '\t') { |
| 179 | + return false; |
| 180 | + } |
| 181 | + } |
| 182 | + return true; |
| 183 | + } |
| 184 | +} |
0 commit comments