EOSIO / welcome

Documentation that covers EOSIO Overview, Getting Started and Protocol documents
38 stars 54 forks source link

[docs] Adding security check before pushing same params #262

Open abhi3700 opened 4 years ago

abhi3700 commented 4 years ago

In this code, there should be check of params in upsert action before modifying. Otherwise, the action (with same params) will be repeatedly pushed & the entire NET, CPU resources of the user will be completely drained.

... ...

- Complete code of the `upsert` action:
```cpp
[[eosio::action]]
void upsert(
    name user, string first_name, string last_name, string street, string city,
    string state ) {
    require_auth(user);

    address_index addresses(get_first_receiver(), get_first_receiver().value);
    auto it = addresses.find(user.value);

    if(it == addresses.end()) {
        addresses.emplace(user, [&](auto& row) {
            row.key = user;
            row.first_name = first_name;
            row.last_name = last_name;
            row.street = street;
            row.city = city;
            row.state = state;
        });

    }
    else {
        // check whether either of the new data is different
        check((it->first_name != first_name) || (it->last_name != last_name) || (it->street != street) || (it->city != city) || (it->state != state), "At least one of all data must be different.");

        addresses.modify(it, user, [&](auto& row) {
            row.first_name = first_name;
            row.last_name = last_name;
            row.street = street;
            row.city = city;
            row.state = state;
        });

    }
}