You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Data Validation in Juno: Best Practices and Security Considerations
12
+
_Photo by [Johann Walter Bantz](https://unsplash.com/fr/@1walter2)_
13
+
14
+
---
7
15
8
16
## Why Data Validation Matters in Decentralized Apps
9
17
10
18
Data validation is always important. However, web3 comes with its own set of challenges which makes validation an even more important part of building trustworthy apps:
11
19
12
20
1.**No Central Administrator**: Unlike traditional systems, decentralized apps have no admin backdoor to fix data issues
13
-
3.**Limited Data Access**: Developers often can't directly access or examine user data due to encryption and/or privacy
14
-
2.**Data Immutability**: Once written to the blockchain, data can be difficult or impossible to modify
21
+
2.**Limited Data Access**: Developers often can't directly access or examine user data due to encryption and/or privacy
22
+
3.**Data Immutability**: Once written to the blockchain, data can be difficult or impossible to modify
15
23
4.**Client-Side Vulnerability**: Front-end validation can be bypassed by determined users (like in web2)
16
24
5.**Security Risks**: Invalid or malicious data can compromise application integrity and user trust
17
25
18
26
Getting validation right from the start is not just a best practice—it's essential for the secure and reliable operation of your application.
19
27
28
+
---
29
+
20
30
## Available Approaches
21
31
22
32
Juno offers three main approaches for data validation:
`on_set_doc` is a Hook that is triggered after a document has been written to the database. It offers a way to execute custom logic whenever data is added or updated to a collection using the set_doc function.
33
45
34
-
This allows for many use-cases, even for certain types of validation, but this hook runs *after* the data has already been written.
46
+
This allows for many use-cases, even for certain types of validation, but this hook runs _after_ the data has already been written.
35
47
36
48
```rust
37
49
// Example of validation and cleanup in on_set_doc
There are also other Juno hooks, but in general, they provide a way to execute custom logic whenever data is added, modified, or deleted from a Juno datastore collection.
77
88
89
+
---
90
+
78
91
### Custom Endpoints using Serverless Functions
79
92
80
93
Custom Endpoints are Juno serverless functions that expose new API endpoints through Candid (the Internet Computer's interface description language). They provide a validation layer through custom API routes before data reaches Juno's datastore, allowing for complex multi-step operations with custom validation logic.
description:None, // Optional field for filtering/searching
115
128
version:None// None for new docs, Some(version) for updates
116
129
};
117
-
130
+
118
131
// Use set_doc_store to save the document
119
132
// This is Juno's low-level storage function that:
120
133
// 1. Takes ownership of the document (caller's Principal)
@@ -147,6 +160,8 @@ The common workaround is to restrict the datastore collection to "controller" ac
147
160
- Requires building a custom permission system from scratch
148
161
- Splits validation logic from data storage
149
162
163
+
---
164
+
150
165
### assert_set_doc Hooks (Recommended)
151
166
152
167
The `assert_set_doc` hook runs BEFORE any data is written to the database, allowing you to validate and reject invalid submissions immediately. This is the most secure validation method in Juno as it integrates directly with the core data storage mechanism.
Remember: Security is about preventing unauthorized or invalid operations, not just making them difficult. assert_set_doc hooks provide the only guaranteed way to validate all data operations in Juno's Datastore.
459
482
483
+
---
484
+
460
485
## Reference: Available Juno Hooks and Context Types
461
486
462
487
This section provides a comprehensive reference of all available Juno hooks and their corresponding context types.
@@ -504,6 +529,8 @@ use junobuild_satellite::{
504
529
};
505
530
```
506
531
532
+
---
533
+
507
534
### Where to find the hooks and assertions in your project
508
535
509
536
When you run `juno dev eject`, all available hooks and assertions are scaffolded in your `lib.rs` module. However, you can selectively enable only the features you need by disabling default features in your `Cargo.toml` and explicitly specifying the ones you want to use.
@@ -514,5 +541,3 @@ Example configuration for using only `on_set_doc` and `assert_set_doc`:
514
541
[dependencies]
515
542
junobuild-satellite = { version = "0.0.21", default-features = false, features = ["on_set_doc", "assert_set_doc"] }
0 commit comments