-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeAPIKey.idr
More file actions
244 lines (209 loc) · 9.5 KB
/
Copy pathSafeAPIKey.idr
File metadata and controls
244 lines (209 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
-- SPDX-License-Identifier: PMPL-1.0-or-later
-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
||| BoJ SafeAPIKey — Formal verification of API key handling safety
module Boj.SafeAPIKey
import Data.List
import Data.List.Elem
import Data.Nat
import Data.String
import Boj.SafetyLemmas
%default total
--------------------------------------------------------------------------------
-- Key Format Validation
--------------------------------------------------------------------------------
public export
isBase64URLChar : Char -> Bool
isBase64URLChar c =
isAlphaNum c || c == '-' || c == '_' || c == '='
public export
isHexChar : Char -> Bool
isHexChar c =
(c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
public export
data Base64URLSafe : String -> Type where
MkBase64URLSafe : (s : String) ->
{auto prf : allRec Boj.SafeAPIKey.isBase64URLChar (unpack s) = True} ->
Base64URLSafe s
public export
data HexSafe : String -> Type where
MkHexSafe : (s : String) ->
{auto prf : allRec Boj.SafeAPIKey.isHexChar (unpack s) = True} ->
HexSafe s
--------------------------------------------------------------------------------
-- Entropy / Length Requirements
--------------------------------------------------------------------------------
public export
minKeyLength : Nat
minKeyLength = 22
public export
data SufficientEntropy : String -> Type where
MkSufficientEntropy : (s : String) ->
{auto prf : LTE Boj.SafeAPIKey.minKeyLength (length s)} ->
SufficientEntropy s
public export
isKnownWeakKey : String -> Bool
isKnownWeakKey s =
s == "test" || s == "demo" || s == "password" || s == "secret" ||
s == "12345" || s == "api_key" || s == "changeme" ||
allSame (unpack s)
where
allSame : List Char -> Bool
allSame [] = True
allSame (x :: xs) = allRec (== x) xs
public export
data NotWeakKey : String -> Type where
MkNotWeakKey : (s : String) ->
{auto prf : isKnownWeakKey s = False} ->
NotWeakKey s
--------------------------------------------------------------------------------
-- Scope Binding
--------------------------------------------------------------------------------
public export
data KeyScope : Type where
GlobalScope : KeyScope
CartridgeScope : (name : String) -> KeyScope
DomainScope : (domain : String) -> KeyScope
ReadOnlyScope : KeyScope
public export
record SafeAPIKeyRecord where
constructor MkSafeAPIKeyRecord
keyId : String
scope : KeyScope
keyLength : Nat
0 entropy : LTE Boj.SafeAPIKey.minKeyLength keyLength
--------------------------------------------------------------------------------
-- Timing-Safe Comparison
--------------------------------------------------------------------------------
public export
data EqualLength : String -> String -> Type where
MkEqualLength : (a : String) -> (b : String) ->
{auto prf : length a = length b} ->
EqualLength a b
public export
data TimingSafeResult : Type where
TSMatch : TimingSafeResult
TSMismatch : TimingSafeResult
--------------------------------------------------------------------------------
-- Logging Safety
--------------------------------------------------------------------------------
public export
data LogSafe : String -> Type where
MkLogSafe : (s : String) ->
{auto prf : LTE (length s) 12} ->
LogSafe s
export
toLogSafe : String -> String
toLogSafe s =
if length s <= 8
then "***"
else substr 0 4 s ++ "..." ++ substr (length s `minus` 4) 4 s
--------------------------------------------------------------------------------
-- Core Theorems
--------------------------------------------------------------------------------
private
minKeyLengthNotZero : LTE Boj.SafeAPIKey.minKeyLength 0 -> Void
minKeyLengthNotZero LTEZero impossible
minKeyLengthNotZero (LTESucc _) impossible
export
sufficientEntropyNonEmpty : SufficientEntropy s -> NonEmpty (unpack s)
sufficientEntropyNonEmpty (MkSufficientEntropy s {prf}) with (unpack s) proof upEq
sufficientEntropyNonEmpty (MkSufficientEntropy s {prf}) | [] =
let len_eq = unpackLength s
len_ge = replace {p = \n => LTE Boj.SafeAPIKey.minKeyLength n}
(sym len_eq) prf
lenNil = cong length upEq
impossibleLen : LTE Boj.SafeAPIKey.minKeyLength 0
impossibleLen = replace {p = \n => LTE Boj.SafeAPIKey.minKeyLength n} lenNil len_ge
in absurd (minKeyLengthNotZero impossibleLen)
sufficientEntropyNonEmpty (MkSufficientEntropy _ {prf}) | (_ :: _) = IsNonEmpty
-- Reduce toLogSafe s to "***" when the short-path condition holds.
-- Works because toLogSafe is transparent and `if True then x else y = x`.
private
toLogSafeShortEq : (s : String) -> (length s <= 8 = True) -> toLogSafe s = "***"
toLogSafeShortEq s prf with (length s <= 8)
toLogSafeShortEq _ _ | True = Refl
toLogSafeShortEq _ prf | False = absurd prf
-- Reduce toLogSafe s to the concat form when the long-path condition holds.
private
toLogSafeLongEq : (s : String) -> (length s <= 8 = False) ->
toLogSafe s = substr 0 4 s ++ "..." ++ substr (length s `minus` 4) 4 s
toLogSafeLongEq s prf with (length s <= 8)
toLogSafeLongEq _ prf | True = absurd prf
toLogSafeLongEq _ _ | False = Refl
-- Length bound on the redacted-short marker. Lifted out of `logSafeBounded`'s
-- with-block so the elaborator reduces `length "***"` against the witness
-- `LTE 3 11`. Inside the with-block the reduction does not fire on Idris2
-- 0.8.0 (the goal is exposed as
-- `LTE (integerToNat (prim__cast_IntInteger (prim__strLength (if ...))))`
-- and the `if`-arm is not reduced before unification).
private
shortMarkerBounded : LTE (length "***") 11
shortMarkerBounded = LTESucc (LTESucc (LTESucc (LTEZero {right = 8})))
-- Length bound on the long-path ellipsis marker. Same lift-out reason as
-- `shortMarkerBounded`.
private
ellipsisLen3 : LTE (length "...") 3
ellipsisLen3 = LTESucc (LTESucc (LTESucc LTEZero))
-- Short path of `logSafeBounded`, lifted out of the with-block. The return
-- type uses the *literal* `"***"` rather than `toLogSafe s`, which matches
-- what the with-block's True-branch substitution rewrites the goal to.
private
logSafeBoundedShort : (s : String) -> (length s <= 8 = True) ->
LTE (length "***") 11
logSafeBoundedShort _ _ = shortMarkerBounded
-- Long path of `logSafeBounded`, lifted out of the with-block. The return
-- type carries the substr-form directly (matching what the with-block False-
-- branch rewrites the goal to via `if False`-evaluation of `toLogSafe`). The
-- substr arguments are repeated rather than let-bound because Idris2 0.8.0's
-- elaborator doesn't always propagate `let`-binding equalities through
-- `appendLengthSum`'s implicit arguments. The proof is right-associated to
-- match `++`'s associativity (`a ++ b ++ c = a ++ (b ++ c)`).
private
logSafeBoundedLong : (s : String) -> (length s <= 8 = False) ->
LTE (length (substr 0 4 s ++ "..." ++ substr (length s `minus` 4) 4 s)) 11
logSafeBoundedLong s _ =
let l1 : LTE (length (substr 0 4 s)) 4
l1 = substrLengthBound s 0 4
l3 : LTE (length (substr (length s `minus` 4) 4 s)) 4
l3 = substrLengthBound s (length s `minus` 4) 4
-- Length of "..." ++ p3
eq23 : length ("..." ++ substr (length s `minus` 4) 4 s)
= length "..." + length (substr (length s `minus` 4) 4 s)
eq23 = appendLengthSum "..." (substr (length s `minus` 4) 4 s)
step23 : LTE (length "..." + length (substr (length s `minus` 4) 4 s)) (3 + 4)
step23 = plusLteMonotone ellipsisLen3 l3
l23 : LTE (length ("..." ++ substr (length s `minus` 4) 4 s)) 7
l23 = replace {p = \n => LTE n 7} (sym eq23) step23
-- Length of p1 ++ ("..." ++ p3)
eq123 : length (substr 0 4 s ++ "..." ++ substr (length s `minus` 4) 4 s)
= length (substr 0 4 s) + length ("..." ++ substr (length s `minus` 4) 4 s)
eq123 = appendLengthSum (substr 0 4 s) ("..." ++ substr (length s `minus` 4) 4 s)
step123 : LTE (length (substr 0 4 s) + length ("..." ++ substr (length s `minus` 4) 4 s)) (4 + 7)
step123 = plusLteMonotone l1 l23
l123 : LTE (length (substr 0 4 s ++ "..." ++ substr (length s `minus` 4) 4 s)) 11
l123 = replace {p = \n => LTE n 11} (sym eq123) step123
in l123
||| The redacted key from `toLogSafe` is always at most 11 characters long.
|||
||| Proof structure (using `appendLengthSum` + `substrLengthBound` axioms):
||| short path (length s ≤ 8): output is "***", length 3 ≤ 11.
||| long path: 4 + 3 + 4 = 11 ≤ 11.
|||
||| The two string-primitive axioms are declared in SafetyLemmas.
export
logSafeBounded : (s : String) -> LTE (length (toLogSafe s)) 11
logSafeBounded s with (length s <= 8) proof cond
logSafeBounded s | True = logSafeBoundedShort s cond
logSafeBounded s | False = logSafeBoundedLong s cond
--------------------------------------------------------------------------------
-- FFI Bridge Declarations
--------------------------------------------------------------------------------
export
%foreign "C:boj_safety_compare_keys,libbozsafety"
boj_safety_compare_keys : (aPtr : AnyPtr) -> (bPtr : AnyPtr) -> (len : Int) -> Int
export
%foreign "C:boj_safety_check_api_key,libbozsafety"
boj_safety_check_api_key : (ptr : AnyPtr) -> (len : Int) -> Int
export
%foreign "C:boj_safety_mask_key_for_log,libbozsafety"
boj_safety_mask_key_for_log : (inPtr : AnyPtr) -> (inLen : Int) -> (outPtr : AnyPtr) -> Int