Modern Apps git

← Modern-Apps.git

web: decode probe JSON escapes in a single pass

Commit
4aa15168d7dc45f7884e297900c36afc0b996fa0
Author
Vayun Mathur <[email protected]>
When
2026-11-08 04:15 UTC (28 days ago)
Parents
c64cb2e
The probe parser's unescapeJson ran three chained replaces: quote,
backslash, then slash. The passes re-scanned each other's output, so a
title containing a backslash immediately before a slash (JSON p\\/q)
lost the backslash: the backslash pass produced \/ and the slash pass
then ate the survivor, yielding p/q in shortcut and install labels.

Replace the chain with a single left-to-right walk that maps each
escape pair once (" \\ \/ plus \n \r \t \b \f \uXXXX), so
\\/ decodes to backslash + slash instead of collapsing. The walk
also decodes control escapes the old code left as literals (a
JSON.stringify newline stayed a two-char backslash-n).

Verified with a red-then-green regression test
(preservesBackslashBeforeSlashInTitle) plus the full
:web:testDevUnitTest suite: 335 tests, 0 failures, 0 errors.

Diff

web/src/main/java/com/vayunmathur/web/platform/PwaHelper.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/PwaHelper.kt b/web/src/main/java/com/vayunmathur/web/platform/PwaHelper.ktindex 3abff4868d..9455fad4d1 100644--- a/web/src/main/java/com/vayunmathur/web/platform/PwaHelper.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/PwaHelper.kt@@ -114,8 +114,67 @@ object PwaHelper {         return s.takeIf { it.isNotBlank() && it != "null" }     } -    private fun String.unescapeJson(): String =-        replace("\\\"", "\"").replace("\\\\", "\\").replace("\\/", "/")+    /**+     * Single-pass JSON/JS-string unescaper. The old chained+     * `replace("\\\"", ...).replace("\\\\", ...).replace("\\/", ...)` re-scanned+     * already-produced output: after step 2 turned `\\/` into `\/`, step 3 ate+     * the remaining backslash, so a title like `p\/q` decoded to `p/q`.+     * Walking once keeps each escape pair intact (`\\/` → `\` + `/`).+     */+    private fun String.unescapeJson(): String {+        val out = StringBuilder(length)+        var i = 0+        while (i < length) {+            val c = this[i]+            if (c != '\\' || i + 1 >= length) {+                out.append(c)+                i+++                continue+            }+            when (val e = this[i + 1]) {+                '"', '\\', '/' -> {+                    out.append(e)+                    i += 2+                }+                'n' -> {+                    out.append('\n')+                    i += 2+                }+                'r' -> {+                    out.append('\r')+                    i += 2+                }+                't' -> {+                    out.append('\t')+                    i += 2+                }+                'b' -> {+                    out.append('\b')+                    i += 2+                }+                'f' -> {+                    out.append('\u000C')+                    i += 2+                }+                'u' -> {+                    val hex = if (i + 5 < length) substring(i + 2, i + 6) else null+                    val code = hex?.toIntOrNull(16)+                    if (code != null) {+                        out.append(code.toChar())+                        i += 6+                    } else {+                        out.append('\\')+                        i+++                    }+                }+                else -> {+                    out.append(e)+                    i += 2+                }+            }+        }+        return out.toString()+    }      private fun String.strField(key: String): String? {         val pattern = "\"" + key + "\"\\s*:\\s*\"((?:\\\\\"|[^\"])*)\""
web/src/test/java/com/vayunmathur/web/platform/PwaHelperTest.kt
diff --git a/web/src/test/java/com/vayunmathur/web/platform/PwaHelperTest.kt b/web/src/test/java/com/vayunmathur/web/platform/PwaHelperTest.ktindex 795cb6e001..ab1df52df7 100644--- a/web/src/test/java/com/vayunmathur/web/platform/PwaHelperTest.kt+++ b/web/src/test/java/com/vayunmathur/web/platform/PwaHelperTest.kt@@ -77,6 +77,16 @@ class PwaHelperTest {         assertEquals(24, blank.width)     } +    @Test fun preservesBackslashBeforeSlashInTitle() {+        // Title `p\/q` (p, backslash, slash, q): JSON.stringify emits `"p\\/q"`,+        // evaluateJavascript doubles the backslashes in transit.+        val raw = """{"title":"p\\/q","origin":"https://x.example"}"""+        val escaped = "\"" + raw.replace("\\", "\\\\").replace("\"", "\\\"") + "\""+        val info = PwaHelper.parseProbeJson(escaped)+        assertNotNull(info)+        assertEquals("p\\/q", info.name)+    }+     @Test fun manifestProbeJsMentionsManifestAndIcons() {         assertTrue(PwaHelper.MANIFEST_PROBE_JS.contains("manifest"))         assertTrue(PwaHelper.MANIFEST_PROBE_JS.contains("JSON.stringify"))