Do not cache a nil project or namespace in ApplicationContext
What does this MR do and why?
This MR stops Gitlab::ApplicationContext from caching a nil project or namespace when something reads the context before the request has loaded the record.
The API and web controllers push the project and namespace as lambdas, such as -> { @project }, that stay nil until the endpoint has loaded the project. The context cached the first value it read, nil included. Every log line reads the whole context, because Gitlab::JsonLogger merges it in. So a single log line written during authorization left the project and root namespace empty for the rest of the request.
On GitLab.com this drops meta.project, meta.root_namespace, meta.gl_root_namespace_id and meta.subscription_plan from the API log whenever authorization logs before the project is loaded. For example:
- The project is in a group with IP restrictions. The IP restriction check in the project policy logs on every request.
- The token has the
ai_workflowsscope, which is audited during authentication. PersonalAccessTokens::LastUsedServicecannot obtain its lease and logs that.
The user already has this exemption. This MR extends it to the project and namespace, so a nil is re-resolved on the next read and the first non-nil value is the one that gets cached. Re-resolving is cheap, because every lambda that pushes these attributes only reads an instance variable.
Runner-authenticated routes have the same problem through runner_project and job_project, which memoize separately. This MR does not change them.
References
- !252817 (merged) added the same exemption for the user.
Screenshots or screen recordings
Not applicable: this MR has no UI change.
How to set up and validate locally
-
With a Premium or Ultimate license, restrict access by IP address on a top-level group (Settings > General > Permissions and group features) to a range that includes your own address, for example
127.0.0.0/8. -
Request a project in that group with a personal access token:
curl --header "PRIVATE-TOKEN: <your_access_token>" "http://gdk.test:3000/api/v4/projects/<project_id>" -
Find the request in
log/api_json.log. Onmasterthe entry hasmeta.userbut nometa.projectormeta.root_namespace. On this branch it has both.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.