Skip to content

Commit 6e848a1

Browse files
committed
Add validation for scopes attribute for secure param
1 parent cd649b1 commit 6e848a1

File tree

5 files changed

+173
-15
lines changed

5 files changed

+173
-15
lines changed

config/locales/en.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,10 @@ en:
239239
password_parameter_type_deprecated: 'Password parameter type can no longer
240240
be used. Use Secure settings instead. Learn more: %{link}.'
241241
scope_requires_secure_parameter: Scopes can only be defined on secure
242-
parameters.
243-
scopes_cannot_be_empty: Scopes cannot be empty when defined.
244-
invalid_secure_parameter_scopes: 'Invalid secure parameter scopes: %{invalid_scope}.'
242+
parameters for %{field}.
243+
scopes_cannot_be_empty: Scopes cannot be empty when defined for %{field}.
244+
invalid_secure_parameter_scopes: 'Invalid secure parameter scopes for
245+
%{field}: %{scopes}.'
245246
warning:
246247
app_build:
247248
deprecated_version: You are targeting a deprecated version of the framework.

config/locales/translations/zendesk_apps_support.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -628,14 +628,14 @@ parts:
628628
title: "App builder job: Password parameter type is deprecated"
629629
value: "Password parameter type can no longer be used. Use Secure settings instead. Learn more: %{link}."
630630
- translation:
631-
key: "txt.apps.admin.error.app_build.scope_requires_secure_parameter"
631+
key: "txt.apps.admin.error.app_build.scopes_require_secure_parameter"
632632
title: "App builder job: scopes can be defined only on secure parameters"
633-
value: "Scopes can only be defined on secure parameters."
633+
value: "Scopes can only be defined on secure parameters for %{field}."
634634
- translation:
635635
key: "txt.apps.admin.error.app_build.scopes_cannot_be_empty"
636636
title: "App builder job: scopes cannot be empty when defined"
637-
value: "Scopes cannot be empty when defined."
637+
value: "Scopes cannot be empty when defined for %{field}."
638638
- translation:
639639
key: "txt.apps.admin.error.app_build.invalid_secure_parameter_scopes"
640640
title: "App builder job: invalid secure parameter scopes"
641-
value: "Invalid secure parameter scopes: %{invalid_scope}."
641+
value: "Invalid secure parameter scopes for %{field}: %{scopes}."

lib/zendesk_apps_support/manifest/parameter.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ZendeskAppsSupport
44
class Manifest
55
class Parameter
66
TYPES = %w[text password checkbox url number multiline hidden oauth].freeze
7-
ATTRIBUTES = %i[name type required secure default].freeze
7+
ATTRIBUTES = %i[name type required secure default scopes].freeze
88
attr_reader(*ATTRIBUTES)
99
def default?
1010
@has_default
@@ -17,6 +17,7 @@ def initialize(p)
1717
@secure = p['secure'] || false
1818
@has_default = p.key? 'default'
1919
@default = p['default'] if @has_default
20+
@scopes = p['scopes']
2021
end
2122
end
2223
end

lib/zendesk_apps_support/validations/manifest.rb

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ module Manifest
1111
OAUTH_REQUIRED_FIELDS = %w[client_id client_secret authorize_uri access_token_uri].freeze
1212
PARAMETER_TYPES = ZendeskAppsSupport::Manifest::Parameter::TYPES
1313
OAUTH_MANIFEST_LINK = 'https://developer.zendesk.com/apps/docs/developer-guide/manifest#oauth'
14+
SECURE_PARAM_SCOPES = %w[header body url_host url_path jwt_secret_key jwt_claim basic_auth_username basic_auth_password].freeze
1415

1516
class << self
16-
def call(package, error_on_password_parameter: false)
17+
def call(package, error_on_password_parameter: false, validate_scopes_for_secure_parameter: false)
1718
unless package.has_file?('manifest.json')
1819
nested_manifest = package.files.find { |file| file =~ %r{\A[^/]+?/manifest\.json\Z} }
1920
if nested_manifest
@@ -24,7 +25,7 @@ def call(package, error_on_password_parameter: false)
2425

2526
package.warnings << password_parameter_warning if !error_on_password_parameter && password_param_present?(package.manifest)
2627

27-
collate_manifest_errors(package, error_on_password_parameter)
28+
collate_manifest_errors(package, error_on_password_parameter, validate_scopes_for_secure_parameter)
2829
rescue JSON::ParserError => e
2930
return [ValidationError.new(:manifest_not_json, errors: e)]
3031
rescue ZendeskAppsSupport::Manifest::OverrideError => e
@@ -37,7 +38,7 @@ def password_param_present?(manifest)
3738
manifest.parameters.any? { |p| p.type == 'password' }
3839
end
3940

40-
def collate_manifest_errors(package, error_on_password_parameter)
41+
def collate_manifest_errors(package, error_on_password_parameter, validate_scopes_for_secure_parameter)
4142
manifest = package.manifest
4243

4344
errors = [
@@ -46,7 +47,7 @@ def collate_manifest_errors(package, error_on_password_parameter)
4647
oauth_error(manifest),
4748
default_locale_error(manifest, package),
4849
validate_urls(manifest),
49-
validate_parameters(manifest),
50+
validate_parameters(manifest, validate_scopes_for_secure_parameter),
5051
if manifest.requirements_only? || manifest.marketing_only?
5152
[ ban_location(manifest),
5253
ban_framework_version(manifest) ]
@@ -87,7 +88,7 @@ def validate_urls(manifest)
8788
errors
8889
end
8990

90-
def validate_parameters(manifest)
91+
def validate_parameters(manifest, validate_scopes_for_secure_parameter)
9192
if manifest.marketing_only?
9293
marketing_only_errors(manifest)
9394
else
@@ -97,7 +98,8 @@ def validate_parameters(manifest)
9798
invalid_type_error(manifest),
9899
too_many_oauth_parameters(manifest),
99100
oauth_cannot_be_secure(manifest),
100-
name_as_parameter_name_error(manifest)
101+
name_as_parameter_name_error(manifest),
102+
*(validate_scopes_for_secure_parameter ? invalid_secure_param_scopes_errors(manifest) : [])
101103
]
102104
end
103105
end
@@ -110,6 +112,36 @@ def oauth_cannot_be_secure(manifest)
110112
end
111113
end
112114

115+
def invalid_secure_param_scopes_errors(manifest)
116+
errors = []
117+
118+
manifest.parameters.each do |parameter|
119+
next if parameter.scopes.nil?
120+
121+
unless parameter.scopes.is_a?(Array)
122+
errors << ValidationError.new(:unacceptable_array,
123+
field: "parameters.#{parameter.name}.scopes",
124+
value: parameter.scopes.class.to_s)
125+
end
126+
127+
unless parameter.secure
128+
errors << ValidationError.new(:scopes_require_secure_parameter, field: "parameters.#{parameter.name}")
129+
end
130+
131+
if parameter.scopes.empty?
132+
errors << ValidationError.new(:scopes_cannot_be_empty, field: "parameters.#{parameter.name}")
133+
end
134+
135+
invalid_scopes = parameter.scopes.reject { |scope| SECURE_PARAM_SCOPES.include?(scope) }
136+
if invalid_scopes.any?
137+
errors << ValidationError.new(:invalid_secure_parameter_scopes,
138+
field: "parameters.#{parameter.name}", scopes: invalid_scopes)
139+
end
140+
end
141+
142+
errors
143+
end
144+
113145
def marketing_only_errors(manifest)
114146
[
115147
ban_parameters(manifest),

spec/validations/manifest_spec.rb

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def create_package(parameter_hash)
2424

2525
RSpec::Matchers.define :have_error do |error|
2626
match do |package|
27-
errors = ZendeskAppsSupport::Validations::Manifest.call(package)
27+
errors = ZendeskAppsSupport::Validations::Manifest.call(package, validate_scopes_for_secure_parameter: true)
2828
errors.map!(&:to_s) unless error.is_a? Symbol
2929
@actual = errors.compact
3030

@@ -864,4 +864,128 @@ def create_package(parameter_hash)
864864
end
865865
end
866866
end
867+
868+
context 'scope parameter validations' do
869+
context 'when validate_scopes_for_secure_parameter is false' do
870+
it 'should not validate scopes even if parameter has invalid scopes' do
871+
@manifest_hash = {
872+
'parameters' => [
873+
{
874+
'name' => 'api_token',
875+
'type' => 'text',
876+
'secure' => false,
877+
'scopes' => ['header']
878+
}
879+
]
880+
}
881+
package = create_package(@manifest_hash)
882+
errors = ZendeskAppsSupport::Validations::Manifest.call(package, validate_scopes_for_secure_parameter: false)
883+
expect(errors.find { |e| e.key == :scopes_require_secure_parameter }).to be_nil
884+
end
885+
end
886+
887+
context 'when validate_scopes_for_secure_parameter is true' do
888+
context 'when a parameter has scopes but is not secure' do
889+
it 'should have an error requiring secure parameter for scopes' do
890+
@manifest_hash = {
891+
'parameters' => [
892+
{
893+
'name' => 'api_token',
894+
'type' => 'text',
895+
'secure' => false,
896+
'scopes' => ['header']
897+
}
898+
]
899+
}
900+
package = create_package(@manifest_hash)
901+
errors = ZendeskAppsSupport::Validations::Manifest.call(package, validate_scopes_for_secure_parameter: true)
902+
expect(errors.find { |e| e.key == :scopes_require_secure_parameter }).not_to be_nil
903+
end
904+
end
905+
end
906+
907+
context 'when a parameter has scopes but is not secure' do
908+
it 'should have an error requiring secure parameter for scopes' do
909+
@manifest_hash = {
910+
'parameters' => [
911+
{
912+
'name' => 'api_token',
913+
'type' => 'text',
914+
'secure' => false,
915+
'scopes' => ['header']
916+
}
917+
]
918+
}
919+
expect(create_package(@manifest_hash)).to have_error(:scopes_require_secure_parameter)
920+
end
921+
end
922+
923+
context 'when a parameter has scopes and is secure' do
924+
it 'should not have an error for valid scope values' do
925+
@manifest_hash = {
926+
'parameters' => [
927+
{
928+
'name' => 'api_token',
929+
'type' => 'text',
930+
'secure' => true,
931+
'scopes' => ['header', 'body']
932+
}
933+
]
934+
}
935+
expect(create_package(@manifest_hash)).not_to have_error(:scopes_require_secure_parameter)
936+
expect(create_package(@manifest_hash)).not_to have_error(:invalid_secure_parameter_scopes)
937+
end
938+
939+
it 'should have an error for invalid scope values with all invalid scopes in the error' do
940+
multiple_invalid_manifest = {
941+
'parameters' => [
942+
{
943+
'name' => 'api_token',
944+
'type' => 'text',
945+
'secure' => true,
946+
'scopes' => ['invalid_scope1', 'header', 'invalid_scope2', 'body', 'invalid_scope3']
947+
}
948+
]
949+
}
950+
@manifest_hash = multiple_invalid_manifest
951+
package = create_package(@manifest_hash)
952+
errors = ZendeskAppsSupport::Validations::Manifest.call(package, validate_scopes_for_secure_parameter: true)
953+
invalid_scope_errors = errors.select { |e| e.key == :invalid_secure_parameter_scopes }
954+
955+
expect(invalid_scope_errors.length).to eq(1)
956+
expect(invalid_scope_errors.first.instance_variable_get(:@data)[:scopes]).to eq(['invalid_scope1', 'invalid_scope2', 'invalid_scope3'])
957+
end
958+
959+
it 'should have an error when scopes are empty' do
960+
@manifest_hash = {
961+
'parameters' => [
962+
{
963+
'name' => 'api_token',
964+
'type' => 'text',
965+
'secure' => true,
966+
'scopes' => []
967+
}
968+
]
969+
}
970+
expect(create_package(@manifest_hash)).to have_error(:scopes_cannot_be_empty)
971+
end
972+
973+
end
974+
975+
context 'when a parameter has no scopes' do
976+
it 'should not have any scope-related errors' do
977+
@manifest_hash = {
978+
'parameters' => [
979+
{
980+
'name' => 'api_token',
981+
'type' => 'text',
982+
'secure' => true
983+
}
984+
]
985+
}
986+
expect(create_package(@manifest_hash)).not_to have_error(:scopes_require_secure_parameter)
987+
expect(create_package(@manifest_hash)).not_to have_error(:invalid_secure_parameter_scopes)
988+
end
989+
end
990+
end
867991
end

0 commit comments

Comments
 (0)