Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an attempt to provide a better structure for handling CVE 404 errors following my comment here:
#11305 (comment)
Basically, we ended up in a situation where we were trying to raise a 404 error from within the
security_api_error
error handler, but we had to do it manually withflask.render_template
rather than the more properflask.abort
because you can'tabort
from within an activeerrorhandler
.I think this was pointing to a sort of code smell, that we shouldn't really have got as far as raising a
SecurityAPIError
if really all that happened is that a CVE is missing. In fact, I think it would be much more reasonable forSecurityAPI.get_cve
to simply returnNone
, rather than an error, if the CVE in question doesn't exist. This is reinforced by the fact that the code to turn thatNone
response into aflask.abort(404)
actually already existed in thecve
view, even though it was not not being used.So I've restructured the code to do the logic of reading the
404
code from the API's response inside theSecurityAPIError.get_cve
method itself, and returnNone
in that case. TheSecurityAPIError._get
method now simply doesraise_for_status()
, which seems reasonable as it is simply a generic HTTP request method and shouldn't have any greater knowledge than that, and then it's up to the public methods (currentlyget_cve
andget_releases
) to turn thoseHTTPError
exceptions intoSecurityAPIError
s orNone
as appropriate. I hope this makes sense.To facilitate this branch I've also proposed a change to canonicalwebteam.flask-base. This branch uses that new version. A review of this branch is effectively a review of that one. Please go and approve that one once this is reviewed, but don't merge this until that PR is merged and published and this PR's
requirements.txt
is updated.QA
Go to https://ubuntu-com-11343.demos.haus/security/CVE-1234-5678, see a beautiful 404 page, with a custom error message.
Go to https://ubuntu-com-11343.demos.haus/zfgzdfsd check the normal 404 page still works.
Go to https://ubuntu-com-11343.demos.haus/security/CVE-2022-23639, check a normal CVE still displays properly.