From 8dbc52f37683593100e99a4a64982c874b347ba7 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Sat, 14 Nov 2026 11:10:39 +0100 Subject: [PATCH] tools: check crypto backend guard structure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add build/crypto_backend checks for the OpenSSL provider and BoringSSL selectors. Require provider guards to name BoringSSL in the same conditional group. Require #error "Unsupported crypto backend" in the final #else of groups with alternatives; nested errors do not satisfy that requirement. Allow standalone BoringSSL exclusions and ordinary feature, compiler, and version guards. These checks validate structure without evaluating backend conditions or API availability. Signed-off-by: Filip Skokan Assisted-by: Codex PR-URL: https://github.com/nodejs/node/pull/67472 Reviewed-By: James M Snell Reviewed-By: Gürgün Dayıoğlu Reviewed-By: Yagiz Nizipli --- tools/cpplint.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tools/cpplint.py b/tools/cpplint.py index 1e26e3ded39..338bbbce3e4 100755 --- a/tools/cpplint.py +++ b/tools/cpplint.py @@ -300,6 +300,7 @@ Syntax: cpplint.py [--verbose=#] [--output=emacs|eclipse|vs7|junit|sed|gsed] _ERROR_CATEGORIES = [ "build/c++11", "build/c++17", + "build/crypto_backend", "build/deprecated", "build/endif_comment", "build/explicit_make_pair", @@ -7466,6 +7467,62 @@ def CheckThreadLocalUsage(filename, lines, error): 'NOLINTNEXTLINE(runtime/thread_local).') +def CheckCryptoBackendUsage(filename, clean_lines, error): + """Require explicit BoringSSL guards and unsupported-backend fallbacks. + + Gates excluding BoringSSL must name it instead of using the provider + selector. Backend alternatives must explicitly reject unsupported backends; + other feature checks remain valid. + """ + provider = re.compile(r'\bNCRYPTO_USE_OPENSSL_PROVIDER\b') + boring = re.compile(r'\b(?:NCRYPTO_USE_BORINGSSL|OPENSSL_IS_BORINGSSL)\b') + directives = re.compile( + r'^\s*#\s*(if|ifdef|ifndef|elif|elifdef|elifndef|else|endif|error)\b(.*)$') + stack = [] + lines = iter(enumerate(clean_lines.lines)) + for linenum, line in lines: + # Preprocessor conditions can span multiple physical lines. + while line.endswith('\\'): + line = line[:-1] + next(lines, (0, ''))[1] + match = directives.match(line) + if not match: + continue + directive, condition = match.groups() + if directive in ('if', 'ifdef', 'ifndef'): + stack.append({ + 'linenum': linenum, + 'provider': bool(provider.search(condition)), + 'boring': bool(boring.search(condition)), + 'alternative': False, + 'else': False, + 'error': False, + }) + elif stack: + group = stack[-1] + if directive in ('elif', 'elifdef', 'elifndef'): + group['provider'] |= bool(provider.search(condition)) + group['boring'] |= bool(boring.search(condition)) + group['alternative'] = True + elif directive == 'else': + group['alternative'] = True + group['else'] = True + elif directive == 'error' and group['else']: + if condition.strip() == '"Unsupported crypto backend"': + group['error'] = True + elif directive == 'endif': + stack.pop() + if group['provider'] and not group['boring']: + error(filename, group['linenum'], 'build/crypto_backend', 5, + 'Crypto backend guards must name BoringSSL explicitly; ' + 'use #if !NCRYPTO_USE_BORINGSSL for BoringSSL exclusions.') + elif ((group['provider'] or group['boring']) and group['alternative'] and + not (group['else'] and group['error'])): + error(filename, group['linenum'], 'build/crypto_backend', 5, + 'Crypto backend alternatives must end with ' + '#else and #error "Unsupported crypto backend"; ' + 'use an explicit #elif for each supported backend.') + + def ProcessLine( filename, file_extension, @@ -7631,6 +7688,8 @@ def ProcessFileData(filename, file_extension, lines, error, extra_check_function CheckThreadLocalUsage(filename, lines, error) + CheckCryptoBackendUsage(filename, clean_lines, error) + def ProcessConfigOverrides(filename): """Loads the configuration files and processes the config overrides.