Skip to content

fix: escape RUFLET_ANDROID_* signing values in key.properties so Gradle reads them as set - #132

Merged
AdamMusa merged 2 commits into
mainfrom
audit/android-key-properties-escaping
Nov 21, 2026
Merged

AdamMusa merged 2 commits into
mainfrom
audit/android-key-properties-escaping

Conversation

@AdamMusa

Copy link
Copy Markdown
Owner

Fixes #131

What changed

packages/ruflet/lib/ruflet/cli/build/native_config/gradle.rb: the client's build.gradle.kts reads build/client/android/key.properties with java.util.Properties#load. That loader decodes backslash escapes, ends a value at a line break, skips the blanks a value starts with, and reads the file as ISO-8859-1. The CLI now writes values so that load returns them as set.

  • RUFLET_ANDROID_* values (the passwords themselves, plus the expanded keystore path) are escaped, but only where load would misread them:

    • \ becomes \\;
    • LF and CR become \n and \r;
    • a leading blank is escaped;
    • anything outside ASCII becomes \uXXXX UTF-16 code units, as Properties#store writes it.

    A leading #/!, tabs and form feeds are left alone. Properties#store would escape them, but load already reads them correctly, so escaping them would change working output.

  • The project's own android/key.properties is Properties text already, so its values are copied unchanged. The one exception is the relative storeFile the CLI resolves against the project folder: its non-ASCII characters (from a folder such as Développement) are escaped. Its backslashes are the author's escapes and stay as written. Raw non-ASCII can never be read back by load.

  • C/POSIX locale: Ruby tags the environment as binary there. Those values are treated as UTF-8 bytes instead of raising Encoding::UndefinedConversionError in write_text_file. Values in another locale encoding are transcoded, as writing them did before.

  • File mode: key.properties holds plaintext passwords, so it is created 0600 and the mode is reset on a file an earlier build left in build/client.

  • The .strip of environment values is unchanged.

Tests:

  • test/build_android_signing_test.rb (new) reads the written file through a port of the JDK's Properties.load (LineReader and loadConvert). When a JVM is installed, it checks that port and the escaping against java.util.Properties itself; otherwise that test skips. It covers:
    • backslashes, a trailing backslash, BMP and astral non-ASCII, LF/CR;
    • a non-ASCII keystore path and alias;
    • a project file in a non-ASCII folder;
    • the C locale, run in a subprocess;
    • the 0600 mode on a new file and on a leftover file.
  • Two of its tests pin the hard rule: plain-ASCII environment values (including #, !, =, :, tab, FF) and a project file in an ASCII folder are written byte for byte as before. Both also pass on unmodified main.
  • New H1 golden build-apk-with-escaped-signing-env: a backslash and a non-ASCII password. The existing build-apk-with-signing-env and build-apk-with-partial-signing-env goldens are unchanged.

How verified

  • Bug planted back. I restored main's gradle.rb with the new tests in place. Under LANG=en_US.UTF-8, 7 of the 9 new tests fail: 5 failures and 2 errors. The two hard-rule tests pass. The new golden fails with the raw compat\not-a-secret / compat-pässwörd-€ content, and the two existing signing goldens pass.
  • With the fix:
    • ruby test/compat/compat.rb --jobs 4 reports 359 goldens checked, 0 failing.
    • Every packages/ruflet/test/*_test.rb, run one process per file (28 files), has 0 failures and 0 errors.
    • The new test file passes under both LANG=en_US.UTF-8 and LC_ALL=C.
  • End to end with JDK 17.0.16. I drove apply_android_signing_config and read the result with java.util.Properties:
    • p\ssw0rd#1, C:\keys\pass, pa\ss\word, pässwörd€😀 and endslash\ load back exactly, and keyAlias is no longer swallowed.
    • A relative storeFile in Développement/ loads as the real path and File.exists() is true.
  • Not run: local Ruby here is 4.0.5, and CI covers .ruby-version 3.4.5.

Behavior changes

Every configuration that works today writes byte-identical key.properties content. That covers plain ASCII values, and a project file with any escapes in an ASCII folder. The changed outputs below are all bug fixes:

  1. A RUFLET_ANDROID_* value containing a backslash, a line break or non-ASCII text is now escaped. Before, Gradle read a different password and release signing failed with "keystore password was incorrect", or the CLI crashed under a C locale. Visible to one group: someone who worked around the bug by pre-escaping a CI secret (setting C:\\keys\\pass to mean C:\keys\pass, or pasting escaped values from key.properties) now gets the literal value. They need to set the plain password instead. The docs say to "set the same values in the environment"; the environment values are the passwords themselves.
  2. A relative storeFile from the project's android/key.properties, resolved against a folder with non-ASCII characters, now has those characters escaped. Before, Gradle could not find the keystore.
  3. build/client/android/key.properties is now mode 0600 instead of following the umask (usually 0644). Gradle runs as the same user, so builds are unaffected.

Docs note (not edited here): publishing_android.md says "In CI, set the same values in the environment instead". It could say that these are the plain values, not Properties-escaped text.

…le reads them as set

The client's build.gradle.kts reads build/client/android/key.properties
with java.util.Properties#load, which decodes backslash escapes and reads
the file as ISO-8859-1. The CLI wrote the RUFLET_ANDROID_* values raw, so
a password with a backslash or non-ASCII text reached Gradle as another
value (p\ssw0rd#1 -> pssw0rd#1, pässwörd -> pässwörd), a trailing
backslash swallowed the keyAlias line, and under a C/POSIX locale a
non-ASCII value raised Encoding::UndefinedConversionError. A relative
storeFile resolved against a non-ASCII project folder was garbled too.

- Escape what load misreads in the values taken from the environment
  (backslash, LF, CR, a leading blank, non-ASCII as \uXXXX UTF-16 code
  units). Values load already reads correctly are written byte for byte
  as before.
- Copy the project's own key.properties values unchanged; escape only
  the non-ASCII characters of the storeFile the CLI resolves against the
  project folder.
- Treat binary-tagged values (C locale) as UTF-8 instead of raising.
- Create key.properties 0600 and reset the mode on a leftover file.

Fixes #131
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant