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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
5169dafc
Commit
5169dafc
authored
Mar 04, 2019
by
Igor Drozdov
Committed by
Yorick Peterse
Mar 04, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Forbid creating discussions for users with restricted access
parent
9faf957b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
137 additions
and
60 deletions
+137
-60
changelogs/unreleased/security-id-restricted-access-to-private-repo.yml
...eleased/security-id-restricted-access-to-private-repo.yml
+5
-0
lib/api/helpers/notes_helpers.rb
lib/api/helpers/notes_helpers.rb
+3
-11
spec/policies/commit_policy_spec.rb
spec/policies/commit_policy_spec.rb
+53
-0
spec/policies/note_policy_spec.rb
spec/policies/note_policy_spec.rb
+45
-49
spec/support/shared_examples/requests/api/discussions.rb
spec/support/shared_examples/requests/api/discussions.rb
+31
-0
No files found.
changelogs/unreleased/security-id-restricted-access-to-private-repo.yml
0 → 100644
View file @
5169dafc
---
title
:
Forbid creating discussions for users with restricted access
merge_request
:
author
:
type
:
security
lib/api/helpers/notes_helpers.rb
View file @
5169dafc
...
@@ -70,14 +70,7 @@ module API
...
@@ -70,14 +70,7 @@ module API
def
find_noteable
(
parent
,
noteables_str
,
noteable_id
)
def
find_noteable
(
parent
,
noteables_str
,
noteable_id
)
noteable
=
public_send
(
"find_
#{
parent
}
_
#{
noteables_str
.
singularize
}
"
,
noteable_id
)
# rubocop:disable GitlabSecurity/PublicSend
noteable
=
public_send
(
"find_
#{
parent
}
_
#{
noteables_str
.
singularize
}
"
,
noteable_id
)
# rubocop:disable GitlabSecurity/PublicSend
readable
=
readable
=
can?
(
current_user
,
noteable_read_ability_name
(
noteable
),
noteable
)
if
noteable
.
is_a?
(
Commit
)
# for commits there is not :read_commit policy, check if user
# has :read_note permission on the commit's project
can?
(
current_user
,
:read_note
,
user_project
)
else
can?
(
current_user
,
noteable_read_ability_name
(
noteable
),
noteable
)
end
return
not_found!
(
noteables_str
)
unless
readable
return
not_found!
(
noteables_str
)
unless
readable
...
@@ -89,12 +82,11 @@ module API
...
@@ -89,12 +82,11 @@ module API
end
end
def
create_note
(
noteable
,
opts
)
def
create_note
(
noteable
,
opts
)
policy_object
=
noteable
.
is_a?
(
Commit
)
?
user_project
:
noteable
authorize!
(
:create_note
,
noteable
)
authorize!
(
:create_note
,
policy_object
)
parent
=
noteable_parent
(
noteable
)
parent
=
noteable_parent
(
noteable
)
opts
.
delete
(
:created_at
)
unless
current_user
.
can?
(
:set_note_created_at
,
policy_object
)
opts
.
delete
(
:created_at
)
unless
current_user
.
can?
(
:set_note_created_at
,
noteable
)
opts
[
:updated_at
]
=
opts
[
:created_at
]
if
opts
[
:created_at
]
opts
[
:updated_at
]
=
opts
[
:created_at
]
if
opts
[
:created_at
]
...
...
spec/policies/commit_policy_spec.rb
0 → 100644
View file @
5169dafc
require
'spec_helper'
describe
CommitPolicy
do
describe
'#rules'
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:commit
)
{
project
.
repository
.
head_commit
}
let
(
:policy
)
{
described_class
.
new
(
user
,
commit
)
}
context
'when project is public'
do
let
(
:project
)
{
create
(
:project
,
:public
,
:repository
)
}
it
'can read commit and create a note'
do
expect
(
policy
).
to
be_allowed
(
:read_commit
)
end
context
'when repository access level is private'
do
let
(
:project
)
{
create
(
:project
,
:public
,
:repository
,
:repository_private
)
}
it
'can not read commit and create a note'
do
expect
(
policy
).
to
be_disallowed
(
:read_commit
)
end
context
'when the user is a project member'
do
before
do
project
.
add_developer
(
user
)
end
it
'can read commit and create a note'
do
expect
(
policy
).
to
be_allowed
(
:read_commit
)
end
end
end
end
context
'when project is private'
do
let
(
:project
)
{
create
(
:project
,
:private
,
:repository
)
}
it
'can not read commit and create a note'
do
expect
(
policy
).
to
be_disallowed
(
:read_commit
)
end
context
'when the user is a project member'
do
before
do
project
.
add_developer
(
user
)
end
it
'can read commit and create a note'
do
expect
(
policy
).
to
be_allowed
(
:read_commit
)
end
end
end
end
end
spec/policies/note_policy_spec.rb
View file @
5169dafc
require
'spec_helper'
require
'spec_helper'
describe
NotePolicy
,
mdoels:
true
do
describe
NotePolicy
do
describe
'#rules'
do
describe
'#rules'
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:project
)
{
create
(
:project
,
:public
)
}
let
(
:project
)
{
create
(
:project
,
:public
)
}
let
(
:issue
)
{
create
(
:issue
,
project:
project
)
}
let
(
:issue
)
{
create
(
:issue
,
project:
project
)
}
let
(
:noteable
)
{
issue
}
def
policies
(
noteable
=
nil
)
let
(
:policy
)
{
described_class
.
new
(
user
,
note
)
}
return
@policies
if
@policies
let
(
:note
)
{
create
(
:note
,
noteable:
noteable
,
author:
user
,
project:
project
)
}
noteable
||=
issue
note
=
if
noteable
.
is_a?
(
Commit
)
create
(
:note_on_commit
,
commit_id:
noteable
.
id
,
author:
user
,
project:
project
)
else
create
(
:note
,
noteable:
noteable
,
author:
user
,
project:
project
)
end
@policies
=
described_class
.
new
(
user
,
note
)
end
shared_examples_for
'a discussion with a private noteable'
do
shared_examples_for
'a discussion with a private noteable'
do
let
(
:noteable
)
{
issue
}
let
(
:policy
)
{
policies
(
noteable
)
}
context
'when the note author can no longer see the noteable'
do
context
'when the note author can no longer see the noteable'
do
it
'can not edit nor read the note'
do
it
'can not edit nor read the note'
do
expect
(
policy
).
to
be_disallowed
(
:admin_note
)
expect
(
policy
).
to
be_disallowed
(
:admin_note
)
...
@@ -46,12 +33,21 @@ describe NotePolicy, mdoels: true do
...
@@ -46,12 +33,21 @@ describe NotePolicy, mdoels: true do
end
end
end
end
context
'when the project is private'
do
context
'when the noteable is a commit'
do
let
(
:project
)
{
create
(
:project
,
:private
,
:repository
)
}
let
(
:commit
)
{
project
.
repository
.
head_commit
}
let
(
:note
)
{
create
(
:note_on_commit
,
commit_id:
commit
.
id
,
author:
user
,
project:
project
)
}
context
'when the project is private'
do
let
(
:project
)
{
create
(
:project
,
:private
,
:repository
)
}
it_behaves_like
'a discussion with a private noteable'
end
context
'when the noteable is a commit'
do
context
'when the project is public'
do
it_behaves_like
'a discussion with a private noteable'
do
context
'when repository access level is private'
do
let
(
:noteable
)
{
project
.
repository
.
head_commit
}
let
(
:project
)
{
create
(
:project
,
:public
,
:repository
,
:repository_private
)
}
it_behaves_like
'a discussion with a private noteable'
end
end
end
end
end
end
...
@@ -59,44 +55,44 @@ describe NotePolicy, mdoels: true do
...
@@ -59,44 +55,44 @@ describe NotePolicy, mdoels: true do
context
'when the project is public'
do
context
'when the project is public'
do
context
'when the note author is not a project member'
do
context
'when the note author is not a project member'
do
it
'can edit a note'
do
it
'can edit a note'
do
expect
(
polic
ies
).
to
be_allowed
(
:admin_note
)
expect
(
polic
y
).
to
be_allowed
(
:admin_note
)
expect
(
polic
ies
).
to
be_allowed
(
:resolve_note
)
expect
(
polic
y
).
to
be_allowed
(
:resolve_note
)
expect
(
polic
ies
).
to
be_allowed
(
:read_note
)
expect
(
polic
y
).
to
be_allowed
(
:read_note
)
end
end
end
end
context
'when the noteable is a project snippet'
do
context
'when the noteable is a project snippet'
do
it
'can edit note'
do
let
(
:noteable
)
{
create
(
:project_snippet
,
:public
,
project:
project
)
}
policies
=
policies
(
create
(
:project_snippet
,
:public
,
project:
project
))
expect
(
policies
).
to
be_allowed
(
:admin_note
)
it
'can edit note'
do
expect
(
policies
).
to
be_allowed
(
:resolve_note
)
expect
(
policy
).
to
be_allowed
(
:admin_note
)
expect
(
policies
).
to
be_allowed
(
:read_note
)
expect
(
policy
).
to
be_allowed
(
:resolve_note
)
expect
(
policy
).
to
be_allowed
(
:read_note
)
end
end
context
'when it is private'
do
context
'when it is private'
do
it_behaves_like
'a discussion with a private noteable'
do
let
(
:noteable
)
{
create
(
:project_snippet
,
:private
,
project:
project
)
}
let
(
:noteable
)
{
create
(
:project_snippet
,
:private
,
project:
project
)
}
end
it_behaves_like
'a discussion with a private noteable'
end
end
end
end
context
'when the noteable is a personal snippet'
do
context
'when the noteable is a personal snippet'
do
it
'can edit note'
do
let
(
:noteable
)
{
create
(
:personal_snippet
,
:public
)
}
policies
=
policies
(
create
(
:personal_snippet
,
:public
))
expect
(
policies
).
to
be_allowed
(
:admin_note
)
it
'can edit note'
do
expect
(
policies
).
to
be_allowed
(
:resolve_note
)
expect
(
policy
).
to
be_allowed
(
:admin_note
)
expect
(
policies
).
to
be_allowed
(
:read_note
)
expect
(
policy
).
to
be_allowed
(
:resolve_note
)
expect
(
policy
).
to
be_allowed
(
:read_note
)
end
end
context
'when it is private'
do
context
'when it is private'
do
it
'can not edit nor read the note'
do
let
(
:noteable
)
{
create
(
:personal_snippet
,
:private
)
}
policies
=
policies
(
create
(
:personal_snippet
,
:private
))
expect
(
policies
).
to
be_disallowed
(
:admin_note
)
it
'can not edit nor read the note'
do
expect
(
policies
).
to
be_disallowed
(
:resolve_note
)
expect
(
policy
).
to
be_disallowed
(
:admin_note
)
expect
(
policies
).
to
be_disallowed
(
:read_note
)
expect
(
policy
).
to
be_disallowed
(
:resolve_note
)
expect
(
policy
).
to
be_disallowed
(
:read_note
)
end
end
end
end
end
end
...
@@ -120,20 +116,20 @@ describe NotePolicy, mdoels: true do
...
@@ -120,20 +116,20 @@ describe NotePolicy, mdoels: true do
end
end
it
'can edit a note'
do
it
'can edit a note'
do
expect
(
polic
ies
).
to
be_allowed
(
:admin_note
)
expect
(
polic
y
).
to
be_allowed
(
:admin_note
)
expect
(
polic
ies
).
to
be_allowed
(
:resolve_note
)
expect
(
polic
y
).
to
be_allowed
(
:resolve_note
)
expect
(
polic
ies
).
to
be_allowed
(
:read_note
)
expect
(
polic
y
).
to
be_allowed
(
:read_note
)
end
end
end
end
context
'when the note author is not a project member'
do
context
'when the note author is not a project member'
do
it
'can not edit a note'
do
it
'can not edit a note'
do
expect
(
polic
ies
).
to
be_disallowed
(
:admin_note
)
expect
(
polic
y
).
to
be_disallowed
(
:admin_note
)
expect
(
polic
ies
).
to
be_disallowed
(
:resolve_note
)
expect
(
polic
y
).
to
be_disallowed
(
:resolve_note
)
end
end
it
'can read a note'
do
it
'can read a note'
do
expect
(
polic
ies
).
to
be_allowed
(
:read_note
)
expect
(
polic
y
).
to
be_allowed
(
:read_note
)
end
end
end
end
end
end
...
...
spec/support/shared_examples/requests/api/discussions.rb
View file @
5169dafc
...
@@ -86,6 +86,37 @@ shared_examples 'discussions API' do |parent_type, noteable_type, id_name|
...
@@ -86,6 +86,37 @@ shared_examples 'discussions API' do |parent_type, noteable_type, id_name|
expect
(
response
).
to
have_gitlab_http_status
(
404
)
expect
(
response
).
to
have_gitlab_http_status
(
404
)
end
end
end
end
context
'when a project is public with private repo access'
do
let!
(
:parent
)
{
create
(
:project
,
:public
,
:repository
,
:repository_private
,
:snippets_private
)
}
let!
(
:user_without_access
)
{
create
(
:user
)
}
context
'when user is not a team member of private repo'
do
before
do
project
.
team
.
truncate
end
context
"creating a new note"
do
before
do
post
api
(
"/
#{
parent_type
}
/
#{
parent
.
id
}
/
#{
noteable_type
}
/
#{
noteable
[
id_name
]
}
/discussions"
,
user_without_access
),
params:
{
body:
'hi!'
}
end
it
'raises 404 error'
do
expect
(
response
).
to
have_gitlab_http_status
(
404
)
end
end
context
"fetching a discussion"
do
before
do
get
api
(
"/
#{
parent_type
}
/
#{
parent
.
id
}
/
#{
noteable_type
}
/
#{
noteable
[
id_name
]
}
/discussions/
#{
note
.
discussion_id
}
"
,
user_without_access
)
end
it
'raises 404 error'
do
expect
(
response
).
to
have_gitlab_http_status
(
404
)
end
end
end
end
end
end
describe
"POST /
#{
parent_type
}
/:id/
#{
noteable_type
}
/:noteable_id/discussions/:discussion_id/notes"
do
describe
"POST /
#{
parent_type
}
/:id/
#{
noteable_type
}
/:noteable_id/discussions/:discussion_id/notes"
do
...
...
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