Skip to content

Commit

Permalink
Add sanitization of config
Browse files Browse the repository at this point in the history
  • Loading branch information
daviesgeek committed Aug 27, 2024
1 parent bb1b494 commit d6f0cac
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 2 deletions.
96 changes: 94 additions & 2 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,100 @@ async def stats(request: Request) -> JSONResponse:

@app.get("/v1/server/config")
async def server_config(request: Request) -> JSONResponse:
# TODO: Sanitize config (i.e. username, password, api_key, etc)
return jsonable_encoder({ "config": self.config })
# # TODO: Sanitize config (i.e. username, password, api_key, etc)
# sanitized_config = self.config
# del sanitized_config['broker']['host']
# del sanitized_config['broker']['port']
# del sanitized_config['broker']['username']
# del sanitized_config['broker']['password']

# del sanitized_config['channels']['encryption_key']

# del sanitized_config['paths']

# sanitized_config['integrations']['discord'] = {
# 'enabled': self.config['integrations']['discord']['enabled'],
# }
# sanitized_config['integrations']['geocoding'] = {
# 'enabled': self.config['integrations']['geocoding']['enabled'],
# 'provider': self.config['integrations']['geocoding']['provider'],
# }

whitelist = {
"config": {
"mesh": {
"name": {},
"shortname": {},
"description": {},
"url": {},
"contact": {},
"country": {},
"region": {},
"metro": {},
"latitude": {},
"longitude": {},
"altitude": {},
"timezone": {},
"announce": {
"enabled": {},
"interval": {},
},
"tools": {
"name": {},
"url": {},
},
},
"broker": {
"enabled": {},
"host": {},
"client_id_prefix": {},
"topics": {},
"decoders": {
"protobuf": {
"enabled": {},
},
"json": {
"enabled": {},
}
},

},
"channels": {
"display": {}
},
"server": {
"node_id": {},
"base_url": {},
"node_activity_prune_threshold": {},
"timezone": {},
"intervals": {
"data_save": {},
"render": {},
},
"enrich": {
"enabled": {},
"interval": {},
"provider": {}
},
"graph": {
"enabled": {},
"max_depth": {},
},
"start_time": {}
},
"integrations": {
"discord": {
"enabled": {}
},
"geocoding": {
"enabled": {},
"provider": {}
}
}
}
}

return jsonable_encoder(utils.filter_dict({'config': self.config}, whitelist))

conf = uvicorn.Config(app=app, host="0.0.0.0", port=9000, loop=loop)
server = uvicorn.Server(conf)
Expand Down
22 changes: 22 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,25 @@ def geocode_position(api_key: str, latitude: float, longitude: float):
return None
print(f"Geocoded {latitude}, {longitude} to {response.json()}")
return response.json()

def filter_dict(d, whitelist):
"""
Recursively filter a dictionary to only include whitelisted keys.
:param d: The original dictionary or list.
:param whitelist: A dictionary that mirrors the structure of `d` with the keys you want to keep.
Nested dictionaries and lists should be specified with the keys you want to retain.
:return: A new dictionary or list containing only the whitelisted keys.
"""
if isinstance(d, dict):
return {
key: filter_dict(d[key], whitelist[key]) if isinstance(d[key], (dict, list)) else d[key]
for key in whitelist if key in d
}
elif isinstance(d, list):
return [
filter_dict(item, whitelist) if isinstance(item, dict) else item
for item in d
]
else:
return d # Return the value if it's neither a dict nor a list

0 comments on commit d6f0cac

Please sign in to comment.