Repository navigation
Fix create_session API endpoint and perform housekeeping on murfey.server.api.session_info - #906
Conversation
…s to it through JSON data payload instead
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #906 +/- ##
==========================================
+ Coverage 55.35% 55.43% +0.07%
==========================================
Files 103 103
Lines 11632 11636 +4
Branches 1546 1546
==========================================
+ Hits 6439 6450 +11
+ Misses 4844 4837 -7
Partials 349 349 🚀 New features to boost your workflow:
|
…rides are only implemented when needed by the router; used a different visit name for the test
create_session API endpoint and perform housekeeping on murfey.server.api.session_info
| name=name, | ||
| visit=visit, | ||
| session = MurfeyDB.Session( | ||
| name=session_info.name, |
There was a problem hiding this comment.
name definitely needs sanitising - I'd suggest stripping out anything except standard alphanumeric and _ or -
There was a problem hiding this comment.
Ok, I've tacked on the sanitise() function to both of them.
There was a problem hiding this comment.
We've applied sanitisation, albeit slightly milder. Besides alphanumerics, hyphens, and underscores, the character allow list is expanded to also include:
- spaces
- forward slashes
- round/square brackets
- periods
- commas
- ampersands
- pluses
- single/double quotes
Because we're not writing an SQL statement directly, but rather inserting it using the sqlalchemy and sqlmodel functions, the insert will be safe.
| visit=visit, | ||
| session = MurfeyDB.Session( | ||
| name=session_info.name, | ||
| visit=session_info.visit, |
There was a problem hiding this comment.
probably also worth sanitising the visit in the same way
There was a problem hiding this comment.
We've decided to sanitise the visit thoroughly, so that only alphanumerics, underscores, and hyphens are retained.
NOTE: This PR will need to be deployed alongside DiamondLightSource/murfey-frontend#88
The
create_sessionFastAPI endpoint previously parsed thevisit(e.g. 'cm12345-6') andname(an optional long-form description) directly from the URL path. This is unsafe, as the presence of characters such as '/' in the parametrers will break the URL construction logic.This PR fixes that by passing the
visitandnameas part of the JSON data payload instead. The API endpoint path was also changed now thatvisitandnameare no longer needed as URL path parts.As we insert these items using SQLModel, the package will parametrize these strings before insertion, so that is a safe process, However, some sanitisation was nonetheless applied. For the visit, only alphanumerics, hyphens, and underscores will be retained. For the description, the character allow list is expanded to also include:
As part of housekeeping, the way in which the Murfey database tables were imported and called was also standardised. Instead of importing the tables directly, we use
import murfey.util.db as MurfeyDBand called tables fromMurfeyDB. Type hints were also replaced with more modern variants.A bug was also identified when seeding the database fixtures upon startup. For integer primary keys, if a value is manually provided as part of the initial insert, it does not update PostGreSQL's internal counter. Subsequent inserts where the primary key is allow to auto-increment will then attempt to insert into that database with the previous primary key, causing the insert to fail. By removing the primary key from the initial insert in the database fixture, the internal PostgreSQL counter is now updated correctly.