|
| 1 | +#!/bin/bash |
| 2 | +# validate-dnr-rules - Validate Declarative Net Request rulesets using WebKit's translator |
| 3 | +# |
| 4 | +# Usage: validate-dnr-rules [--compile] <rules.json> [<rules.json> ...] |
| 5 | +# |
| 6 | +# Runs DNR rule files through WebKit's translation pipeline and reports errors. |
| 7 | +# With --compile, also compiles translated rules to content blocker bytecode. |
| 8 | +# |
| 9 | +# Requires: WebKit built with Tools/Scripts/build-webkit --debug |
| 10 | + |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 14 | +SOURCE_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" |
| 15 | + |
| 16 | +COMPILE=0 |
| 17 | +FILES=() |
| 18 | + |
| 19 | +for arg in "$@"; do |
| 20 | + case "$arg" in |
| 21 | + --compile) COMPILE=1 ;; |
| 22 | + --help|-h) |
| 23 | + echo "Usage: validate-dnr-rules [--compile] <rules.json> [<rules.json> ...]" |
| 24 | + echo "" |
| 25 | + echo "Validates DNR rulesets using WebKit's native translator pipeline." |
| 26 | + echo "Reports translation errors (unsupported regex, invalid rules, etc)." |
| 27 | + echo "" |
| 28 | + echo "Options:" |
| 29 | + echo " --compile Also compile translated rules to content blocker bytecode" |
| 30 | + echo " --help Show this help" |
| 31 | + exit 0 |
| 32 | + ;; |
| 33 | + *) FILES+=("$arg") ;; |
| 34 | + esac |
| 35 | +done |
| 36 | + |
| 37 | +if [ ${#FILES[@]} -eq 0 ]; then |
| 38 | + echo "Error: No input files specified. Use --help for usage." >&2 |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +BUILD_DIR="$("$SCRIPT_DIR/webkit-build-directory" --configuration=Debug --top-level 2>/dev/null || true)" |
| 43 | +if [ -z "$BUILD_DIR" ]; then |
| 44 | + BUILD_DIR="$SOURCE_ROOT/WebKitBuild" |
| 45 | +fi |
| 46 | + |
| 47 | +FRAMEWORK_DIR="$BUILD_DIR/Debug" |
| 48 | + |
| 49 | +if [ ! -d "$FRAMEWORK_DIR/WebKit.framework" ]; then |
| 50 | + echo "Error: WebKit.framework not found at $FRAMEWORK_DIR" >&2 |
| 51 | + echo "Build WebKit first: Tools/Scripts/build-webkit --debug" >&2 |
| 52 | + exit 1 |
| 53 | +fi |
| 54 | + |
| 55 | +TOOL_SRC=$(mktemp /tmp/validate-dnr-XXXXXX.mm) |
| 56 | +TOOL_BIN=$(mktemp /tmp/validate-dnr-XXXXXX) |
| 57 | + |
| 58 | +trap "rm -f '$TOOL_SRC' '$TOOL_BIN'" EXIT |
| 59 | + |
| 60 | +cat > "$TOOL_SRC" << 'OBJC_SOURCE' |
| 61 | +#import <Foundation/Foundation.h> |
| 62 | +#import <WebKit/WebKit.h> |
| 63 | +#import <WebKit/_WKWebExtensionDeclarativeNetRequestTranslator.h> |
| 64 | +#import <WebKit/_WKWebExtensionDeclarativeNetRequestRule.h> |
| 65 | +#import <WebKit/WKContentRuleListPrivate.h> |
| 66 | +
|
| 67 | +int main(int argc, const char *argv[]) { |
| 68 | + @autoreleasepool { |
| 69 | + BOOL doCompile = NO; |
| 70 | + NSMutableArray<NSString *> *files = [NSMutableArray array]; |
| 71 | +
|
| 72 | + for (int i = 1; i < argc; i++) { |
| 73 | + NSString *arg = [NSString stringWithUTF8String:argv[i]]; |
| 74 | + if ([arg isEqualToString:@"--compile"]) |
| 75 | + doCompile = YES; |
| 76 | + else |
| 77 | + [files addObject:arg]; |
| 78 | + } |
| 79 | +
|
| 80 | + int totalErrors = 0; |
| 81 | +
|
| 82 | + for (NSString *filePath in files) { |
| 83 | + NSData *data = [NSData dataWithContentsOfFile:filePath]; |
| 84 | + if (!data) { |
| 85 | + fprintf(stderr, "ERROR: Cannot read file: %s\n", filePath.UTF8String); |
| 86 | + totalErrors++; |
| 87 | + continue; |
| 88 | + } |
| 89 | +
|
| 90 | + NSString *rulesetID = [[filePath lastPathComponent] stringByDeletingPathExtension]; |
| 91 | + NSDictionary<NSString *, NSData *> *jsonDataDict = @{ rulesetID: data }; |
| 92 | +
|
| 93 | + printf("=== %s ===\n", filePath.UTF8String); |
| 94 | + printf("File size: %lu bytes\n", (unsigned long)data.length); |
| 95 | +
|
| 96 | + NSArray<NSString *> *jsonErrors = nil; |
| 97 | + NSDictionary *allJSONObjects = [_WKWebExtensionDeclarativeNetRequestTranslator jsonObjectsFromData:jsonDataDict errorStrings:&jsonErrors]; |
| 98 | +
|
| 99 | + if (jsonErrors.count > 0) { |
| 100 | + printf("JSON deserialization errors: %lu\n", (unsigned long)jsonErrors.count); |
| 101 | + for (NSString *error in jsonErrors) { |
| 102 | + printf(" ERROR: %s\n", error.UTF8String); |
| 103 | + totalErrors++; |
| 104 | + } |
| 105 | + } |
| 106 | +
|
| 107 | + NSUInteger ruleCount = 0; |
| 108 | + for (NSString *key in allJSONObjects) |
| 109 | + ruleCount += [allJSONObjects[key] count]; |
| 110 | + printf("Rules parsed: %lu\n", (unsigned long)ruleCount); |
| 111 | +
|
| 112 | + NSArray<NSString *> *translationErrors = nil; |
| 113 | + NSArray *convertedRules = [_WKWebExtensionDeclarativeNetRequestTranslator translateRules:allJSONObjects errorStrings:&translationErrors]; |
| 114 | +
|
| 115 | + printf("Rules translated: %lu\n", (unsigned long)convertedRules.count); |
| 116 | +
|
| 117 | + if (translationErrors.count > 0) { |
| 118 | + printf("Translation errors: %lu\n", (unsigned long)translationErrors.count); |
| 119 | + for (NSString *error in translationErrors) { |
| 120 | + printf(" ERROR: %s\n", error.UTF8String); |
| 121 | + totalErrors++; |
| 122 | + } |
| 123 | + } |
| 124 | +
|
| 125 | + if (doCompile && convertedRules.count > 0) { |
| 126 | + NSError *jsonSerializationError = nil; |
| 127 | + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:convertedRules options:0 error:&jsonSerializationError]; |
| 128 | + if (jsonSerializationError) { |
| 129 | + printf(" ERROR: JSON serialization failed: %s\n", jsonSerializationError.localizedDescription.UTF8String); |
| 130 | + totalErrors++; |
| 131 | + } else { |
| 132 | + NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; |
| 133 | + printf("Content blocker JSON: %lu bytes\n", (unsigned long)jsonData.length); |
| 134 | + printf("Compiling..."); |
| 135 | + fflush(stdout); |
| 136 | +
|
| 137 | + __block BOOL done = NO; |
| 138 | + __block BOOL success = NO; |
| 139 | + __block NSString *compilationError = nil; |
| 140 | +
|
| 141 | + NSDate *startTime = [NSDate date]; |
| 142 | +
|
| 143 | + [[WKContentRuleListStore defaultStore] compileContentRuleListForIdentifier:rulesetID encodedContentRuleList:jsonString completionHandler:^(WKContentRuleList *ruleList, NSError *error) { |
| 144 | + success = (ruleList != nil); |
| 145 | + if (error) |
| 146 | + compilationError = error.localizedDescription; |
| 147 | + done = YES; |
| 148 | + }]; |
| 149 | +
|
| 150 | + while (!done) |
| 151 | + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; |
| 152 | +
|
| 153 | + double elapsed = -[startTime timeIntervalSinceNow]; |
| 154 | +
|
| 155 | + if (success) |
| 156 | + printf(" OK (%.1f seconds)\n", elapsed); |
| 157 | + else { |
| 158 | + printf(" FAILED (%.1f seconds): %s\n", elapsed, compilationError.UTF8String); |
| 159 | + totalErrors++; |
| 160 | + } |
| 161 | +
|
| 162 | + [[WKContentRuleListStore defaultStore] removeContentRuleListForIdentifier:rulesetID completionHandler:^(NSError *error) {}]; |
| 163 | + } |
| 164 | + } |
| 165 | +
|
| 166 | + printf("\n"); |
| 167 | + } |
| 168 | +
|
| 169 | + if (totalErrors) |
| 170 | + printf("FAILED: %d error(s) found.\n", totalErrors); |
| 171 | + else |
| 172 | + printf("OK: All rules validated successfully.\n"); |
| 173 | +
|
| 174 | + return totalErrors ? 1 : 0; |
| 175 | + } |
| 176 | +} |
| 177 | +OBJC_SOURCE |
| 178 | + |
| 179 | +clang -ObjC++ -std=c++20 -fobjc-arc -w \ |
| 180 | + -F"$FRAMEWORK_DIR" \ |
| 181 | + -framework WebKit -framework Foundation \ |
| 182 | + -lc++ \ |
| 183 | + -Wl,-rpath,"$FRAMEWORK_DIR" \ |
| 184 | + -o "$TOOL_BIN" "$TOOL_SRC" |
| 185 | + |
| 186 | +ARGS=() |
| 187 | +if [ "$COMPILE" -eq 1 ]; then |
| 188 | + ARGS+=(--compile) |
| 189 | +fi |
| 190 | + |
| 191 | +for f in "${FILES[@]}"; do |
| 192 | + ARGS+=("$(cd "$(dirname "$f")" && pwd)/$(basename "$f")") |
| 193 | +done |
| 194 | + |
| 195 | +DYLD_FRAMEWORK_PATH="$FRAMEWORK_DIR" "$TOOL_BIN" "${ARGS[@]}" |
0 commit comments