12db4cc0e7
Fix missing Note access checks in by moving Note#search to updated NoteFinder Split from !2024 to partially solve https://gitlab.com/gitlab-org/gitlab-ce/issues/23867 ## Which fixes are in this MR? ⚠️ - Potentially untested 💣 - No test coverage 🚥 - Test coverage of some sort exists (a test failed when error raised) 🚦 - Test coverage of return value (a test failed when nil used) ✅ - Permissions check tested ### Note lookup without access check - [x] ✅ app/finders/notes_finder.rb:13 :download_code check - [x] ✅ app/finders/notes_finder.rb:19 `SnippetsFinder` - [x] ✅ app/models/note.rb:121 [`Issue#visible_to_user`] - [x] ✅ lib/gitlab/project_search_results.rb:113 - This is the only use of `app/models/note.rb:121` above, but importantly has no access checks at all. This means it leaks MR comments and snippets when those features are `team-only` in addition to the issue comments which would be fixed by `app/models/note.rb:121`. - It is only called from SearchController where `can?(current_user, :download_code, @project)` is checked, so commit comments are not leaked. ### Previous discussions - [x] https://dev.gitlab.org/gitlab/gitlabhq/merge_requests/2024/diffs#b915c5267a63628b0bafd23d37792ae73ceae272_13_13 `: download_code` check on commit - [x] https://dev.gitlab.org/gitlab/gitlabhq/merge_requests/2024/diffs#b915c5267a63628b0bafd23d37792ae73ceae272_19_19 `SnippetsFinder` should be used - `SnippetsFinder` should check if the snippets feature is enabled -> https://gitlab.com/gitlab-org/gitlab-ce/issues/25223 ### Acceptance criteria met? - [x] Tests added for new code - [x] TODO comments removed - [x] Squashed and removed skipped tests - [x] Changelog entry - [ ] State Gitlab versions affected and issue severity in description - [ ] Create technical debt issue for NotesFinder. - Either split into `NotesFinder::ForTarget` and `NotesFinder::Search` or consider object per notable type such as `NotesFinder::OnIssue`. For the first option could create `NotesFinder::Base` which is either inherited from or which can be included in the other two. - Avoid case statement anti-pattern in this finder with use of `NotesFinder::OnCommit` etc. Consider something on the finder for this? `Model.finder(user, project)` - Move `inc_author` to the controller, and implement `related_notes` to replace `non_diff_notes`/`mr_and_commit_notes` See merge request !2035
102 lines
2.9 KiB
Ruby
102 lines
2.9 KiB
Ruby
class NotesFinder
|
|
FETCH_OVERLAP = 5.seconds
|
|
|
|
# Used to filter Notes
|
|
# When used with target_type and target_id this returns notes specifically for the controller
|
|
#
|
|
# Arguments:
|
|
# current_user - which user check authorizations with
|
|
# project - which project to look for notes on
|
|
# params:
|
|
# target_type: string
|
|
# target_id: integer
|
|
# last_fetched_at: time
|
|
# search: string
|
|
#
|
|
def initialize(project, current_user, params = {})
|
|
@project = project
|
|
@current_user = current_user
|
|
@params = params
|
|
init_collection
|
|
end
|
|
|
|
def execute
|
|
@notes = since_fetch_at(@params[:last_fetched_at]) if @params[:last_fetched_at]
|
|
@notes
|
|
end
|
|
|
|
private
|
|
|
|
def init_collection
|
|
if @params[:target_id]
|
|
@notes = on_target(@params[:target_type], @params[:target_id])
|
|
else
|
|
@notes = notes_of_any_type
|
|
end
|
|
end
|
|
|
|
def notes_of_any_type
|
|
types = %w(commit issue merge_request snippet)
|
|
note_relations = types.map { |t| notes_for_type(t) }
|
|
note_relations.map!{ |notes| search(@params[:search], notes) } if @params[:search]
|
|
UnionFinder.new.find_union(note_relations, Note)
|
|
end
|
|
|
|
def noteables_for_type(noteable_type)
|
|
case noteable_type
|
|
when "issue"
|
|
IssuesFinder.new(@current_user, project_id: @project.id).execute
|
|
when "merge_request"
|
|
MergeRequestsFinder.new(@current_user, project_id: @project.id).execute
|
|
when "snippet", "project_snippet"
|
|
SnippetsFinder.new.execute(@current_user, filter: :by_project, project: @project)
|
|
else
|
|
raise 'invalid target_type'
|
|
end
|
|
end
|
|
|
|
def notes_for_type(noteable_type)
|
|
if noteable_type == "commit"
|
|
if Ability.allowed?(@current_user, :download_code, @project)
|
|
@project.notes.where(noteable_type: 'Commit')
|
|
else
|
|
Note.none
|
|
end
|
|
else
|
|
finder = noteables_for_type(noteable_type)
|
|
@project.notes.where(noteable_type: finder.base_class.name, noteable_id: finder.reorder(nil))
|
|
end
|
|
end
|
|
|
|
def on_target(target_type, target_id)
|
|
if target_type == "commit"
|
|
notes_for_type('commit').for_commit_id(target_id)
|
|
else
|
|
target = noteables_for_type(target_type).find(target_id)
|
|
|
|
if target.respond_to?(:related_notes)
|
|
target.related_notes
|
|
else
|
|
target.notes
|
|
end
|
|
end
|
|
end
|
|
|
|
# Searches for notes matching the given query.
|
|
#
|
|
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
|
|
#
|
|
def search(query, notes_relation = @notes)
|
|
pattern = "%#{query}%"
|
|
notes_relation.where(Note.arel_table[:note].matches(pattern))
|
|
end
|
|
|
|
# Notes changed since last fetch
|
|
# Uses overlapping intervals to avoid worrying about race conditions
|
|
def since_fetch_at(fetch_time)
|
|
# Default to 0 to remain compatible with old clients
|
|
last_fetched_at = Time.at(@params.fetch(:last_fetched_at, 0).to_i)
|
|
|
|
@notes.where('updated_at > ?', last_fetched_at - FETCH_OVERLAP).fresh
|
|
end
|
|
end
|