-
Notifications
You must be signed in to change notification settings - Fork 400
Introduce RootConfig.validate_config()
which can be subclassed in HomeServerConfig
to do cross-config class validation
#19027
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
39c719e
3b282c4
4ac0690
260f137
3632588
92fd225
f39fd82
886c35f
daedb9f
3faa7ea
1551f4a
5df163b
dbd0297
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Introduce `RootConfig.validate_config()` which can be subclassed in `HomeServerConfig` to do cross-config class validation. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -545,18 +545,22 @@ def generate_config( | |
|
||
@classmethod | ||
def load_config( | ||
cls: Type[TRootConfig], description: str, argv: List[str] | ||
cls: Type[TRootConfig], description: str, argv_options: List[str] | ||
) -> TRootConfig: | ||
"""Parse the commandline and config files | ||
|
||
Doesn't support config-file-generation: used by the worker apps. | ||
|
||
Args: | ||
description: TODO | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
argv_options: The options passed to Synapse. Usually `sys.argv[1:]`. | ||
|
||
Returns: | ||
Config object. | ||
""" | ||
config_parser = argparse.ArgumentParser(description=description) | ||
cls.add_arguments_to_parser(config_parser) | ||
obj, _ = cls.load_config_with_parser(config_parser, argv) | ||
obj, _ = cls.load_config_with_parser(config_parser, argv_options) | ||
|
||
return obj | ||
|
||
|
@@ -609,6 +613,10 @@ def load_config_with_parser( | |
|
||
Used for workers where we want to add extra flags/subcommands. | ||
|
||
Note: This is the common denominator for loading config and is also used by | ||
`load_config` and `load_or_generate_config`. Which is why we call | ||
`validate_config()` here. | ||
|
||
Args: | ||
parser | ||
argv_options: The options passed to Synapse. Usually `sys.argv[1:]`. | ||
|
@@ -642,6 +650,10 @@ def load_config_with_parser( | |
|
||
obj.invoke_all("read_arguments", config_args) | ||
|
||
# Now that we finally have the full config sections parsed, allow subclasses to | ||
# do some extra validation across the entire config. | ||
obj.validate_config() | ||
|
||
return obj, config_args | ||
|
||
@classmethod | ||
|
@@ -842,15 +854,7 @@ def load_or_generate_config( | |
): | ||
return None | ||
|
||
obj.parse_config_dict( | ||
config_dict, | ||
config_dir_path=config_dir_path, | ||
data_dir_path=data_dir_path, | ||
allow_secrets_in_config=config_args.secrets_in_config, | ||
) | ||
obj.invoke_all("read_arguments", config_args) | ||
|
||
return obj | ||
return cls.load_config(description, argv_options) | ||
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 change makes it so we go through the normal It also means we can settle on calling |
||
|
||
def parse_config_dict( | ||
self, | ||
|
@@ -911,6 +915,20 @@ def reload_config_section(self, section_name: str) -> Config: | |
existing_config.root = None | ||
return existing_config | ||
|
||
def validate_config(self) -> None: | ||
""" | ||
Additional config validation across all config sections. | ||
|
||
Override this in subclasses to add extra validation. This is called once all | ||
config option values have been populated. | ||
|
||
XXX: This should only validate, not modify the configuration, as the final | ||
config state is required for proper validation across all config sections. | ||
|
||
Raises: | ||
ConfigError: if the config is invalid. | ||
""" | ||
|
||
|
||
def read_config_files(config_files: Iterable[str]) -> Dict[str, Any]: | ||
"""Read the config files and shallowly merge them into a dict. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,8 @@ | |
# [This file includes modifications made by New Vector Limited] | ||
# | ||
# | ||
from ._base import RootConfig | ||
|
||
from ._base import ConfigError, RootConfig | ||
from .account_validity import AccountValidityConfig | ||
from .api import ApiConfig | ||
from .appservice import AppServiceConfig | ||
|
@@ -67,6 +68,10 @@ | |
|
||
|
||
class HomeServerConfig(RootConfig): | ||
""" | ||
Top-level config object for Synapse homeserver (main process and workers). | ||
""" | ||
|
||
config_classes = [ | ||
ModulesConfig, | ||
ServerConfig, | ||
|
@@ -115,3 +120,22 @@ class HomeServerConfig(RootConfig): | |
# This must be last, as it checks for conflicts with other config options. | ||
MasConfig, | ||
] | ||
|
||
def validate_config( | ||
self, | ||
) -> None: | ||
if ( | ||
self.registration.enable_registration | ||
and not self.registration.enable_registration_without_verification | ||
): | ||
if ( | ||
not self.captcha.enable_registration_captcha | ||
and not self.registration.registrations_require_3pid | ||
and not self.registration.registration_requires_token | ||
): | ||
raise ConfigError( | ||
"You have enabled open registration without any verification. This is a known vector for " | ||
"spam and abuse. If you would like to allow public registration, please consider adding email, " | ||
"captcha, or token-based verification. Otherwise this check can be removed by setting the " | ||
"`enable_registration_without_verification` config option to `true`." | ||
) | ||
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. The only real change is that this has moved from a place that only runs for the main Synapse instance to everything including workers. But I assume that's fine. We want the same warning for workers as we wouldn't want open registration there either. |
Uh oh!
There was an error while loading. Please reload this page.