Skip to content

Commit 15f4dbf

Browse files
committed
Various hardening
1 parent 67528f6 commit 15f4dbf

File tree

4 files changed

+169
-34
lines changed

4 files changed

+169
-34
lines changed

edr/edrclient.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ def process_sent_message(self, entry):
555555
def noteworthy_about_system(self, fsdjump_event):
556556
if fsdjump_event["SystemSecurity"]:
557557
self.player.location_security(fsdjump_event["SystemSecurity"])
558-
self.edrsystems.system_id(fsdjump_event['StarSystem'], may_create=True, coords=fsdjump_event.get("StarPos", None))
558+
self.edrsystems.system_id(fsdjump_event['StarSystem'], may_create=not self.is_anonymous(), coords=fsdjump_event.get("StarPos", None))
559559
self.edrfactions.process_jump_event(fsdjump_event)
560560
facts = self.edrresourcefinder.assess_jump(fsdjump_event, self.player.inventory)
561561
header = _('Rare materials in {} (USS-HGE/EE, Mission Rewards)').format(fsdjump_event['StarSystem'])
@@ -2138,8 +2138,8 @@ def blip(self, cmdr_name, blip, system_wide=False):
21382138
return its_actually_fine
21392139

21402140
profile = self.cmdr(cmdr_name, check_inara_server=True)
2141-
legal = self.edrlegal.summarize(profile.cid)
21422141
if profile and (self.player.name != cmdr_name) and profile.is_dangerous(self.player.powerplay):
2142+
legal = self.edrlegal.summarize(profile.cid)
21432143
self.status = _("{} is bad news.").format(cmdr_name)
21442144
if self.novel_enough_blip(cmdr_id, blip, cognitive = True, system_wide=system_wide):
21452145
details = [profile.short_profile(self.player.powerplay)]
@@ -2200,9 +2200,9 @@ def scanned(self, cmdr_name, scan):
22002200

22012201
if self.novel_enough_scan(cmdr_id, scan, cognitive = True):
22022202
profile = self.cmdr(cmdr_name, check_inara_server=True)
2203-
legal = self.edrlegal.summarize(profile.cid)
22042203
bounty = EDFineOrBounty(scan["bounty"]) if scan["bounty"] else None
22052204
if profile and (self.player.name != cmdr_name):
2205+
legal = self.edrlegal.summarize(profile.cid)
22062206
if profile.is_dangerous(self.player.powerplay):
22072207
# Translators: this is shown via EDMC's EDR status line upon contact with a known outlaw
22082208
self.status = _("{} is bad news.").format(cmdr_name)

edr/edrcmdrs.py

Lines changed: 109 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from lrucache import LRUCache
77
from edrlog import EDR_LOG
88
from edentities import EDPlayerOne
9-
10-
9+
from edrserver import CommsJammedError
1110

1211

1312
class EDRCmdrs(object):
@@ -66,7 +65,11 @@ def player_pledged_to(self, power, time_pledged=0):
6665
return False
6766
self._player.pledged_to(power, time_pledged)
6867
since = self._player.pledged_since()
69-
return self.server.pledged_to(power, since)
68+
try:
69+
return self.server.pledged_to(power, since)
70+
except CommsJammedError:
71+
EDR_LOG.log(u"Comms jammed: Failed to update pledge status to EDR.", "WARNING")
72+
return False
7073

7174
def __squadron_id(self):
7275
self.__update_squadron_info()
@@ -78,7 +81,13 @@ def __update_squadron_info(self, force_update=False):
7881
return
7982
mark_twain_flag = int((EDTime.js_epoch_now() - self.heartbeat_timestamp)/1000) >= self._edr_heartbeat if self.heartbeat_timestamp else True
8083
if force_update or mark_twain_flag:
81-
info = self.server.heartbeat()
84+
info = None
85+
try:
86+
info = self.server.heartbeat()
87+
except CommsJammedError:
88+
EDR_LOG.log(u"Comms jammed: Failed to get heartbeat from EDR.", "WARNING")
89+
info = None
90+
8291
if info:
8392
self.heartbeat_timestamp = info["heartbeat"] if "heartbeat" in info else EDTime.js_epoch_now()
8493
self._player.squadron_member(info) if "squadronId" in info else self._player.lone_wolf()
@@ -127,9 +136,12 @@ def __edr_cmdr(self, cmdr_name, autocreate):
127136

128137
try:
129138
profile = self.server.cmdr(cmdr_name, autocreate)
130-
except:
131-
EDR_LOG.log("Exception during call to EDR server cmdr.", "WARNING")
132-
pass
139+
except CommsJammedError: # Refined exception handling
140+
EDR_LOG.log("Comms jammed: Failed to fetch cmdr profile from EDR server.", "WARNING")
141+
profile = None
142+
except Exception as e: # Catch other, unexpected exceptions
143+
EDR_LOG.log(f"Unexpected exception during call to EDR server cmdr: {e}", "ERROR")
144+
profile = None
133145

134146
if not profile:
135147
if backup_profile:
@@ -141,11 +153,18 @@ def __edr_cmdr(self, cmdr_name, autocreate):
141153
self.cmdrs_cache.set(cmdr_name.lower(), None)
142154
EDR_LOG.log(u"No match on EDR. Temporary entry to be nice on EDR's server.", "DEBUG")
143155
return None
144-
dex_profile = self.server.cmdrdex(profile.cid)
156+
157+
dex_profile = None
158+
try:
159+
dex_profile = self.server.cmdrdex(profile.cid)
160+
except CommsJammedError:
161+
EDR_LOG.log("Comms jammed: Failed to fetch cmdr dex from EDR server.", "WARNING")
162+
dex_profile = None
163+
145164
if dex_profile:
146-
EDR_LOG.log(u"EDR CmdrDex entry found for {cmdr}: {id}".format(cmdr=cmdr_name,
147-
id=profile.cid), "DEBUG")
165+
EDR_LOG.log(u"EDR CmdrDex entry found for {cmdr}: {id}".format(cmdr=cmdr_name, id=profile.cid), "DEBUG")
148166
profile.dex(dex_profile)
167+
149168
self.cmdrs_cache.set(cmdr_name.lower(), profile)
150169
EDR_LOG.log(u"Cached EDR profile {cmdr}: {id}".format(cmdr=cmdr_name,
151170
id=profile.cid), "DEBUG")
@@ -167,7 +186,13 @@ def __edr_sqdrdex(self, cmdr_name, autocreate):
167186
if not profile:
168187
return None
169188

170-
sqdrdex_dict = self.server.sqdrdex(sqdr_id, profile.cid)
189+
sqdrdex_dict = None
190+
try:
191+
sqdrdex_dict = self.server.sqdrdex(sqdr_id, profile.cid)
192+
except CommsJammedError:
193+
EDR_LOG.log("Comms jammed: Failed to fetch squadron dex from EDR server.", "WARNING")
194+
sqdrdex_dict = None
195+
171196
if sqdrdex_dict:
172197
EDR_LOG.log(u"EDR SqdrDex {sqid} entry found for {cmdr}@{cid}".format(sqid=sqdr_id,
173198
cmdr=cmdr_name, cid=profile.cid
@@ -189,7 +214,11 @@ def __inara_cmdr(self, cmdr_name, check_inara_server):
189214
"DEBUG")
190215
elif check_inara_server:
191216
EDR_LOG.log(u"Stale {} or not cached {} in Inara cache. Inara API call for {}.".format(stale, cached, cmdr_name), "INFO")
192-
inara_profile = self.server.inara_cmdr(cmdr_name)
217+
try: # New try-except for self.server.inara_cmdr(cmdr_name)
218+
inara_profile = self.server.inara_cmdr(cmdr_name)
219+
except CommsJammedError:
220+
EDR_LOG.log("Comms jammed: Failed to fetch Inara profile via EDR server.", "WARNING")
221+
inara_profile = None
193222

194223
if inara_profile and inara_profile.name.lower() == cmdr_name.lower():
195224
self.inara_cache.set(cmdr_name.lower(), inara_profile)
@@ -256,7 +285,11 @@ def tag_cmdr(self, cmdr_name, tag):
256285
return self.__tag_cmdr(cmdr_name, tag)
257286

258287
def contracts(self):
259-
return self.server.contracts()
288+
try:
289+
return self.server.contracts()
290+
except CommsJammedError:
291+
EDR_LOG.log(u"Comms jammed: Failed to get contracts list.", "WARNING")
292+
return None
260293

261294
def contract_for(self, cmdr_name):
262295
if not cmdr_name:
@@ -266,7 +299,11 @@ def contract_for(self, cmdr_name):
266299
if not profile:
267300
return False
268301

269-
return self.server.contract_for(profile.cid)
302+
try:
303+
return self.server.contract_for(profile.cid)
304+
except CommsJammedError:
305+
EDR_LOG.log(u"Comms jammed: Failed to get contract for {}.".format(cmdr_name), "WARNING")
306+
return False
270307

271308
def place_contract(self, cmdr_name, reward):
272309
if not cmdr_name:
@@ -279,7 +316,11 @@ def place_contract(self, cmdr_name, reward):
279316
if not profile:
280317
return False
281318

282-
return self.server.place_contract(profile.cid, {"cname": cmdr_name.lower(), "reward": reward})
319+
try:
320+
return self.server.place_contract(profile.cid, {"cname": cmdr_name.lower(), "reward": reward})
321+
except CommsJammedError:
322+
EDR_LOG.log(u"Comms jammed: Failed to place contract on {}.".format(cmdr_name), "WARNING")
323+
return False
283324

284325
def remove_contract(self, cmdr_name):
285326
if not cmdr_name:
@@ -289,7 +330,11 @@ def remove_contract(self, cmdr_name):
289330
if not profile:
290331
return False
291332

292-
return self.server.remove_contract(profile.cid)
333+
try:
334+
return self.server.remove_contract(profile.cid)
335+
except CommsJammedError:
336+
EDR_LOG.log(u"Comms jammed: Failed to remove contract on {}.".format(cmdr_name), "WARNING")
337+
return False
293338

294339
def __tag_cmdr(self, cmdr_name, tag):
295340
EDR_LOG.log(u"Tagging {} with {}".format(cmdr_name, tag), "DEBUG")
@@ -306,7 +351,14 @@ def __tag_cmdr(self, cmdr_name, tag):
306351

307352
dex_dict = profile.dex_dict()
308353
EDR_LOG.log(u"New dex state: {}".format(dex_dict), "DEBUG")
309-
success = self.server.update_cmdrdex(profile.cid, dex_dict)
354+
355+
success = False
356+
try:
357+
success = self.server.update_cmdrdex(profile.cid, dex_dict)
358+
except CommsJammedError:
359+
EDR_LOG.log(u"Comms jammed: Failed to update EDR Dex for {}.".format(cmdr_name), "WARNING")
360+
success = False
361+
310362
self.evict(cmdr_name)
311363
return success
312364

@@ -333,7 +385,14 @@ def __squadron_tag_cmdr(self, cmdr_name, tag):
333385
augmented_sqdrdex_dict = sqdrdex_dict
334386
augmented_sqdrdex_dict["level"] = self._player.squadron_info()["squadronLevel"]
335387
augmented_sqdrdex_dict["by"] = self._player.name
336-
success = self.server.update_sqdrdex(sqdr_id, profile.cid, augmented_sqdrdex_dict)
388+
389+
success = False # Initialize success before the try block
390+
try:
391+
success = self.server.update_sqdrdex(sqdr_id, profile.cid, augmented_sqdrdex_dict)
392+
except CommsJammedError:
393+
EDR_LOG.log(u"Comms jammed: Failed to update Squadron Dex (tag) for {}.".format(cmdr_name), "WARNING")
394+
success = False
395+
337396
self.evict(cmdr_name)
338397
return success
339398

@@ -353,7 +412,14 @@ def memo_cmdr(self, cmdr_name, memo):
353412
return False
354413

355414
dex_dict = profile.dex_dict()
356-
success = self.server.update_cmdrdex(profile.cid, dex_dict)
415+
416+
success = False # Initialize success
417+
try:
418+
success = self.server.update_cmdrdex(profile.cid, dex_dict)
419+
except CommsJammedError:
420+
EDR_LOG.log(u"Comms jammed: Failed to update EDR Dex (memo) for {}.".format(cmdr_name), "WARNING")
421+
success = False
422+
357423
self.evict(cmdr_name)
358424
return success
359425

@@ -371,7 +437,14 @@ def clear_memo_cmdr(self, cmdr_name):
371437
return False
372438

373439
dex_dict = profile.dex_dict()
374-
success = self.server.update_cmdrdex(profile.cid, dex_dict)
440+
441+
success = False # Initialize success
442+
try:
443+
success = self.server.update_cmdrdex(profile.cid, dex_dict)
444+
except CommsJammedError:
445+
EDR_LOG.log(u"Comms jammed: Failed to update EDR Dex (clear memo) for {}.".format(cmdr_name), "WARNING")
446+
success = False
447+
375448
self.evict(cmdr_name)
376449
return success
377450

@@ -396,7 +469,14 @@ def __untag_cmdr(self, cmdr_name, tag):
396469

397470
dex_dict = profile.dex_dict()
398471
EDR_LOG.log(u"New dex state: {}".format(dex_dict), "DEBUG")
399-
success = self.server.update_cmdrdex(profile.cid, dex_dict)
472+
473+
success = False # Initialize success
474+
try:
475+
success = self.server.update_cmdrdex(profile.cid, dex_dict)
476+
except CommsJammedError:
477+
EDR_LOG.log(u"Comms jammed: Failed to update EDR Dex (untag) for {}.".format(cmdr_name), "WARNING")
478+
success = False
479+
400480
self.evict(cmdr_name)
401481
return success
402482

@@ -423,6 +503,13 @@ def __squadron_untag_cmdr(self, cmdr_name, tag):
423503
augmented_sqdrdex_dict = sqdrdex_dict
424504
augmented_sqdrdex_dict["level"] = self._player.squadron_info()["squadronLevel"]
425505
augmented_sqdrdex_dict["by"] = self._player.name
426-
success = self.server.update_sqdrdex(sqdr_id, profile.cid, augmented_sqdrdex_dict)
506+
507+
success = False # Initialize success
508+
try:
509+
success = self.server.update_sqdrdex(sqdr_id, profile.cid, augmented_sqdrdex_dict)
510+
except CommsJammedError:
511+
EDR_LOG.log(u"Comms jammed: Failed to update Squadron Dex (untag) for {}.".format(cmdr_name), "WARNING")
512+
success = False
513+
427514
self.evict(cmdr_name)
428515
return success

edr/edrserver.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,20 @@ def system(self, star_system, may_create, coords=None):
271271
if not self.__check_response(resp, "EDR", "Systems"):
272272
EDR_LOG.log(u"Failed to create new star system.", "ERROR")
273273
return None
274-
the_system = json.loads(resp.content)
275-
EDR_LOG.log(u"Created system {} in EDR.".format(star_system), "DEBUG")
274+
275+
# --- FIX START ---
276+
# Firebase POST response: {"name": "-<FIREBASE_ID>"}
277+
post_response = json.loads(resp.content)
278+
279+
if "name" in post_response:
280+
# Create the dictionary structure expected by system_id: {<FIREBASE_ID>: <SYSTEM_DATA>}
281+
new_id = post_response["name"]
282+
the_system = {new_id: payload} # Use the ID from the response and the original data
283+
EDR_LOG.log(u"Created system {} in EDR with new ID: {}.".format(star_system, new_id), "DEBUG")
284+
else:
285+
EDR_LOG.log(u"Unexpected response format after system creation.", "ERROR")
286+
return None
287+
# --- FIX END ---
276288
else:
277289
return None
278290
else:
@@ -282,7 +294,7 @@ def system(self, star_system, may_create, coords=None):
282294
EDR_LOG.log(u"System {} has no id={}.".format(star_system, sid), "DEBUG")
283295
return None
284296
EDR_LOG.log(u"System {} is in EDR with id={}.".format(star_system, sid), "DEBUG")
285-
if may_create and coords and "coords" not in the_system[sid]:
297+
if may_create and not self.is_anonymous() and coords and "coords" not in the_system[sid]:
286298
EDR_LOG.log(u"Adding coords to system in EDR.", "DEBUG")
287299
params = { "auth" : self.auth_token() }
288300
payload = {

edr/edrsystems.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,28 +213,64 @@ def set_dlc(self, name):
213213
def system_id(self, star_system, may_create=False, coords=None):
214214
if not star_system:
215215
return None
216+
216217
system = self.systems_cache.get(star_system.lower())
217218
cached = self.systems_cache.has_key(star_system.lower())
218219
if cached and system is None:
219220
EDR_LOG.log(u"Temporary entry for System {} in the cache".format(star_system), "DEBUG")
220221
return None
221222

223+
# Helper function to safely extract and validate the system ID
224+
def get_and_validate_sid(system_dict, star_system_name):
225+
if not system_dict or not isinstance(system_dict, dict):
226+
return None
227+
228+
# The expected ID is the only key
229+
sid = list(system_dict.keys())[0] if system_dict.keys() else None
230+
231+
if not sid:
232+
return None
233+
234+
# 1. Check if the key is the literal system name (an unexpected placeholder)
235+
if sid.lower() == star_system_name.lower():
236+
EDR_LOG.log(u"Rejected potential system ID (matches system name): {}".format(sid), "WARNING")
237+
return None
238+
239+
# 2. Check if the key looks like an internal/common field name
240+
if sid.lower() in ["name", "id", "system"]:
241+
EDR_LOG.log(u"Rejected potential system ID (matches internal field): {}".format(sid), "WARNING")
242+
return None
243+
244+
return sid
245+
222246
if cached and system:
223-
sid = list(system)[0]
224-
if may_create and coords and not "coords" in system[sid]:
247+
sid = get_and_validate_sid(system, star_system)
248+
249+
if not sid: # If validation failed, treat as if system was not found
250+
self.systems_cache.evict(star_system.lower())
251+
EDR_LOG.log(u"Cached system {} had an invalid SID. Evicting cache entry.".format(star_system), "ERROR")
252+
# Fall through to server fetch below
253+
254+
# If sid is valid, continue with the rest of the block
255+
elif may_create and coords and not "coords" in system[sid]:
225256
EDR_LOG.log(u"System {} is in the cache with id={} but missing coords".format(star_system, sid), "DEBUG")
226257
system = self.server.system(star_system, may_create, coords)
227258
if system:
228259
self.systems_cache.set(star_system.lower(), system)
229-
sid = list(system)[0]
260+
sid = list(system)[0]
230261
return sid
231262

263+
# TODO handle commsjammederror... everywhere...
232264
system = self.server.system(star_system, may_create, coords)
233265
if system:
234266
self.systems_cache.set(star_system.lower(), system)
235-
sid = list(system)[0]
236-
EDR_LOG.log(u"Cached {}'s info with id={}".format(star_system, sid), "DEBUG")
237-
return sid
267+
sid = get_and_validate_sid(system, star_system) # Use the validation function
268+
269+
if sid:
270+
EDR_LOG.log(u"Cached {}'s info with id={}".format(star_system, sid), "DEBUG")
271+
return sid
272+
else:
273+
EDR_LOG.log(u"Server returned a system for {} but the ID was invalid.".format(star_system), "ERROR")
238274

239275
self.systems_cache.set(star_system.lower(), None)
240276
EDR_LOG.log(u"No match on EDR. Temporary entry to be nice on EDR's server.", "DEBUG")

0 commit comments

Comments
 (0)