Skip to content

Commit 551a48b

Browse files
authored
Merge pull request #59 from mpretty-cyro/fix/legacy-blinding-flag
Removed the `legacy_blinding` flag from blinded contacts
2 parents bd86254 + 1004da7 commit 551a48b

File tree

7 files changed

+168
-76
lines changed

7 files changed

+168
-76
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if(CCACHE_PROGRAM)
1717
endif()
1818

1919
project(libsession-util
20-
VERSION 1.5.2
20+
VERSION 1.5.3
2121
DESCRIPTION "Session client utility library"
2222
LANGUAGES ${LANGS})
2323

include/session/config/contacts.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,13 @@ LIBSESSION_EXPORT contacts_blinded_contact_list* contacts_blinded(const config_o
272272
/// Inputs:
273273
/// - `conf` -- [in] Pointer to the config object
274274
/// - `blinded_id` -- [in] null terminated hex string
275-
/// - `legacy_blinding` -- [in] null terminated hex string
276275
/// - `blinded_contact` -- [out] the blinded contact info data
277276
///
278277
/// Output:
279278
/// - `bool` -- Returns true if blinded contact exists
280279
LIBSESSION_EXPORT bool contacts_get_blinded(
281280
config_object* conf,
282281
const char* blinded_id,
283-
bool legacy_blinding,
284282
contacts_blinded_contact* blinded_contact) LIBSESSION_WARN_UNUSED;
285283

286284
/// API: contacts/contacts_get_or_construct_blinded
@@ -301,7 +299,6 @@ LIBSESSION_EXPORT bool contacts_get_blinded(
301299
/// [in] const char* community_base_url,
302300
/// [in] const char* community_pubkey_hex,
303301
/// [in] const char* blinded_id,
304-
/// [in] bool legacy_blinding,
305302
/// [out] contacts_blinded_contact* blinded_contact
306303
/// );
307304
/// ```
@@ -311,7 +308,6 @@ LIBSESSION_EXPORT bool contacts_get_blinded(
311308
/// - `community_base_url` -- [in] null terminated string
312309
/// - `community_pubkey_hex` -- [in] null terminated hex string
313310
/// - `blinded_id` -- [in] null terminated hex string
314-
/// - `legacy_blinding` -- [in] null terminated hex string
315311
/// - `blinded_contact` -- [out] the blinded contact info data
316312
///
317313
/// Output:
@@ -321,7 +317,6 @@ LIBSESSION_EXPORT bool contacts_get_or_construct_blinded(
321317
const char* community_base_url,
322318
const char* community_pubkey_hex,
323319
const char* blinded_id,
324-
bool legacy_blinding,
325320
contacts_blinded_contact* blinded_contact) LIBSESSION_WARN_UNUSED;
326321

327322
/// API: contacts/contacts_set_blinded
@@ -355,8 +350,7 @@ LIBSESSION_EXPORT bool contacts_set_blinded(
355350
/// BOOL contacts_erase_blinded(
356351
/// [in, out] config_object* conf,
357352
/// [in] const char* community_base_url,
358-
/// [in] const char* blinded_id,
359-
/// [in] bool legacy_blinding
353+
/// [in] const char* blinded_id
360354
/// );
361355
/// ```
362356
///
@@ -370,10 +364,7 @@ LIBSESSION_EXPORT bool contacts_set_blinded(
370364
/// Outputs:
371365
/// - `bool` -- True if erasing was successful
372366
LIBSESSION_EXPORT bool contacts_erase_blinded_contact(
373-
config_object* conf,
374-
const char* community_base_url,
375-
const char* blinded_id,
376-
bool legacy_blinding);
367+
config_object* conf, const char* community_base_url, const char* blinded_id);
377368

378369
typedef struct contacts_iterator {
379370
void* _internals;

include/session/config/contacts.hpp

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ struct contact_info {
119119
};
120120

121121
struct blinded_contact_info {
122-
community comm;
123-
124122
const std::string session_id() const; // in hex
125123
std::string name;
126124
profile_pic profile_picture;
@@ -131,8 +129,7 @@ struct blinded_contact_info {
131129
explicit blinded_contact_info(
132130
std::string_view community_base_url,
133131
std::span<const unsigned char> community_pubkey,
134-
std::string_view blinded_id,
135-
bool legacy_blinding);
132+
std::string_view blinded_id);
136133

137134
// Internal ctor/method for C API implementations:
138135
blinded_contact_info(const struct contacts_blinded_contact& c); // From c struct
@@ -154,17 +151,51 @@ struct blinded_contact_info {
154151
/// - `name` -- Name to assign to the contact
155152
void set_name(std::string name);
156153

154+
/// API: contacts/blinded_contact_info::community_base_url
155+
///
156+
/// Accesses the base url for the community (i.e. not including room or pubkey). Always
157+
/// lower-case/normalized.
158+
///
159+
/// Inputs: None
160+
///
161+
/// Outputs:
162+
/// - `const std::string&` -- Returns the base url
163+
const std::string& community_base_url() const { return comm.base_url(); }
164+
165+
/// API: contacts/blinded_contact_info::community_pubkey
166+
///
167+
/// Accesses the community server pubkey (32 bytes).
168+
///
169+
/// Inputs: None
170+
///
171+
/// Outputs:
172+
/// - `const std::vector<unsigned char>&` -- Returns the pubkey
173+
const std::vector<unsigned char>& community_pubkey() const { return comm.pubkey(); }
174+
175+
/// API: contacts/blinded_contact_info::community_pubkey_hex
176+
///
177+
/// Accesses the community server pubkey as hex (64 hex digits).
178+
///
179+
/// Inputs: None
180+
///
181+
/// Outputs:
182+
/// - `std::string` -- Returns the pubkey
183+
std::string community_pubkey_hex() const { return comm.pubkey_hex(); }
184+
185+
private:
186+
friend class Contacts;
187+
friend struct session::config::comm_iterator_helper;
188+
189+
community comm;
190+
191+
void load(const dict& info_dict);
192+
157193
/// These functions are here so we can use the `comm_iterator_helper` for loading data
158194
/// into this struct
159195
void set_base_url(std::string_view base_url);
160196
void set_room(std::string_view room);
161197
void set_pubkey(std::span<const unsigned char> pubkey);
162198
void set_pubkey(std::string_view pubkey);
163-
164-
private:
165-
friend class Contacts;
166-
friend struct session::config::comm_iterator_helper;
167-
void load(const dict& info_dict);
168199
};
169200

170201
class Contacts : public ConfigBase {
@@ -444,13 +475,11 @@ class Contacts : public ConfigBase {
444475
///
445476
/// Inputs:
446477
/// - `blinded_id_hex` -- hex string of the session id
447-
/// - `legacy_blinding` -- flag indicating whether the pubkey is using legacy blinding
448478
///
449479
/// Outputs:
450480
/// - `std::optional<blinded_contact_info>` - Returns nullopt if blinded session ID was not
451481
/// found, otherwise a filled out blinded_contact_info
452-
std::optional<blinded_contact_info> get_blinded(
453-
std::string_view blinded_id_hex, bool legacy_blinding) const;
482+
std::optional<blinded_contact_info> get_blinded(std::string_view blinded_id_hex) const;
454483

455484
/// API: contacts/Contacts::get_or_construct_blinded
456485
///
@@ -468,15 +497,13 @@ class Contacts : public ConfigBase {
468497
/// - `community_pubkey_hex` -- Hex string of the public key for the community this blinded id
469498
/// originates from
470499
/// - `blinded_id_hex` -- hex string of the blinded id
471-
/// - `legacy_blinding` -- flag indicating whether the pubkey is using legacy blinding
472500
///
473501
/// Outputs:
474502
/// - `blinded_contact_info` - Returns a filled out blinded_contact_info
475503
blinded_contact_info get_or_construct_blinded(
476504
std::string_view community_base_url,
477505
std::string_view community_pubkey_hex,
478-
std::string_view blinded_id_hex,
479-
bool legacy_blinding);
506+
std::string_view blinded_id_hex);
480507

481508
/// API: contacts/contacts::set_blinded
482509
///
@@ -503,12 +530,10 @@ class Contacts : public ConfigBase {
503530
/// Inputs:
504531
/// - `base_url` -- the base url for the community this blinded contact originated from
505532
/// - `blinded_id` -- hex string of the blinded id
506-
/// - `legacy_blinding` -- flag indicating whether `blinded_id` is using legacy blinding
507533
///
508534
/// Outputs:
509535
/// - `bool` - Returns true if contact was found and removed, false otherwise
510-
bool erase_blinded(
511-
std::string_view base_url, std::string_view blinded_id, bool legacy_blinding);
536+
bool erase_blinded(std::string_view base_url, std::string_view blinded_id);
512537

513538
struct iterator;
514539
/// API: contacts/contacts::begin

src/config/contacts.cpp

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <variant>
1111

1212
#include "internal.hpp"
13+
#include "session/blinding.hpp"
1314
#include "session/config/contacts.h"
1415
#include "session/config/error.h"
1516
#include "session/export.h"
@@ -308,12 +309,16 @@ size_t Contacts::size() const {
308309
blinded_contact_info::blinded_contact_info(
309310
std::string_view community_base_url,
310311
std::span<const unsigned char> community_pubkey,
311-
std::string_view blinded_id,
312-
bool legacy_blinding) :
312+
std::string_view blinded_id) :
313313
comm{community(
314-
std::move(community_base_url), blinded_id.substr(2), std::move(community_pubkey))},
315-
legacy_blinding{legacy_blinding} {
316-
check_session_id(blinded_id, legacy_blinding ? "15" : "25");
314+
std::move(community_base_url), blinded_id.substr(2), std::move(community_pubkey))} {
315+
auto prefix = get_session_id_prefix(blinded_id);
316+
legacy_blinding = (prefix == session::SessionIDPrefix::community_blinded_legacy);
317+
318+
if (prefix != session::SessionIDPrefix::community_blinded &&
319+
prefix != session::SessionIDPrefix::community_blinded_legacy)
320+
throw std::invalid_argument{
321+
"Invalid blinded ID: Expected '15' or '25' prefix; got " + std::string{blinded_id}};
317322
}
318323

319324
void blinded_contact_info::load(const dict& info_dict) {
@@ -378,6 +383,10 @@ void blinded_contact_info::set_base_url(std::string_view base_url) {
378383
}
379384

380385
void blinded_contact_info::set_room(std::string_view room) {
386+
if (room.size() != 64 || !oxenc::is_hex(room))
387+
throw std::invalid_argument{
388+
fmt::format("Invalid room: expected 64 hex digits; got {}", room)};
389+
381390
comm.set_room(room);
382391
}
383392

@@ -403,9 +412,8 @@ ConfigBase::DictFieldProxy Contacts::blinded_contact_field(
403412

404413
using any_blinded_contact = std::variant<blinded_contact_info>;
405414

406-
std::optional<blinded_contact_info> Contacts::get_blinded(
407-
std::string_view blinded_id_hex, bool legacy_blinding) const {
408-
check_session_id(blinded_id_hex, legacy_blinding ? "15" : "25");
415+
std::optional<blinded_contact_info> Contacts::get_blinded(std::string_view blinded_id_hex) const {
416+
get_session_id_prefix(blinded_id_hex);
409417

410418
if (auto* b = data["b"].dict()) {
411419
auto comm = comm_iterator_helper{b->begin(), b->end()};
@@ -426,16 +434,12 @@ std::optional<blinded_contact_info> Contacts::get_blinded(
426434
blinded_contact_info Contacts::get_or_construct_blinded(
427435
std::string_view community_base_url,
428436
std::string_view community_pubkey_hex,
429-
std::string_view blinded_id_hex,
430-
bool legacy_blinding) {
431-
if (auto maybe = get_blinded(blinded_id_hex, legacy_blinding))
437+
std::string_view blinded_id_hex) {
438+
if (auto maybe = get_blinded(blinded_id_hex))
432439
return *std::move(maybe);
433440

434441
return blinded_contact_info{
435-
community_base_url,
436-
to_span(oxenc::from_hex(community_pubkey_hex)),
437-
blinded_id_hex,
438-
legacy_blinding};
442+
community_base_url, to_span(oxenc::from_hex(community_pubkey_hex)), blinded_id_hex};
439443
}
440444

441445
std::vector<blinded_contact_info> Contacts::blinded() const {
@@ -475,9 +479,13 @@ void Contacts::set_blinded(const blinded_contact_info& bc) {
475479
set_ts(info["j"], bc.created);
476480
}
477481

478-
bool Contacts::erase_blinded(
479-
std::string_view base_url_, std::string_view blinded_id, bool legacy_blinding) {
480-
check_session_id(blinded_id, legacy_blinding ? "15" : "25");
482+
bool Contacts::erase_blinded(std::string_view base_url_, std::string_view blinded_id) {
483+
auto prefix = get_session_id_prefix(blinded_id);
484+
485+
if (prefix != session::SessionIDPrefix::community_blinded &&
486+
prefix != session::SessionIDPrefix::community_blinded_legacy)
487+
throw std::invalid_argument{
488+
"Invalid blinded ID: Expected '15' or '25' prefix; got " + std::string{blinded_id}};
481489

482490
auto base_url = community::canonical_url(base_url_);
483491
auto pk = std::string(blinded_id.substr(2));
@@ -588,14 +596,11 @@ LIBSESSION_C_API size_t contacts_size(const config_object* conf) {
588596
}
589597

590598
LIBSESSION_C_API bool contacts_get_blinded(
591-
config_object* conf,
592-
const char* blinded_id,
593-
bool legacy_blinding,
594-
contacts_blinded_contact* blinded_contact) {
599+
config_object* conf, const char* blinded_id, contacts_blinded_contact* blinded_contact) {
595600
return wrap_exceptions(
596601
conf,
597602
[&] {
598-
if (auto bc = unbox<Contacts>(conf)->get_blinded(blinded_id, legacy_blinding)) {
603+
if (auto bc = unbox<Contacts>(conf)->get_blinded(blinded_id)) {
599604
bc->into(*blinded_contact);
600605
return true;
601606
}
@@ -609,17 +614,13 @@ LIBSESSION_C_API bool contacts_get_or_construct_blinded(
609614
const char* community_base_url,
610615
const char* community_pubkey_hex,
611616
const char* blinded_id,
612-
bool legacy_blinding,
613617
contacts_blinded_contact* blinded_contact) {
614618
return wrap_exceptions(
615619
conf,
616620
[&] {
617621
unbox<Contacts>(conf)
618622
->get_or_construct_blinded(
619-
community_base_url,
620-
community_pubkey_hex,
621-
blinded_id,
622-
legacy_blinding)
623+
community_base_url, community_pubkey_hex, blinded_id)
623624
.into(*blinded_contact);
624625
return true;
625626
},
@@ -682,13 +683,9 @@ LIBSESSION_C_API bool contacts_set_blinded(
682683
}
683684

684685
LIBSESSION_C_API bool contacts_erase_blinded(
685-
config_object* conf,
686-
const char* community_base_url,
687-
const char* blinded_id,
688-
bool legacy_blinding) {
686+
config_object* conf, const char* community_base_url, const char* blinded_id) {
689687
try {
690-
return unbox<Contacts>(conf)->erase_blinded(
691-
community_base_url, blinded_id, legacy_blinding);
688+
return unbox<Contacts>(conf)->erase_blinded(community_base_url, blinded_id);
692689
} catch (...) {
693690
return false;
694691
}

src/config/internal.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "internal.hpp"
22

3+
#include <fmt/ranges.h>
34
#include <oxenc/base32z.h>
45
#include <oxenc/base64.h>
56
#include <oxenc/bt_value_producer.h>
@@ -8,9 +9,24 @@
89

910
#include <iterator>
1011
#include <optional>
12+
#include <oxen/log/format.hpp>
13+
14+
using namespace oxen::log::literals;
1115

1216
namespace session::config {
1317

18+
namespace {
19+
20+
constexpr std::array all_session_id_prefixes = {
21+
session::SessionIDPrefix::standard,
22+
session::SessionIDPrefix::group,
23+
session::SessionIDPrefix::community_blinded_legacy,
24+
session::SessionIDPrefix::community_blinded,
25+
session::SessionIDPrefix::version_blinded,
26+
session::SessionIDPrefix::unblinded};
27+
28+
} // namespace
29+
1430
void check_session_id(std::string_view session_id, std::string_view prefix) {
1531
if (!(session_id.size() == 64 + prefix.size() && oxenc::is_hex(session_id) &&
1632
session_id.substr(0, prefix.size()) == prefix))
@@ -19,6 +35,24 @@ void check_session_id(std::string_view session_id, std::string_view prefix) {
1935
"; got " + std::string{session_id}};
2036
}
2137

38+
SessionIDPrefix get_session_id_prefix(std::string_view id) {
39+
if (oxenc::is_hex(id) && id.size() == 66) {
40+
for (auto prefix : all_session_id_prefixes) {
41+
auto prefix_str = to_string(prefix);
42+
43+
if ((id.size() == 64 + prefix_str.size() &&
44+
id.substr(0, prefix_str.size()) == prefix_str))
45+
return prefix;
46+
}
47+
}
48+
49+
// If we get here then the id wasn't any of the currently defined prefixes
50+
throw std::invalid_argument{fmt::format(
51+
"Invalid session ID: expected 66 hex digits starting with one of [{}]; got {}",
52+
fmt::join(all_session_id_prefixes, ", "),
53+
id)};
54+
}
55+
2256
std::string session_id_to_bytes(std::string_view session_id, std::string_view prefix) {
2357
check_session_id(session_id, prefix);
2458
return oxenc::from_hex(session_id);

0 commit comments

Comments
 (0)