taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

refund-amount-swallows-extra-decimal-points.md (4977B)


      1 # A refund amount with two decimal points refunds a different number
      2 
      3 > **Fixed on master** (checked 2026-11-08, commit `24f7b249`). See [STATUS.md](STATUS.md) for the fix and the test that pins it.
      4 
      5 **Severity:** medium-high -- it is a money bug with no warning attached. The
      6 cashier types one amount, the terminal accepts it without comment, and the
      7 customer is refunded another.
      8 
      9 **Where:** `taler-kotlin-android/src/main/java/net/taler/common/Amount.kt:96`
     10 (`fromString`), reached from
     11 `merchant-terminal/src/main/java/net/taler/merchantpos/refund/RefundFragment.kt`
     12 (`submitRefund`).
     13 
     14 **Found by:** `pos-simulator`, scenario `refund_amount_edges`, which reported
     15 that a refund of `'1.0.0'` was accepted; `refund_amount_swallows_extra_dots`
     16 is the sharpened version.
     17 
     18 ## What happens
     19 
     20 Refund a paid order and type an amount with two decimal points -- a slip, or
     21 the thousands separator of a locale that uses `.` for it. The terminal accepts
     22 it and goes straight to the refund code. What it refunds is not what was typed:
     23 
     24 | Typed | Refunded |
     25 | --- | --- |
     26 | `1.0.0` | 1.00 |
     27 | `1.000.50` | 1.00 |
     28 | `12.34.56` | 12.34 |
     29 | `10.5.0` | 10.50 |
     30 
     31 No error, no rounding notice, nothing. `1.000.50` on an order of ten units
     32 refunds one.
     33 
     34 Observed, on an order of KUDOS:10.00 paid by a real wallet against
     35 `backend.demo.taler.net`:
     36 
     37 ```
     38 typing '1.000.50' as the refund amount on a KUDOS:10.00 order
     39 after approving: accepted=True, dialog='Refund':
     40     'Please let customer scan QR Code to offer refund ク1.00
     41      Purchase reference: 2026.302-EWDB9FKBH4DJG' ['Abort']
     42 the backend recorded a refund of 'KUDOS:1'
     43 ```
     44 
     45 The refund screen does print the amount it settled on -- "ク1.00" -- so a
     46 cashier who reads it carefully can catch this. Nothing draws their attention to
     47 it, and the screen they are reading is the one that says the refund has already
     48 been made.
     49 
     50 ## What makes it a defect rather than a gap
     51 
     52 The form does validate. `refund_amount_edges` submits four malformed amounts
     53 on an order of 1.00 and the terminal catches three of them, each with a
     54 message that names the problem:
     55 
     56 | Typed | What the form said |
     57 | --- | --- |
     58 | `99.00` | "Greater than order amount of 1.00" |
     59 | `0.00` | "Needs to be positive amount" |
     60 | `abc` | "Invalid amount" |
     61 | `1.0.0` | *accepted* |
     62 
     63 So this is not a screen with no input checking; it is one case slipping
     64 through the checking that is there -- which is why it has survived, and why it
     65 is worth fixing at the parser rather than by adding one more branch to the
     66 form.
     67 
     68 ## Why
     69 
     70 ```kotlin
     71 fun fromString(currency: String, str: String): Amount {
     72     // value
     73     val valueSplit = str.split(".")
     74     val value = checkValue(valueSplit[0].toLongOrNull())
     75     // fraction
     76     val fraction: Int = if (valueSplit.size > 1) {
     77         val fractionStr = valueSplit[1]
     78         ...
     79 ```
     80 
     81 `split(".")` on `"1.000.50"` gives three pieces. The parser reads
     82 `valueSplit[0]` and `valueSplit[1]` and never looks at the rest, so everything
     83 after the second point is discarded in silence.
     84 
     85 The validator in the same companion object gets it right --
     86 
     87 ```kotlin
     88 fun isValidAmountStr(str: String): Boolean {
     89     if (str.count { it == '.' } > 1) return false
     90 ```
     91 
     92 -- but `submitRefund` does not call it. It calls `fromString` directly, inside
     93 a `try` that only catches `AmountParserException`, and `fromString` does not
     94 throw one here: as far as it is concerned the input parsed fine.
     95 
     96 ## Scope
     97 
     98 `Amount` is in `taler-kotlin-android`, so `fromString` is shared by the
     99 terminal, the wallet and the cashier app. The refund form is the place this
    100 round reached, because it is a free-text amount field fed straight to the
    101 parser; any other screen that parses user-typed text with `fromString` has the
    102 same hole. The order screen's custom-product dialog is worth checking next --
    103 it also takes a typed amount.
    104 
    105 ## Reproducing it
    106 
    107 ```bash
    108 cd /tmp/taler-pos-sim/driver && . ../env.sh
    109 python3 suite.py refund_amount_swallows_extra_dots
    110 ```
    111 
    112 It bills ten units, has the customer wallet pay, types `1.000.50` into the
    113 refund amount, and compares what the backend recorded against what was typed.
    114 
    115 ## The fix
    116 
    117 `refund-amount-swallows-extra-decimal-points.diff`: two lines in `fromString`,
    118 rejecting more than one separator so that the parser agrees with the validator
    119 that has always been beside it.
    120 
    121 Verified with the patch applied: `:taler-kotlin-android:testDebugUnitTest`
    122 (20 tests, including the 9 of `AmountTest`), `:merchant-terminal:testSimUnitTest`
    123 (37 tests) and `:wallet:testFdroidDebugUnitTest` (116 tests) all pass. The
    124 change only turns input that was previously misread into input that is
    125 rejected, so nothing that was parsed correctly before parses differently now.
    126 
    127 Worth doing as well, though it needs a decision rather than a patch: the refund
    128 field accepts anything and only complains when Approve is pressed. Validating
    129 as the cashier types -- `isValidAmountStr` is right there -- would stop the
    130 whole class at the field rather than at the parser.