-
Notifications
You must be signed in to change notification settings - Fork 834
Add validation to ensure that settings.configs values are dictionaries, in order to prevent misuse #1547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ryu0118
wants to merge
23
commits into
yonaskolb:master
Choose a base branch
from
Ryu0118:validate_invalid_configs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add validation to ensure that settings.configs values are dictionaries, in order to prevent misuse #1547
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0cdd80d
Add validation to ensure settings.configs values are dictionaries to …
Ryu0118 58cdf9b
Add tests for invalid settings.configs value formats
Ryu0118 6af2709
Merge branch 'master' into validate_invalid_configs
Ryu0118 5c164b2
Replaced with filter and split into a function
Ryu0118 d90866f
Rename invalidConfigsFormat to invalidConfigsMappingFormat
Ryu0118 eac4831
Add comments to explain invalid fixture
Ryu0118 8d90c4a
Rename test fixture
Ryu0118 340b8e6
Update CHANGELOG.md
Ryu0118 1b32082
Correct grammer
Ryu0118 70bf933
Use KeyPath instead of closure
Ryu0118 d9ca225
Rename validateMappingStyleInConfig to extractValidConfigs
Ryu0118 53968a5
Add a document comment for extractValidConfigs(from:)
Ryu0118 c56b072
Use old testing api and remove EquatableErrorBox
Ryu0118 d65fc9e
Rename test case to use "mapping" instead of "dictionary"
Ryu0118 cb2f605
Add ValidSettingsExtractor to encapsulate the logic for converting a …
Ryu0118 54081ce
Add settings validation for both Target and AggregateTarget
Ryu0118 24040e7
Add tests for invalid settings.configs in Target and AggregateTarget
Ryu0118 9dc8405
Add document comments for ValidSettingsExtractor
Ryu0118 ca9ed3e
Rename ValidSettingsExtractor to BuildSettingsExtractor
Ryu0118 6b9cc59
Add settings validation for settingGroups
Ryu0118 6f7592d
Add tests for settingGroups
Ryu0118 79d78ca
Rename extract to parse
Ryu0118 03bcec1
Refactor
Ryu0118 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Foundation | ||
import JSONUtilities | ||
|
||
/// A helper for extracting and validating the `Settings` object from a JSON dictionary. | ||
struct BuildSettingsParser { | ||
let jsonDictionary: JSONDictionary | ||
|
||
/// Attempts to extract and parse the `Settings` from the dictionary. | ||
/// | ||
/// - Returns: A valid `Settings` object | ||
func parse() throws -> Settings { | ||
do { | ||
return try jsonDictionary.json(atKeyPath: "settings") | ||
} catch let specParsingError as SpecParsingError { | ||
// Re-throw `SpecParsingError` to prevent the misuse of settings.configs. | ||
throw specParsingError | ||
} catch { | ||
// Ignore all errors except `SpecParsingError` | ||
return .empty | ||
} | ||
} | ||
|
||
/// Attempts to extract and parse setting groups from the dictionary with fallback defaults. | ||
/// | ||
/// - Returns: Parsed setting groups or default groups if parsing fails | ||
func parseSettingGroups() throws -> [String: Settings] { | ||
do { | ||
return try jsonDictionary.json(atKeyPath: "settingGroups", invalidItemBehaviour: .fail) | ||
} catch let specParsingError as SpecParsingError { | ||
// Re-throw `SpecParsingError` to prevent the misuse of settingGroups. | ||
throw specParsingError | ||
} catch { | ||
// Ignore all errors except `SpecParsingError` | ||
return jsonDictionary.json(atKeyPath: "settingPresets") ?? [:] | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Tests/Fixtures/invalid_configs/invalid_configs_value_non_mapping_aggregate_targets.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: InvalidConfigsValueNonMappingAggregateTargets | ||
|
||
# This fixture tests validation of `settings.configs` under an aggregate target. | ||
# Here, `invalid_key0` and `invalid_key1` are scalar values (not mappings), | ||
# so parsing should throw SpecParsingError.invalidConfigsMappingFormat. | ||
targets: | ||
valid_target1: | ||
type: application | ||
platform: iOS | ||
valid_target2: | ||
type: application | ||
platform: iOS | ||
|
||
aggregateTargets: | ||
invalid_target: | ||
targets: | ||
- valid_target1 | ||
- valid_target2 | ||
settings: | ||
configs: | ||
invalid_key0: value0 | ||
debug: | ||
valid_key: value1 | ||
invalid_key1: value2 |
19 changes: 19 additions & 0 deletions
19
Tests/Fixtures/invalid_configs/invalid_configs_value_non_mapping_setting_groups.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: InvalidConfigsValueNonMappingSettingGroups | ||
|
||
# This fixture tests validation of `settings.configs` under an aggregate target. | ||
# Here, `invalid_key0` and `invalid_key1` are scalar values (not mappings), | ||
# so parsing should throw SpecParsingError.invalidConfigsMappingFormat. | ||
settingGroups: | ||
invalid_preset: | ||
configs: | ||
invalid_key0: value0 | ||
debug: | ||
valid_key: value1 | ||
invalid_key1: value2 | ||
targets: | ||
invalid_target: | ||
type: application | ||
platform: iOS | ||
settings: | ||
groups: | ||
- invalid_preset |
11 changes: 11 additions & 0 deletions
11
Tests/Fixtures/invalid_configs/invalid_configs_value_non_mapping_settings.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: InvalidConfigsValueNonMappingSettings | ||
|
||
# This fixture tests validation of `settings.configs` at the top level. | ||
# Here, `invalid_key0` and `invalid_key1` are scalar values (not mappings), | ||
# so parsing should throw SpecParsingError.invalidConfigsMappingFormat. | ||
settings: | ||
configs: | ||
invalid_key0: value0 | ||
debug: | ||
valid_key: value1 | ||
invalid_key1: value2 |
15 changes: 15 additions & 0 deletions
15
Tests/Fixtures/invalid_configs/invalid_configs_value_non_mapping_targets.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: InvalidConfigsValueNonMappingTargets | ||
|
||
# This fixture tests nested validation of `settings.configs` under a target. | ||
# Here, `invalid_key0` and `invalid_key1` are scalar values (not mappings), | ||
# so parsing should throw SpecParsingError.invalidConfigsMappingFormat. | ||
targets: | ||
invalid_target: | ||
type: application | ||
platform: iOS | ||
settings: | ||
configs: | ||
invalid_key0: value0 | ||
debug: | ||
valid_key: value1 | ||
invalid_key1: value2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import ProjectSpec | ||
import Testing | ||
import TestSupport | ||
import PathKit | ||
|
||
struct invalidConfigsMappingFormatTests { | ||
struct InvalidConfigsTestArguments { | ||
var fixturePath: Path | ||
var expectedError: SpecParsingError | ||
} | ||
|
||
private static var testArguments: [InvalidConfigsTestArguments] { | ||
let invalidConfigsFixturePath: Path = fixturePath + "invalid_configs" | ||
return [ | ||
InvalidConfigsTestArguments( | ||
fixturePath: invalidConfigsFixturePath + "invalid_configs_value_non_mapping_settings.yml", | ||
expectedError: SpecParsingError.invalidConfigsMappingFormat(keys: ["invalid_key0", "invalid_key1"]) | ||
), | ||
InvalidConfigsTestArguments( | ||
fixturePath: invalidConfigsFixturePath + "invalid_configs_value_non_mapping_targets.yml", | ||
expectedError: SpecParsingError.invalidConfigsMappingFormat(keys: ["invalid_key0", "invalid_key1"]) | ||
), | ||
InvalidConfigsTestArguments( | ||
fixturePath: invalidConfigsFixturePath + "invalid_configs_value_non_mapping_aggregate_targets.yml", | ||
expectedError: SpecParsingError.invalidConfigsMappingFormat(keys: ["invalid_key0", "invalid_key1"]) | ||
), | ||
InvalidConfigsTestArguments( | ||
fixturePath: invalidConfigsFixturePath + "invalid_configs_value_non_mapping_setting_groups.yml", | ||
expectedError: SpecParsingError.invalidConfigsMappingFormat(keys: ["invalid_key0", "invalid_key1"]) | ||
) | ||
] | ||
} | ||
|
||
@Test("throws invalidConfigsMappingFormat for non-mapping configs entries", arguments: testArguments) | ||
func testInvalidConfigsMappingFormat(_ arguments: InvalidConfigsTestArguments) throws { | ||
#expect { | ||
try Project(path: arguments.fixturePath) | ||
} throws: { actualError in | ||
(actualError as any CustomStringConvertible).description | ||
== arguments.expectedError.description | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.