Commit 2dff9bb7 authored by Vijay Hawoldar's avatar Vijay Hawoldar

Handle Snippet file name errors in backfill

parent 05ef70b8
---
title: Handle Snippet file name errors in backfill
merge_request: 30981
author:
type: fixed
...@@ -15,12 +15,15 @@ module Gitlab ...@@ -15,12 +15,15 @@ module Gitlab
next if repository_present?(snippet) next if repository_present?(snippet)
retry_index = 0 retry_index = 0
@invalid_path_error = false
begin begin
create_repository_and_files(snippet) create_repository_and_files(snippet)
logger.info(message: 'Snippet Migration: repository created and migrated', snippet: snippet.id) logger.info(message: 'Snippet Migration: repository created and migrated', snippet: snippet.id)
rescue => e rescue => e
set_file_path_error(e)
retry_index += 1 retry_index += 1
retry if retry_index < MAX_RETRIES retry if retry_index < MAX_RETRIES
...@@ -73,7 +76,10 @@ module Gitlab ...@@ -73,7 +76,10 @@ module Gitlab
end end
def filename(snippet) def filename(snippet)
snippet.file_name.presence || empty_file_name file_name = snippet.file_name
file_name = file_name.parameterize if @invalid_path_error
file_name.presence || empty_file_name
end end
def empty_file_name def empty_file_name
...@@ -104,6 +110,15 @@ module Gitlab ...@@ -104,6 +110,15 @@ module Gitlab
def admin_user def admin_user
@admin_user ||= User.admins.active.first @admin_user ||= User.admins.active.first
end end
# We sometimes receive invalid path errors from Gitaly if the Snippet filename
# cannot be parsed into a valid git path.
# In this situation, we need to parameterize the file name of the Snippet so that
# the migration can succeed, to achieve that, we'll identify in migration retries
# that the path is invalid
def set_file_path_error(error)
@invalid_path_error = error.message.downcase.start_with?('invalid path', 'path cannot include directory traversal')
end
end end
end end
end end
...@@ -177,6 +177,48 @@ describe Gitlab::BackgroundMigration::BackfillSnippetRepositories, :migration, s ...@@ -177,6 +177,48 @@ describe Gitlab::BackgroundMigration::BackfillSnippetRepositories, :migration, s
end end
end end
end end
context 'with invalid file names' do
using RSpec::Parameterized::TableSyntax
where(:invalid_file_name, :converted_file_name) do
'filename.js // with comment' | 'filename-js-with-comment'
'.git/hooks/pre-commit' | 'git-hooks-pre-commit'
'https://gitlab.com' | 'https-gitlab-com'
'html://web.title%mp4/mpg/mpeg.net' | 'html-web-title-mp4-mpg-mpeg-net'
'../../etc/passwd' | 'etc-passwd'
'.' | 'snippetfile1.txt'
end
with_them do
let!(:snippet_with_invalid_path) { snippets.create(id: 4, type: 'PersonalSnippet', author_id: user.id, file_name: invalid_file_name, content: content) }
let!(:snippet_with_valid_path) { snippets.create(id: 5, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) }
let(:ids) { [4, 5] }
after do
raw_repository(snippet_with_invalid_path).remove
raw_repository(snippet_with_valid_path).remove
end
it 'checks for file path errors when errors are raised' do
expect(service).to receive(:set_file_path_error).once.and_call_original
subject
end
it 'converts invalid filenames' do
subject
expect(blob_at(snippet_with_invalid_path, converted_file_name)).to be
end
it 'does not convert valid filenames on subsequent migrations' do
subject
expect(blob_at(snippet_with_valid_path, file_name)).to be
end
end
end
end end
def blob_at(snippet, path) def blob_at(snippet, path)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment