-
Notifications
You must be signed in to change notification settings - Fork 13
ENT-13089: Added validation of module names #247
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,13 +20,17 @@ | |||||||
| """ | ||||||||
|
|
||||||||
| import argparse | ||||||||
| import logging as log | ||||||||
| import sys | ||||||||
| import re | ||||||||
| from collections import OrderedDict | ||||||||
| from typing import List, Tuple | ||||||||
|
|
||||||||
| from cfbs.module import is_module_local | ||||||||
| from cfbs.utils import ( | ||||||||
| is_a_commit_hash, | ||||||||
| strip_left, | ||||||||
| strip_right_any, | ||||||||
| GenericExitError, | ||||||||
| ) | ||||||||
| from cfbs.pretty import TOP_LEVEL_KEYS, MODULE_KEYS | ||||||||
|
|
@@ -166,6 +170,46 @@ def _validate_top_level_keys(config): | |||||||
| ) | ||||||||
|
|
||||||||
|
|
||||||||
| def validate_module_name_content(name): | ||||||||
| MAX_MODULE_NAME_LENGTH = 64 | ||||||||
|
|
||||||||
| if len(name) > MAX_MODULE_NAME_LENGTH: | ||||||||
| raise CFBSValidationError( | ||||||||
| name, | ||||||||
| "Module name is too long (over " | ||||||||
| + str(MAX_MODULE_NAME_LENGTH) | ||||||||
| + " characters)", | ||||||||
| ) | ||||||||
|
|
||||||||
| # lowercase ASCII alphanumericals, starting with a letter, and possible singular dashes in the middle | ||||||||
| r = "[a-z][a-z0-9]*(-[a-z0-9]+)*" | ||||||||
| proper_name = name | ||||||||
|
|
||||||||
| if is_module_local(name): | ||||||||
| if not name.startswith("./"): | ||||||||
| raise CFBSValidationError(name, "Local module names should begin with `./`") | ||||||||
|
Comment on lines
+189
to
+190
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is ensured by
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but I thought this is safer since the implementation could change in the future. |
||||||||
|
|
||||||||
| if not name.endswith((".cf", ".json", "/")): | ||||||||
| raise CFBSValidationError( | ||||||||
| name, | ||||||||
| "Local module names should end with `/` (for directories) or `.json` or `.cf` (for files).", | ||||||||
| ) | ||||||||
|
|
||||||||
| proper_name = strip_left(proper_name, "./") | ||||||||
| proper_name = strip_right_any(proper_name, ("/", ".cf", ".json")) | ||||||||
|
|
||||||||
| # allow underscores, only for local modules | ||||||||
| proper_name = proper_name.replace("_", "-") | ||||||||
|
|
||||||||
| if not re.fullmatch(r, proper_name): | ||||||||
| raise CFBSValidationError( | ||||||||
| name, | ||||||||
| "Module name contains illegal characters (only lowercase ASCII alphanumeric characters are legal)", | ||||||||
| ) | ||||||||
|
|
||||||||
| log.debug("Validated name of module %s" % name) | ||||||||
jakub-nt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
|
|
||||||||
| def _validate_config(config, empty_build_list_ok=False): | ||||||||
| # First validate the config i.e. the user's cfbs.json | ||||||||
| config.warn_about_unknown_keys() | ||||||||
|
|
@@ -183,6 +227,9 @@ def _validate_config(config, empty_build_list_ok=False): | |||||||
| for name, module in raw_data["provides"].items(): | ||||||||
| _validate_module_object("provides", name, module, config) | ||||||||
|
|
||||||||
| if config["type"] == "module": | ||||||||
| validate_module_name_content(config["name"]) | ||||||||
|
|
||||||||
|
|
||||||||
| def validate_config(config, empty_build_list_ok=False): | ||||||||
| try: | ||||||||
|
|
@@ -329,6 +376,7 @@ def validate_alias(name, module, context): | |||||||
| raise CFBSValidationError(name, '"alias" must be of type string') | ||||||||
| if not module["alias"]: | ||||||||
| raise CFBSValidationError(name, '"alias" must be non-empty') | ||||||||
| validate_module_name_content(name) | ||||||||
| if not config.can_reach_dependency(module["alias"], search_in): | ||||||||
| raise CFBSValidationError( | ||||||||
| name, '"alias" must reference another module in the index' | ||||||||
|
|
@@ -344,6 +392,8 @@ def validate_name(name, module): | |||||||
| if not module["name"]: | ||||||||
| raise CFBSValidationError(name, '"name" must be non-empty') | ||||||||
|
|
||||||||
| validate_module_name_content(name) | ||||||||
|
|
||||||||
| def validate_description(name, module): | ||||||||
| assert "description" in module | ||||||||
| if type(module["description"]) != str: | ||||||||
|
|
@@ -640,6 +690,12 @@ def validate_module_input(name, module): | |||||||
| if "input" in module: | ||||||||
| validate_module_input(name, module) | ||||||||
|
|
||||||||
| # Step 4 - Additional validation checks: | ||||||||
|
|
||||||||
| # Validate module name content also when there's no explicit "name" field (for "index" and "provides" project types) | ||||||||
| if "name" not in module: | ||||||||
| validate_module_name_content(name) | ||||||||
|
|
||||||||
|
|
||||||||
| def _validate_config_for_build_field(config, empty_build_list_ok=False): | ||||||||
| """Validate that neccessary fields are in the config for the build/download commands to work""" | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.