Commit d4842cf
authored
security: fix inverted fundraiser deadline logic in token-fundraiser (#674)
* security: fix inverted fundraiser deadline logic in token-fundraiser
contribute.rs and refund.rs both gated their time check on the wrong
side of the comparison. contribute() required duration <= elapsed_days
to pass - contributions only succeeded *after* the fundraiser's
duration had already elapsed, and stayed open forever after that.
refund() required duration >= elapsed_days - refunds only succeeded
*before* the deadline.
Net effect: with any realistic nonzero duration, contribute() and
refund() are live in mutually-exclusive, backwards time windows.
Nobody can contribute while the fundraiser claims to be running; once
contributions start working (only possible post-deadline), refund is
permanently blocked. If the target isn't met, funds sit in the vault
with no instruction able to move them - check_contributions requires
the target met, refund now requires "not yet past deadline" which is
false by construction. A fund-lock, not a theft, but a complete break
of the contract's core guarantee.
Confirmed against the example's own README, whose prose states the
correct intent while the code implemented the opposite - and whose
refund code snippet already had the correct form, meaning refund.rs
itself had drifted from the documented behavior, not the reverse.
Fixed with a two-character flip per file: contribute.rs now requires
duration > elapsed (window still open); refund.rs now requires
duration <= elapsed (window has closed), matching the README exactly.
Also fixed the one inverted README snippet (contribute section; the
refund section was already correct) and the misleading comments above
both checks.
Both test suites previously called initialize(..., 0) - duration=0
made elapsed_days >= 0 trivially satisfy both inverted checks at once,
completely masking the bug. Updated both to a realistic duration=1 and
added adversarial coverage: tests/litesvm.test.ts (which can warp its
own clock) now proves contribute() correctly rejects at the exact
deadline boundary and refund() correctly succeeds once past it, with
balance/account-closure assertions on the happy path.
tests/fundraiser.ts (real validator, no way to fast-forward its clock)
proves refund() is correctly rejected while still active. Verified the
new tests actually fail against the pre-fix code (multiple independent
failure signals, including a direct "succeeded when it should have
rejected" repro at the boundary) and pass after.
Out of scope, called out for follow-up: a possibly-negative elapsed
value getting cast to u16 (wraps on a backwards clock - the fix widens
nothing, kept as a 2-line diff instead of adding saturating_sub
hardening); refund.rs/checker.rs gating on the vault's live token
balance rather than the tracked current_amount, letting anyone grief
the target check by transferring tokens into the vault directly; and
check_contributions closing the Fundraiser PDA while Contributor PDAs
may still be open, stranding their rent. All separate findings from
this time-gate inversion.
* fix(#674): address dev-jodee review - shared test util, trim comments
1. Extracted the duplicated expectAnchorError helper from both test
files into a shared tests/utils.ts.
2. Trimmed the explanatory comments across both test files - several
ran 5-13 lines to explain a single design decision. Kept the one
load-bearing sentence per comment, cut the rest.1 parent 4120f25 commit d4842cf
6 files changed
Lines changed: 161 additions & 80 deletions
File tree
- tokens/token-fundraiser/anchor
- programs/fundraiser/src/instructions
- tests
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
| 71 | + | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| |||
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
58 | 58 | | |
59 | | - | |
| 59 | + | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
212 | 212 | | |
213 | 213 | | |
214 | 214 | | |
215 | | - | |
| 215 | + | |
216 | 216 | | |
217 | 217 | | |
218 | | - | |
| 218 | + | |
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| |||
78 | 80 | | |
79 | 81 | | |
80 | 82 | | |
| 83 | + | |
| 84 | + | |
81 | 85 | | |
82 | | - | |
| 86 | + | |
83 | 87 | | |
84 | 88 | | |
85 | 89 | | |
| |||
145 | 149 | | |
146 | 150 | | |
147 | 151 | | |
148 | | - | |
149 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
150 | 155 | | |
151 | | - | |
| 156 | + | |
| 157 | + | |
152 | 158 | | |
153 | 159 | | |
154 | 160 | | |
| |||
159 | 165 | | |
160 | 166 | | |
161 | 167 | | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
174 | | - | |
175 | | - | |
| 174 | + | |
| 175 | + | |
176 | 176 | | |
177 | | - | |
| 177 | + | |
| 178 | + | |
178 | 179 | | |
179 | 180 | | |
180 | 181 | | |
| |||
186 | 187 | | |
187 | 188 | | |
188 | 189 | | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
198 | 193 | | |
199 | 194 | | |
200 | | - | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
201 | 199 | | |
| 200 | + | |
202 | 201 | | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
221 | 220 | | |
222 | | - | |
223 | | - | |
224 | | - | |
| 221 | + | |
| 222 | + | |
225 | 223 | | |
226 | 224 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
| 22 | + | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
| |||
78 | 81 | | |
79 | 82 | | |
80 | 83 | | |
| 84 | + | |
| 85 | + | |
81 | 86 | | |
82 | | - | |
| 87 | + | |
83 | 88 | | |
84 | 89 | | |
85 | 90 | | |
| |||
143 | 148 | | |
144 | 149 | | |
145 | 150 | | |
146 | | - | |
147 | | - | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
148 | 154 | | |
149 | | - | |
| 155 | + | |
| 156 | + | |
150 | 157 | | |
151 | 158 | | |
152 | 159 | | |
| |||
156 | 163 | | |
157 | 164 | | |
158 | 165 | | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
| 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 | + | |
168 | 197 | | |
169 | 198 | | |
170 | 199 | | |
171 | | - | |
172 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
173 | 203 | | |
174 | | - | |
| 204 | + | |
| 205 | + | |
175 | 206 | | |
176 | 207 | | |
177 | 208 | | |
| |||
182 | 213 | | |
183 | 214 | | |
184 | 215 | | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
| 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 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
194 | 252 | | |
195 | 253 | | |
196 | 254 | | |
| 255 | + | |
197 | 256 | | |
198 | 257 | | |
199 | 258 | | |
200 | 259 | | |
201 | 260 | | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
202 | 264 | | |
203 | 265 | | |
204 | 266 | | |
| |||
216 | 278 | | |
217 | 279 | | |
218 | 280 | | |
219 | | - | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
220 | 289 | | |
221 | 290 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
0 commit comments