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
773d965b
Commit
773d965b
authored
Jul 30, 2021
by
Jonathan Schafer
Committed by
David Fernandez
Jul 30, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add field validations for Finding Evidence models
Updates to models and tests Changelog: changed EE: true
parent
f10d4e52
Changes
17
Show whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
121 additions
and
86 deletions
+121
-86
.rubocop_manual_todo.yml
.rubocop_manual_todo.yml
+1
-0
app/models/concerns/any_field_validation.rb
app/models/concerns/any_field_validation.rb
+0
-25
app/validators/any_field_validator.rb
app/validators/any_field_validator.rb
+35
-0
ee/app/models/vulnerabilities/finding/evidence/asset.rb
ee/app/models/vulnerabilities/finding/evidence/asset.rb
+3
-10
ee/app/models/vulnerabilities/finding/evidence/header.rb
ee/app/models/vulnerabilities/finding/evidence/header.rb
+2
-2
ee/app/models/vulnerabilities/finding/evidence/request.rb
ee/app/models/vulnerabilities/finding/evidence/request.rb
+3
-0
ee/app/models/vulnerabilities/finding/evidence/response.rb
ee/app/models/vulnerabilities/finding/evidence/response.rb
+1
-1
ee/app/models/vulnerabilities/finding/evidence/source.rb
ee/app/models/vulnerabilities/finding/evidence/source.rb
+3
-0
ee/app/models/vulnerabilities/finding/evidence/supporting_message.rb
...ls/vulnerabilities/finding/evidence/supporting_message.rb
+1
-1
ee/spec/models/vulnerabilities/finding/evidence/asset_spec.rb
...pec/models/vulnerabilities/finding/evidence/asset_spec.rb
+1
-23
ee/spec/models/vulnerabilities/finding/evidence/header_spec.rb
...ec/models/vulnerabilities/finding/evidence/header_spec.rb
+2
-24
ee/spec/models/vulnerabilities/finding/evidence/request_spec.rb
...c/models/vulnerabilities/finding/evidence/request_spec.rb
+2
-0
ee/spec/models/vulnerabilities/finding/evidence/response_spec.rb
.../models/vulnerabilities/finding/evidence/response_spec.rb
+1
-0
ee/spec/models/vulnerabilities/finding/evidence/source_spec.rb
...ec/models/vulnerabilities/finding/evidence/source_spec.rb
+2
-0
ee/spec/models/vulnerabilities/finding/evidence/supporting_message_spec.rb
...lnerabilities/finding/evidence/supporting_message_spec.rb
+1
-0
ee/spec/support/shared_examples/models/concerns/any_field_validation_shared_examples.rb
...s/models/concerns/any_field_validation_shared_examples.rb
+19
-0
spec/validators/any_field_validator_spec.rb
spec/validators/any_field_validator_spec.rb
+44
-0
No files found.
.rubocop_manual_todo.yml
View file @
773d965b
...
...
@@ -1685,6 +1685,7 @@ Gitlab/NamespacedClass:
-
'
app/uploaders/personal_file_uploader.rb'
-
'
app/validators/abstract_path_validator.rb'
-
'
app/validators/addressable_url_validator.rb'
-
'
app/validators/any_field_validator.rb'
-
'
app/validators/array_members_validator.rb'
-
'
app/validators/branch_filter_validator.rb'
-
'
app/validators/certificate_fingerprint_validator.rb'
...
...
app/models/concerns/any_field_validation.rb
deleted
100644 → 0
View file @
f10d4e52
# frozen_string_literal: true
# This module enables a record to be valid if any field is present
#
# Overwrite one_of_required_fields to set one of which fields must be present
module
AnyFieldValidation
extend
ActiveSupport
::
Concern
included
do
validate
:any_field_present
end
private
def
any_field_present
return
unless
one_of_required_fields
.
all?
{
|
field
|
self
[
field
].
blank?
}
errors
.
add
(
:base
,
_
(
"At least one field of %{one_of_required_fields} must be present"
)
%
{
one_of_required_fields:
one_of_required_fields
})
end
def
one_of_required_fields
raise
NotImplementedError
end
end
app/validators/any_field_validator.rb
0 → 100644
View file @
773d965b
# frozen_string_literal: true
# AnyFieldValidator
#
# Custom validator that checks if any of the provided
# fields are present to ensure creation of a non-empty
# record
#
# Example:
#
# class MyModel < ApplicationRecord
# validates_with AnyFieldValidator, fields: %w[type name url]
# end
class
AnyFieldValidator
<
ActiveModel
::
Validator
def
initialize
(
*
args
)
super
if
options
[
:fields
].
blank?
raise
'Provide the fields options'
end
end
def
validate
(
record
)
return
unless
one_of_required_fields
.
all?
{
|
field
|
record
[
field
].
blank?
}
record
.
errors
.
add
(
:base
,
_
(
"At least one field of %{one_of_required_fields} must be present"
)
%
{
one_of_required_fields:
one_of_required_fields
})
end
private
def
one_of_required_fields
options
[
:fields
]
end
end
ee/app/models/vulnerabilities/finding/evidence/asset.rb
View file @
773d965b
...
...
@@ -4,10 +4,10 @@ module Vulnerabilities
class
Finding
class
Evidence
class
Asset
<
ApplicationRecord
include
AnyFieldValidation
self
.
table_name
=
'vulnerability_finding_evidence_assets'
DATA_FIELDS
=
%w[type name url]
.
freeze
belongs_to
:evidence
,
class_name:
'Vulnerabilities::Finding::Evidence'
,
inverse_of: :assets
,
...
...
@@ -17,14 +17,7 @@ module Vulnerabilities
validates
:type
,
length:
{
maximum:
2048
}
validates
:name
,
length:
{
maximum:
2048
}
validates
:url
,
length:
{
maximum:
2048
}
validate
:any_field_present
private
def
one_of_required_fields
[
:type
,
:name
,
:url
]
end
validates_with
AnyFieldValidator
,
fields:
DATA_FIELDS
end
end
end
...
...
ee/app/models/vulnerabilities/finding/evidence/header.rb
View file @
773d965b
...
...
@@ -9,8 +9,8 @@ module Vulnerabilities
belongs_to
:request
,
class_name:
'Vulnerabilities::Finding::Evidence::Request'
,
inverse_of: :headers
,
foreign_key:
'vulnerability_finding_evidence_request_id'
belongs_to
:response
,
class_name:
'Vulnerabilities::Finding::Evidence::Response'
,
inverse_of: :headers
,
foreign_key:
'vulnerability_finding_evidence_response_id'
validates
:name
,
length:
{
maximum:
255
}
validates
:value
,
length:
{
maximum:
8192
}
validates
:name
,
length:
{
maximum:
255
}
,
presence:
true
validates
:value
,
length:
{
maximum:
8192
}
,
presence:
true
validate
:request_or_response_is_set
validate
:request_and_response_cannot_be_set
...
...
ee/app/models/vulnerabilities/finding/evidence/request.rb
View file @
773d965b
...
...
@@ -8,6 +8,8 @@ module Vulnerabilities
self
.
table_name
=
'vulnerability_finding_evidence_requests'
DATA_FIELDS
=
%w[method url]
.
freeze
belongs_to
:evidence
,
class_name:
'Vulnerabilities::Finding::Evidence'
,
inverse_of: :request
,
...
...
@@ -26,6 +28,7 @@ module Vulnerabilities
validates
:method
,
length:
{
maximum:
32
}
validates
:url
,
length:
{
maximum:
2048
}
validates_with
AnyFieldValidator
,
fields:
DATA_FIELDS
end
end
end
...
...
ee/app/models/vulnerabilities/finding/evidence/response.rb
View file @
773d965b
...
...
@@ -24,7 +24,7 @@ module Vulnerabilities
inverse_of: :response
,
foreign_key:
'vulnerability_finding_evidence_response_id'
validates
:reason_phrase
,
length:
{
maximum:
2048
}
validates
:reason_phrase
,
length:
{
maximum:
2048
}
,
presence:
true
end
end
end
...
...
ee/app/models/vulnerabilities/finding/evidence/source.rb
View file @
773d965b
...
...
@@ -6,10 +6,13 @@ module Vulnerabilities
class
Source
<
ApplicationRecord
self
.
table_name
=
'vulnerability_finding_evidence_sources'
DATA_FIELDS
=
%w[name url]
.
freeze
belongs_to
:evidence
,
class_name:
'Vulnerabilities::Finding::Evidence'
,
inverse_of: :source
,
foreign_key:
'vulnerability_finding_evidence_id'
,
optional:
false
validates
:name
,
length:
{
maximum:
2048
}
validates
:url
,
length:
{
maximum:
2048
}
validates_with
AnyFieldValidator
,
fields:
DATA_FIELDS
end
end
end
...
...
ee/app/models/vulnerabilities/finding/evidence/supporting_message.rb
View file @
773d965b
...
...
@@ -11,7 +11,7 @@ module Vulnerabilities
has_one
:request
,
class_name:
'Vulnerabilities::Finding::Evidence::Request'
,
inverse_of: :supporting_message
,
foreign_key:
'vulnerability_finding_evidence_supporting_message_id'
has_one
:response
,
class_name:
'Vulnerabilities::Finding::Evidence::Response'
,
inverse_of: :supporting_message
,
foreign_key:
'vulnerability_finding_evidence_supporting_message_id'
validates
:name
,
length:
{
maximum:
2048
}
validates
:name
,
length:
{
maximum:
2048
}
,
presence:
true
end
end
end
...
...
ee/spec/models/vulnerabilities/finding/evidence/asset_spec.rb
View file @
773d965b
...
...
@@ -9,27 +9,5 @@ RSpec.describe Vulnerabilities::Finding::Evidence::Asset do
it
{
is_expected
.
to
validate_length_of
(
:name
).
is_at_most
(
2048
)
}
it
{
is_expected
.
to
validate_length_of
(
:url
).
is_at_most
(
2048
)
}
describe
'.any_field_present'
do
let_it_be
(
:evidence
)
{
build
(
:vulnerabilties_finding_evidence
)
}
let_it_be
(
:asset
)
{
Vulnerabilities
::
Finding
::
Evidence
::
Asset
.
new
(
evidence:
evidence
)
}
it
'is invalid if there are no fields present'
do
expect
(
asset
).
not_to
be_valid
end
it
'validates if there is only a type'
do
asset
.
type
=
'asset-type'
expect
(
asset
).
to
be_valid
end
it
'validates if there is only a name'
do
asset
.
type
=
'asset-name'
expect
(
asset
).
to
be_valid
end
it
'validates if there is only a url'
do
asset
.
type
=
'asset-url.example'
expect
(
asset
).
to
be_valid
end
end
it_behaves_like
'validates presence of any field'
end
ee/spec/models/vulnerabilities/finding/evidence/header_spec.rb
View file @
773d965b
...
...
@@ -7,29 +7,7 @@ RSpec.describe Vulnerabilities::Finding::Evidence::Header do
it
{
is_expected
.
to
belong_to
(
:response
).
class_name
(
'Vulnerabilities::Finding::Evidence::Response'
).
inverse_of
(
:headers
).
optional
}
it
{
is_expected
.
to
validate_length_of
(
:name
).
is_at_most
(
255
)
}
it
{
is_expected
.
to
validate_presence_of
(
:name
)
}
it
{
is_expected
.
to
validate_length_of
(
:value
).
is_at_most
(
8192
)
}
describe
'.request_or_response_is_set'
do
let
(
:header
)
{
build
(
:vulnerabilties_finding_evidence_header
)
}
it
'is invalid if there is no request or response'
do
expect
(
header
).
not_to
be_valid
end
it
'validates if there is a response'
do
header
.
response
=
build
(
:vulnerabilties_finding_evidence_response
)
expect
(
header
).
to
be_valid
end
it
'validates if there is a request'
do
header
.
request
=
build
(
:vulnerabilties_finding_evidence_request
)
expect
(
header
).
to
be_valid
end
it
'is invalid if there is a request and a response'
do
header
.
request
=
build
(
:vulnerabilties_finding_evidence_request
)
header
.
response
=
build
(
:vulnerabilties_finding_evidence_response
)
expect
(
header
).
not_to
be_valid
end
end
it
{
is_expected
.
to
validate_presence_of
(
:value
)
}
end
ee/spec/models/vulnerabilities/finding/evidence/request_spec.rb
View file @
773d965b
...
...
@@ -11,4 +11,6 @@ RSpec.describe Vulnerabilities::Finding::Evidence::Request do
it
{
is_expected
.
to
validate_length_of
(
:url
).
is_at_most
(
2048
)
}
it_behaves_like
'body shared examples'
,
:vulnerabilties_finding_evidence_request
it_behaves_like
'validates presence of any field'
end
ee/spec/models/vulnerabilities/finding/evidence/response_spec.rb
View file @
773d965b
...
...
@@ -8,6 +8,7 @@ RSpec.describe Vulnerabilities::Finding::Evidence::Response do
it
{
is_expected
.
to
have_many
(
:headers
).
class_name
(
'Vulnerabilities::Finding::Evidence::Header'
).
with_foreign_key
(
'vulnerability_finding_evidence_response_id'
).
inverse_of
(
:response
)
}
it
{
is_expected
.
to
validate_length_of
(
:reason_phrase
).
is_at_most
(
2048
)
}
it
{
is_expected
.
to
validate_presence_of
(
:reason_phrase
)
}
it_behaves_like
'body shared examples'
,
:vulnerabilties_finding_evidence_response
end
ee/spec/models/vulnerabilities/finding/evidence/source_spec.rb
View file @
773d965b
...
...
@@ -7,4 +7,6 @@ RSpec.describe Vulnerabilities::Finding::Evidence::Source do
it
{
is_expected
.
to
validate_length_of
(
:name
).
is_at_most
(
2048
)
}
it
{
is_expected
.
to
validate_length_of
(
:url
).
is_at_most
(
2048
)
}
it_behaves_like
'validates presence of any field'
end
ee/spec/models/vulnerabilities/finding/evidence/supporting_message_spec.rb
View file @
773d965b
...
...
@@ -8,4 +8,5 @@ RSpec.describe Vulnerabilities::Finding::Evidence::SupportingMessage do
it
{
is_expected
.
to
have_one
(
:response
).
class_name
(
'Vulnerabilities::Finding::Evidence::Response'
).
with_foreign_key
(
'vulnerability_finding_evidence_supporting_message_id'
).
inverse_of
(
:supporting_message
)
}
it
{
is_expected
.
to
validate_length_of
(
:name
).
is_at_most
(
2048
)
}
it
{
is_expected
.
to
validate_presence_of
(
:name
)
}
end
ee/spec/support/shared_examples/models/concerns/any_field_validation_shared_examples.rb
0 → 100644
View file @
773d965b
# frozen_string_literal: true
RSpec
.
shared_examples
'validates presence of any field'
do
describe
'.any_field_present'
do
let_it_be
(
:evidence
)
{
build
(
:vulnerabilties_finding_evidence
)
}
let_it_be
(
:test_object
)
{
described_class
.
new
(
evidence:
evidence
)
}
it
'is invalid if there are no fields present'
do
expect
(
test_object
).
not_to
be_valid
end
described_class
::
DATA_FIELDS
.
each
do
|
field
|
it
"validates on a single field present when
#{
field
}
is set"
do
test_object
[
field
]
=
"test-object-
#{
field
}
"
expect
(
test_object
).
to
be_valid
end
end
end
end
spec/validators/any_field_validator_spec.rb
0 → 100644
View file @
773d965b
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
AnyFieldValidator
do
context
'when validation is instantiated correctly'
do
let
(
:validated_class
)
do
Class
.
new
(
ApplicationRecord
)
do
self
.
table_name
=
'vulnerabilities'
validates_with
AnyFieldValidator
,
fields:
%w(title description)
end
end
it
'raises an error if no fields are defined'
do
validated_object
=
validated_class
.
new
expect
(
validated_object
.
valid?
).
to
be_falsey
expect
(
validated_object
.
errors
.
messages
)
.
to
eq
(
base:
[
"At least one field of %{one_of_required_fields} must be present"
%
{
one_of_required_fields:
%w(title description)
}])
end
it
'validates if only one field is present'
do
validated_object
=
validated_class
.
new
(
title:
'Vulnerability title'
)
expect
(
validated_object
.
valid?
).
to
be_truthy
end
end
context
'when validation is missing the fields parameter'
do
let
(
:invalid_class
)
do
Class
.
new
(
ApplicationRecord
)
do
self
.
table_name
=
'vulnerabilities'
validates_with
AnyFieldValidator
end
end
it
'raises an error'
do
expect
{
invalid_class
.
new
}.
to
raise_error
(
RuntimeError
)
end
end
end
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