Skip to content

Commit cdc0962

Browse files
committed
upgrade semver validation to disallow leading zeros and overly long versions
1 parent 8add151 commit cdc0962

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

addons/mod_loader/mod_manifest.gd

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extends Resource
22
# Stores and validates contents of the manifest set by the user
33
class_name ModManifest
44

5+
const LOG_NAME := "ModLoader:ModManifest"
56

67
# Mod name.
78
# Validated by [method is_name_or_namespace_valid]
@@ -95,7 +96,7 @@ func get_package_id() -> String:
9596

9697
# A valid namespace may only use letters (any case), numbers and underscores
9798
# and has to be longer than 3 characters
98-
# /^[a-zA-Z0-9_]{3,}$/
99+
# a-z A-Z 0-9 _ (longer than 3 characters)
99100
static func is_name_or_namespace_valid(name: String) -> bool:
100101
var re := RegEx.new()
101102
re.compile("^[a-zA-Z0-9_]*$") # alphanumeric and _
@@ -114,14 +115,22 @@ static func is_name_or_namespace_valid(name: String) -> bool:
114115

115116
# A valid semantic version should follow this format: {mayor}.{minor}.{patch}
116117
# reference https://semver.org/ for details
117-
# /^[0-9]+\\.[0-9]+\\.[0-9]+$/
118+
# {0-9}.{0-9}.{0-9} (no leading 0, shorter than 16 characters total)
118119
static func is_semver_valid(version_number: String) -> bool:
119120
var re := RegEx.new()
120-
re.compile("^[0-9]+\\.[0-9]+\\.[0-9]+$")
121+
re.compile("^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$")
121122

122123
if re.search(version_number) == null:
123-
printerr('Invalid semantic version: "%s". ' +
124-
'You may only use numbers and periods in this format {mayor}.{minor}.{patch}' % version_number)
124+
ModLoaderUtils.log_fatal('Invalid semantic version: "%s". ' +
125+
'You may only use numbers without leading zero and periods following this format {mayor}.{minor}.{patch}' % version_number,
126+
LOG_NAME
127+
)
128+
return false
129+
130+
if version_number.length() > 16:
131+
ModLoaderUtils.log_fatal('Invalid semantic version: "%s". ' +
132+
'Version number must be shorter than 16 characters.', LOG_NAME
133+
)
125134
return false
126135

127136
return true

0 commit comments

Comments
 (0)