Skip to content

Commit 0ecb19e

Browse files
committed
descriptor-policy: disallow mixed cardinality key expressions
Point 9 of the policy key requirements.
1 parent bba7474 commit 0ecb19e

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/ctest/test_descriptor.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,16 @@ static const struct descriptor_test {
14561456
"sh(multi(1,@0/**,xpub6AHA9hZDN11k2ijHMeS5QqHx2KP9aMBRhTDqANMnwVtdyw2TDYRmF8PjpvwUFcL1Et8Hj59S3gTSMcUQ5gAqTz3Wd8EsMTmF3DChhqPQBnU))",
14571457
WALLY_NETWORK_BITCOIN_MAINNET, 0, 0, 0, NULL,
14581458
WALLY_MINISCRIPT_POLICY, NULL, ""
1459+
}, {
1460+
"policy errchk - mismatched key cardinalities (1)",
1461+
"sh(multi(1,@0/**,@1/*))",
1462+
WALLY_NETWORK_BITCOIN_MAINNET, 0, 0, 0, NULL,
1463+
WALLY_MINISCRIPT_POLICY, NULL, ""
1464+
}, {
1465+
"policy errchk - mismatched key cardinalities (2)",
1466+
"sh(multi(1,@0/<0;1>/*,@1/*))",
1467+
WALLY_NETWORK_BITCOIN_MAINNET, 0, 0, 0, NULL,
1468+
WALLY_MINISCRIPT_POLICY, NULL, ""
14591469
}
14601470
};
14611471

src/descriptor.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ static int canonicalize(const char *descriptor,
380380
int key_index_hwm = -1;
381381
const char *p = descriptor, *start;
382382
char *out;
383-
bool found_policy_key = false;
383+
bool found_policy_key = false, found_policy_single = false, found_policy_multi = false;;
384384

385385
*output = NULL;
386386
*num_substitutions = 0;
@@ -430,14 +430,18 @@ static int canonicalize(const char *descriptor,
430430
if (*p++ != '/')
431431
return WALLY_EINVAL;
432432
++required_len;
433-
if (*p == '<')
433+
if (*p == '<') {
434+
found_policy_multi = true;
434435
continue;
436+
}
435437
if (*p++ != '*')
436438
return WALLY_EINVAL;
437439
if (*p == '*') {
440+
found_policy_multi = true;
438441
++p;
439442
required_len += strlen("<0;1>/*");
440443
} else {
444+
found_policy_single = true;
441445
required_len += 1;
442446
}
443447
}
@@ -447,8 +451,12 @@ static int canonicalize(const char *descriptor,
447451

448452
if (!*p && (flags & WALLY_MINISCRIPT_REQUIRE_CHECKSUM))
449453
return WALLY_EINVAL; /* Checksum required but not present */
450-
if (!found_policy_key && flags & WALLY_MINISCRIPT_POLICY)
451-
return WALLY_EINVAL; /* At least one key expression must be present */
454+
if (flags & WALLY_MINISCRIPT_POLICY) {
455+
if (!found_policy_key)
456+
return WALLY_EINVAL; /* At least one key expression must be present */
457+
if (found_policy_single && found_policy_multi)
458+
return WALLY_EINVAL; /* Cannot mix cardinality of policy keys */
459+
}
452460
if (!(*output = wally_malloc(required_len + 1 + DESCRIPTOR_CHECKSUM_LENGTH + 1)))
453461
return WALLY_ENOMEM;
454462

0 commit comments

Comments
 (0)