Home / Learn / 17 Information flow
Release v0.4.0Edition 0.2
17.8 Common mistakes
Page 9 of 11
Each mistake below is a small change to the day-to-day program or to a step of this chapter, as it says. Each diagnostic names the place a value would flow into, the label it has, and the label that place allows; read those three, and the repair follows.
17.8.1 Dropping a label in a return type (U4001)
In step 03, declare the margin as a plain Int:
--- a/src/pricing.una
+++ b/src/pricing.una
@@ -9,3 +9,3 @@
--- The margin of a product, in cents. It is computed from the cost, so it is confidential too.
-margin(product: Product) -> confidential Int = product.price - product.cost
+margin(product: Product) -> Int = product.price - product.cost
[U4001] Error: the result of `margin` is `Int`, and this value is `confidential Int`: a flow from `confidential` to `public`
Why: product.cost is confidential, so the difference is too, and a result is checked against the declared label. It happens when a function is first written with plain types, and a field it reads gains a label later. The message gives the declared type, Int, then the value's, confidential Int, then the direction of the flow.
Fix: declare confidential Int. A header is where callers learn what they receive.
17.8.2 A sink without accepts (U4003)
Remove accepts secret[PII] from Mailer:
--- a/src/notices.una
+++ b/src/notices.una
@@ -32,3 +32,2 @@
fails ServiceError
- accepts secret[PII]
[U4003] Error: `@mailer.send` accepts `public` data, and this argument is `secret[PII]`
Why: an operation accepts public data unless it says more. It happens when a capability is declared before its data is labeled, as in chapter 12. una reports the address and the body, one diagnostic each, since each argument is its own sink; the mark points at the argument.
Fix: give the operation the ceiling of the service it stands for. Do not lower the data to fit: the mail service needs the real address.
17.8.3 A branch that decides a public result (U4001)
In step 19, declare the fee as a plain Int:
--- a/src/shipping.una
+++ b/src/shipping.una
@@ -9,3 +9,3 @@
--- what the customer spent, so the fee is confidential, even though 0 and 790 are not.
-shipping_fee(spent: confidential Int) -> confidential Int = if spent >= 50000 then 0 else 790
+shipping_fee(spent: confidential Int) -> Int = if spent >= 50000 then 0 else 790
[U4001] Error: the result of `shipping_fee` is `public`, and which value it returns depends on this `confidential` condition: a flow from `confidential` to `public`
Why: the fee is a constant in each branch, but which branch runs depends on spent. It is easy to miss, since no labeled value appears in the result. The message says which value it returns depends on this confidential condition, and the mark points at the condition, since that is where the information enters.
Fix: declare confidential Int.
17.8.4 A category that is not declared (U4007)
In step 26, remove the [labels] table from una.toml:
--- a/una.toml
+++ b/una.toml
@@ -5,5 +5,2 @@
-[labels]
-categories = ["PII"]
-
[run]
[U4007] Error: label category `PII` is not declared in `[labels] categories` of `una.toml`
╭─[ src/courier.una:7:17 ]
[U4007] Error: label category `PII` is not declared in `[labels] categories` of `una.toml`
╭─[ src/courier.una:13:20 ]
Why: each package declares its categories, so a typo cannot create one. It happens when code is copied from another package, whose manifest declared the category. una reports each use of the category, one diagnostic each: in the field phone, and in the accepts clause of book. The marks point at the uses, not at the manifest, since that is where the unknown name is used.
Fix: declare categories = ["PII"] under [labels].
17.8.5 A trusted module that una.toml does not list (U7005)
Remove the [trusted] table of the day-to-day program:
--- a/una.toml
+++ b/una.toml
@@ -8,5 +8,2 @@
-[trusted]
-modules = ["privacy"]
-
[run]
[U7005] Error: module `privacy` is `trusted` in its header but not listed in una.toml [trusted] modules
Why: trust is declared twice, in the module and in the manifest, so that adding it shows in review. It happens when a trusted module is copied into a package without its manifest line. The message names the module and the half of the declaration that is missing.
Fix: list the module under [trusted] modules, after a review of what it releases.
17.8.6 A public via function (U4005)
Make mask_email public:
--- a/src/privacy.una
+++ b/src/privacy.una
@@ -10,3 +10,3 @@
-mask_email(address: secret[PII] Text) -> public Text
+pub mask_email(address: secret[PII] Text) -> public Text
=
[U4005] Error: the `via` function of a policy is private to its trusted module, and `mask_email` is not; remove its `pub`, or declare it in this module
Why: a public via function would release data without the policy, so its calls would be missing from the report. It happens when a helper is made public to reuse it elsewhere, or to test it. The message names the function and gives both repairs.
Fix: keep via private, and call the policy, masked_email. An example can call the policy directly, as section 17.7 does.
Next:17.9 Exercises