Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
b9cb04b0
Commit
b9cb04b0
authored
May 06, 2020
by
Vijay Hawoldar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor snippet repo path error handling
parent
f91731a8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
3 deletions
+33
-3
app/models/snippet_repository.rb
app/models/snippet_repository.rb
+16
-2
lib/gitlab/background_migration/backfill_snippet_repositories.rb
...lab/background_migration/backfill_snippet_repositories.rb
+1
-1
spec/models/snippet_repository_spec.rb
spec/models/snippet_repository_spec.rb
+16
-0
No files found.
app/models/snippet_repository.rb
View file @
b9cb04b0
...
@@ -7,6 +7,7 @@ class SnippetRepository < ApplicationRecord
...
@@ -7,6 +7,7 @@ class SnippetRepository < ApplicationRecord
EMPTY_FILE_PATTERN
=
/^
#{
DEFAULT_EMPTY_FILE_NAME
}
(\d+)\.txt$/
.
freeze
EMPTY_FILE_PATTERN
=
/^
#{
DEFAULT_EMPTY_FILE_NAME
}
(\d+)\.txt$/
.
freeze
CommitError
=
Class
.
new
(
StandardError
)
CommitError
=
Class
.
new
(
StandardError
)
InvalidPathError
=
Class
.
new
(
CommitError
)
belongs_to
:snippet
,
inverse_of: :snippet_repository
belongs_to
:snippet
,
inverse_of: :snippet_repository
...
@@ -40,8 +41,9 @@ class SnippetRepository < ApplicationRecord
...
@@ -40,8 +41,9 @@ class SnippetRepository < ApplicationRecord
rescue
Gitlab
::
Git
::
Index
::
IndexError
,
rescue
Gitlab
::
Git
::
Index
::
IndexError
,
Gitlab
::
Git
::
CommitError
,
Gitlab
::
Git
::
CommitError
,
Gitlab
::
Git
::
PreReceiveError
,
Gitlab
::
Git
::
PreReceiveError
,
Gitlab
::
Git
::
CommandError
=>
e
Gitlab
::
Git
::
CommandError
=>
error
raise
CommitError
,
e
.
message
raise
commit_error_exception
(
error
)
end
end
def
transform_file_entries
(
files
)
def
transform_file_entries
(
files
)
...
@@ -85,4 +87,16 @@ class SnippetRepository < ApplicationRecord
...
@@ -85,4 +87,16 @@ class SnippetRepository < ApplicationRecord
def
build_empty_file_name
(
index
)
def
build_empty_file_name
(
index
)
"
#{
DEFAULT_EMPTY_FILE_NAME
}#{
index
}
.txt"
"
#{
DEFAULT_EMPTY_FILE_NAME
}#{
index
}
.txt"
end
end
def
commit_error_exception
(
error
)
if
error
.
is_a?
(
Gitlab
::
Git
::
Index
::
IndexError
)
&&
invalid_path_error?
(
error
.
message
)
InvalidPathError
.
new
(
'Invalid Path'
)
# To avoid returning the message with the path included
else
CommitError
.
new
(
error
.
message
)
end
end
def
invalid_path_error?
(
message
)
message
.
downcase
.
start_with?
(
'invalid path'
,
'path cannot include directory traversal'
)
end
end
end
lib/gitlab/background_migration/backfill_snippet_repositories.rb
View file @
b9cb04b0
...
@@ -117,7 +117,7 @@ module Gitlab
...
@@ -117,7 +117,7 @@ module Gitlab
# the migration can succeed, to achieve that, we'll identify in migration retries
# the migration can succeed, to achieve that, we'll identify in migration retries
# that the path is invalid
# that the path is invalid
def
set_file_path_error
(
error
)
def
set_file_path_error
(
error
)
@invalid_path_error
=
error
.
message
.
downcase
.
start_with?
(
'invalid path'
,
'path cannot include directory traversal'
)
@invalid_path_error
=
error
.
is_a?
(
SnippetRepository
::
InvalidPathError
)
end
end
end
end
end
end
...
...
spec/models/snippet_repository_spec.rb
View file @
b9cb04b0
...
@@ -202,6 +202,22 @@ describe SnippetRepository do
...
@@ -202,6 +202,22 @@ describe SnippetRepository do
it_behaves_like
'snippet repository with file names'
,
'snippetfile10.txt'
,
'snippetfile11.txt'
it_behaves_like
'snippet repository with file names'
,
'snippetfile10.txt'
,
'snippetfile11.txt'
end
end
shared_examples
'snippet repository with git errors'
do
|
path
,
error
|
let
(
:new_file
)
{
{
file_path:
path
,
content:
'bar'
}
}
it
'raises a path specific error'
do
expect
do
snippet_repository
.
multi_files_action
(
user
,
data
,
commit_opts
)
end
.
to
raise_error
(
error
)
end
end
context
'with git errors'
do
it_behaves_like
'snippet repository with git errors'
,
'invalid://path/here'
,
described_class
::
InvalidPathError
it_behaves_like
'snippet repository with git errors'
,
'../../path/traversal/here'
,
described_class
::
InvalidPathError
it_behaves_like
'snippet repository with git errors'
,
'README'
,
described_class
::
CommitError
end
end
end
def
blob_at
(
snippet
,
path
)
def
blob_at
(
snippet
,
path
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment