Class: Releases::Link

Inherits:
ApplicationRecord show all
Defined in:
app/models/releases/link.rb

Constant Summary collapse

FILEPATH_REGEX =

See gitlab.com/gitlab-org/gitlab/-/issues/218753 Regex modified to prevent catastrophic backtracking

%r{\A\/[^\/](?!.*\/\/.*)[\-\.\w\/]+[\da-zA-Z]+\z}.freeze
FILEPATH_MAX_LENGTH =
128

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Instance Method Summary collapse

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Instance Method Details

#external?Boolean

Returns:

  • (Boolean)

40
41
42
# File 'app/models/releases/link.rb', line 40

def external?
  !internal?
end

#filepath_format_valid?Boolean

we use a custom validator here to prevent running the regex if the string is too long see gitlab.com/gitlab-org/gitlab/-/issues/273771

Returns:

  • (Boolean)

21
22
23
24
25
# File 'app/models/releases/link.rb', line 21

def filepath_format_valid?
  return if filepath.nil? # valid use case
  return errors.add(:filepath, "is too long (maximum is #{FILEPATH_MAX_LENGTH} characters)") if filepath.length > FILEPATH_MAX_LENGTH
  return errors.add(:filepath, 'is in an invalid format') unless FILEPATH_REGEX.match? filepath
end

#hook_attrsObject


44
45
46
47
48
49
50
51
52
# File 'app/models/releases/link.rb', line 44

def hook_attrs
  {
    id: id,
    external: external?,
    link_type: link_type,
    name: name,
    url: url
  }
end

#internal?Boolean

Returns:

  • (Boolean)

36
37
38
# File 'app/models/releases/link.rb', line 36

def internal?
  url.start_with?(release.project.web_url)
end