Skip to content

Commit

Permalink
event sent by authorized local user is considered trusted
Browse files Browse the repository at this point in the history
  • Loading branch information
ondra-novak committed Aug 18, 2023
1 parent c833dc7 commit e68db9c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/nostr_server/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,9 @@ cocls::future<bool> App::send_simple_stats(coroserver::http::ServerRequest &req)
}

bool App::is_home_user(const Event::Pubkey &pubkey) const {
auto fnd = _index_replaceable.find({pubkey, static_cast<unsigned int>(0),std::string_view()});
return fnd;
auto r = _index_whitelist.find(pubkey);
if (!r) return false;
return r->local;
}


Expand Down
7 changes: 6 additions & 1 deletion src/nostr_server/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ struct Event {
ID id;
Pubkey author;
Signature sig;
///this event contains binary data NIP97 (search 'x' tag for hash)
bool nip97 = false;
///this event was sent by someone who was authorized using AUTH and was found as local user
bool trusted = false;
///contains reference depth -
Depth ref_level = 0;

static Event fromJSON(std::string_view json_text);
Expand Down Expand Up @@ -97,7 +101,7 @@ struct EventDocument {
*out = static_cast<char>(evatt.index());
if (std::holds_alternative<Event>(evatt)) {
const Event &ev = std::get<Event>(evatt);
out = Srl::string_to_binary(ev.nip97?0x80:0,ev.content,out);
out = Srl::string_to_binary((ev.nip97?0x80:0)|(ev.trusted?0x40:0),ev.content,out);
out = Srl::uint_to_binary(0,ev.kind,out);
out = Srl::uint_to_binary(0,ev.created_at, out);
out = Srl::uint_to_binary(0,ev.tags.size(),out);
Expand Down Expand Up @@ -139,6 +143,7 @@ struct EventDocument {
auto x = get_extra(at,end);
ev.content = Srl::string_from_binary(x, at, end);
ev.nip97 = (x & 0x80) != 0;
ev.trusted = (x & 0x40) != 0;
x = get_extra(at,end);
ev.kind = Srl::uint_from_binary(x,at,end);
x = get_extra(at,end);
Expand Down
5 changes: 4 additions & 1 deletion src/nostr_server/peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ void Peer::on_event_generic(const JSON &msg, Fn &&on_verify, bool no_special_eve
if (!_secp.has_value()) {
_secp.emplace();
}
if (_authent && _app->is_home_user(_auth_pubkey)) {
event.trusted = true;
}
if (!event.verify(*_secp)) {
throw std::invalid_argument("Signature verification failed");
}
Expand All @@ -229,7 +232,7 @@ void Peer::on_event_generic(const JSON &msg, Fn &&on_verify, bool no_special_eve
if (!_no_limit && _options.read_only /*&& _options.replicators.find(pubkey) == _options.replicators.npos*/) {
throw Blocked("Sorry, server is in read_only mode");
}
if (!_no_limit && _options.whitelisting) {
if (!_no_limit && _options.whitelisting && !event.trusted) {
if (!_app->check_whitelist(event.author)) {
if (k == kind::Encrypted_Direct_Messages || k == kind::Gift_Wrap_Event) { //receiver must be a local user
auto target = event.get_tag_content("p");
Expand Down
2 changes: 1 addition & 1 deletion src/nostr_server/whitelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct KarmaDocument {
};

struct WhiteListIndexFn {
static constexpr int revision = 4;
static constexpr int revision = 5;
template<typename Emit>
void operator ()(Emit emit, const EventOrAttachment &evatt) const;
};
Expand Down
1 change: 1 addition & 0 deletions src/nostr_server/whitelist_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void WhiteListIndexFn::operator ()(Emit emit, const EventOrAttachment &evatt) co
case kind::Contacts: update_counter(&Karma::followers);break;
case kind::Encrypted_Direct_Messages: update_counter(&Karma::directmsgs);break;
case kind::Mute_List: update_counter(&Karma::mutes);break;
case kind::Gift_Wrap_Event: if (ev.trusted) update_counter(&Karma::directmsgs);break;
default:break;
}
}
Expand Down

0 comments on commit e68db9c

Please sign in to comment.