Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mastodon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pierre Boudes
mastodon
Commits
0412a4d0
Unverified
Commit
0412a4d0
authored
2 years ago
by
Eugen Rochko
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Change e-mail domain blocks to match subdomains of blocked domains (#18979)
parent
d83faa1a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/models/email_domain_block.rb
+44
-20
44 additions, 20 deletions
app/models/email_domain_block.rb
spec/models/email_domain_block_spec.rb
+20
-7
20 additions, 7 deletions
spec/models/email_domain_block_spec.rb
with
64 additions
and
27 deletions
app/models/email_domain_block.rb
+
44
−
20
View file @
0412a4d0
...
...
@@ -30,32 +30,56 @@ class EmailDomainBlock < ApplicationRecord
@history
||=
Trends
::
History
.
new
(
'email_domain_blocks'
,
id
)
end
def
self
.
block?
(
domain_or_domains
,
attempt_ip:
nil
)
domains
=
Array
(
domain_or_domains
).
map
do
|
str
|
domain
=
begin
if
str
.
include?
(
'@'
)
str
.
split
(
'@'
,
2
).
last
else
str
end
end
class
Matcher
def
initialize
(
domain_or_domains
,
attempt_ip:
nil
)
@uris
=
extract_uris
(
domain_or_domains
)
@attempt_ip
=
attempt_ip
end
TagManager
.
instance
.
normalize_domain
(
domain
)
if
domain
.
present?
rescue
Addressable
::
URI
::
InvalidURIError
nil
def
match?
blocking?
||
invalid_uri?
end
# If some of the inputs passed in are invalid, we definitely want to
# block the attempt, but we also want to register hits against any
# other valid matches
private
blocked
=
domains
.
any?
(
&
:nil?
)
def
invalid_uri?
@uris
.
any?
(
&
:nil?
)
end
where
(
domain:
domains
).
find_each
do
|
block
|
blocked
=
true
block
.
history
.
add
(
attempt_ip
)
if
attempt_ip
.
present?
def
blocking?
blocks
=
EmailDomainBlock
.
where
(
domain:
domains_with_variants
).
order
(
Arel
.
sql
(
'char_length(domain) desc'
))
blocks
.
each
{
|
block
|
block
.
history
.
add
(
@attempt_ip
)
}
if
@attempt_ip
.
present?
blocks
.
any?
end
blocked
def
domains_with_variants
@uris
.
flat_map
do
|
uri
|
next
if
uri
.
nil?
segments
=
uri
.
normalized_host
.
split
(
'.'
)
segments
.
map
.
with_index
{
|
_
,
i
|
segments
[
i
..-
1
].
join
(
'.'
)
}
end
end
def
extract_uris
(
domain_or_domains
)
Array
(
domain_or_domains
).
map
do
|
str
|
domain
=
begin
if
str
.
include?
(
'@'
)
str
.
split
(
'@'
,
2
).
last
else
str
end
end
Addressable
::
URI
.
new
.
tap
{
|
u
|
u
.
host
=
domain
.
strip
}
if
domain
.
present?
rescue
Addressable
::
URI
::
InvalidURIError
,
IDN
::
Idna
::
IdnaError
nil
end
end
end
def
self
.
block?
(
domain_or_domains
,
attempt_ip:
nil
)
Matcher
.
new
(
domain_or_domains
,
attempt_ip:
attempt_ip
).
match?
end
end
This diff is collapsed.
Click to expand it.
spec/models/email_domain_block_spec.rb
+
20
−
7
View file @
0412a4d0
...
...
@@ -12,16 +12,29 @@ RSpec.describe EmailDomainBlock, type: :model do
let
(
:input
)
{
nil
}
context
'given an e-mail address'
do
let
(
:input
)
{
'nyarn@example.com'
}
let
(
:input
)
{
"foo@
#{
domain
}
"
}
it
'returns true if the domain is blocked'
do
Fabricate
(
:email_domain_block
,
domain:
'example.com'
)
expect
(
EmailDomainBlock
.
block?
(
input
)).
to
be
true
context
do
let
(
:domain
)
{
'example.com'
}
it
'returns true if the domain is blocked'
do
Fabricate
(
:email_domain_block
,
domain:
'example.com'
)
expect
(
EmailDomainBlock
.
block?
(
input
)).
to
be
true
end
it
'returns false if the domain is not blocked'
do
Fabricate
(
:email_domain_block
,
domain:
'other-example.com'
)
expect
(
EmailDomainBlock
.
block?
(
input
)).
to
be
false
end
end
it
'returns false if the domain is not blocked'
do
Fabricate
(
:email_domain_block
,
domain:
'other-example.com'
)
expect
(
EmailDomainBlock
.
block?
(
input
)).
to
be
false
context
do
let
(
:domain
)
{
'mail.example.com'
}
it
'returns true if it is a subdomain of a blocked domain'
do
Fabricate
(
:email_domain_block
,
domain:
'example.com'
)
expect
(
described_class
.
block?
(
input
)).
to
be
true
end
end
end
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment