|
| 1 | +const std = @import("std"); |
| 2 | +const config = @import("config"); |
| 3 | +const registry = @import("registry"); |
| 4 | + |
| 5 | +pub const reg_value_trusted_modules = "TrustedModules"; |
| 6 | +pub const reg_value_approved_modules = "ApprovedModules"; |
| 7 | +pub const reg_value_approved_global_modules = "ApprovedGlobalModules"; |
| 8 | +pub const reg_value_untrusted_handler = "UntrustedModuleHandlerAction"; |
| 9 | + |
| 10 | +pub const PackageSpec = struct { |
| 11 | + name: []const u8, |
| 12 | + version: []const u8, |
| 13 | + raw: []const u8, |
| 14 | +}; |
| 15 | + |
| 16 | +fn trimAscii(s: []const u8) []const u8 { |
| 17 | + return std.mem.trim(u8, s, " \t\r\n"); |
| 18 | +} |
| 19 | + |
| 20 | +fn eqlIgnoreCase(a: []const u8, b: []const u8) bool { |
| 21 | + return std.ascii.eqlIgnoreCase(a, b); |
| 22 | +} |
| 23 | + |
| 24 | +fn startsWithIgnoreCase(hay: []const u8, needle: []const u8) bool { |
| 25 | + if (hay.len < needle.len) return false; |
| 26 | + return eqlIgnoreCase(hay[0..needle.len], needle); |
| 27 | +} |
| 28 | + |
| 29 | +pub fn splitNameVersion(spec: []const u8) struct { name: []const u8, version: []const u8 } { |
| 30 | + const s = trimAscii(spec); |
| 31 | + if (s.len == 0) return .{ .name = "", .version = "" }; |
| 32 | + if (s[0] == '@') { |
| 33 | + if (std.mem.indexOfScalar(u8, s[1..], '/')) |slash_rel| { |
| 34 | + const slash = slash_rel + 1; |
| 35 | + const after = s[slash + 1 ..]; |
| 36 | + if (std.mem.indexOfScalar(u8, after, '@')) |at| { |
| 37 | + return .{ .name = s[0 .. slash + 1 + at], .version = after[at + 1 ..] }; |
| 38 | + } |
| 39 | + return .{ .name = s, .version = "" }; |
| 40 | + } |
| 41 | + return .{ .name = s, .version = "" }; |
| 42 | + } |
| 43 | + if (std.mem.indexOfScalar(u8, s, '@')) |at| { |
| 44 | + return .{ .name = s[0..at], .version = s[at + 1 ..] }; |
| 45 | + } |
| 46 | + return .{ .name = s, .version = "" }; |
| 47 | +} |
| 48 | + |
| 49 | +fn normalizeRule(raw: []const u8) struct { entry: []const u8, negated: bool } { |
| 50 | + var entry = trimAscii(raw); |
| 51 | + var negated = false; |
| 52 | + if (startsWithIgnoreCase(entry, "not ") or startsWithIgnoreCase(entry, "not\t")) { |
| 53 | + negated = true; |
| 54 | + entry = trimAscii(entry[3..]); |
| 55 | + } else if (entry.len > 0 and entry[0] == '!') { |
| 56 | + negated = true; |
| 57 | + entry = trimAscii(entry[1..]); |
| 58 | + } |
| 59 | + return .{ .entry = entry, .negated = negated }; |
| 60 | +} |
| 61 | + |
| 62 | +fn nameMatches(pattern: []const u8, pkg_name: []const u8) bool { |
| 63 | + if (eqlIgnoreCase(pattern, "all")) return true; |
| 64 | + if (std.mem.endsWith(u8, pattern, "/*")) { |
| 65 | + const org = pattern[0 .. pattern.len - 2]; |
| 66 | + if (pkg_name.len <= org.len) return false; |
| 67 | + if (!eqlIgnoreCase(pkg_name[0..org.len], org)) return false; |
| 68 | + return pkg_name[org.len] == '/'; |
| 69 | + } |
| 70 | + return eqlIgnoreCase(pattern, pkg_name); |
| 71 | +} |
| 72 | + |
| 73 | +fn versionMatches(pattern_ver: []const u8, pkg_ver: []const u8) bool { |
| 74 | + if (pattern_ver.len == 0) return true; |
| 75 | + if (pkg_ver.len == 0) return true; |
| 76 | + if (std.mem.endsWith(u8, pattern_ver, ".*")) { |
| 77 | + const prefix = pattern_ver[0 .. pattern_ver.len - 2]; |
| 78 | + if (eqlIgnoreCase(pkg_ver, prefix)) return true; |
| 79 | + if (pkg_ver.len > prefix.len and eqlIgnoreCase(pkg_ver[0..prefix.len], prefix) and pkg_ver[prefix.len] == '.') return true; |
| 80 | + return false; |
| 81 | + } |
| 82 | + // Exact or prefix equality for MVP ranges like >= are handled loosely: exact match only in Zig; |
| 83 | + // full semver ranges evaluated in Go path / HTTPS. For common 1.0.0 pins: |
| 84 | + return eqlIgnoreCase(pattern_ver, pkg_ver) or startsWithIgnoreCase(pattern_ver, ">=") or startsWithIgnoreCase(pattern_ver, "^") or startsWithIgnoreCase(pattern_ver, "~"); |
| 85 | +} |
| 86 | + |
| 87 | +fn ruleMatches(rule_entry: []const u8, pkg: PackageSpec) bool { |
| 88 | + if (eqlIgnoreCase(rule_entry, "all")) return true; |
| 89 | + const nv = splitNameVersion(rule_entry); |
| 90 | + if (!nameMatches(nv.name, pkg.name)) return false; |
| 91 | + // For range operators in Zig MVP, treat as name match (policy still useful); exact pins enforced. |
| 92 | + if (nv.version.len > 0 and (nv.version[0] == '>' or nv.version[0] == '^' or nv.version[0] == '~' or nv.version[0] == '<')) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + return versionMatches(nv.version, pkg.version); |
| 96 | +} |
| 97 | + |
| 98 | +/// True when policy list contains an https:// URL (remote evaluation mode). |
| 99 | +pub fn listHasHttps(rules: []const []const u8) bool { |
| 100 | + for (rules) |r| { |
| 101 | + const e = trimAscii(r); |
| 102 | + if (startsWithIgnoreCase(e, "https://")) return true; |
| 103 | + } |
| 104 | + return false; |
| 105 | +} |
| 106 | + |
| 107 | +/// Local-list evaluation (VersionAllowList-compatible). HTTPS URL lists return null (caller does remote). |
| 108 | +pub fn isPackageAllowed(pkg: PackageSpec, rules: []const []const u8) ?bool { |
| 109 | + if (listHasHttps(rules)) return null; |
| 110 | + |
| 111 | + var not_all = false; |
| 112 | + var has_exclusive = false; |
| 113 | + var i: usize = 0; |
| 114 | + while (i < rules.len) : (i += 1) { |
| 115 | + const norm = normalizeRule(rules[i]); |
| 116 | + if (norm.entry.len == 0) continue; |
| 117 | + if (norm.negated and eqlIgnoreCase(norm.entry, "all")) not_all = true; |
| 118 | + if (!norm.negated and !eqlIgnoreCase(norm.entry, "all")) has_exclusive = true; |
| 119 | + } |
| 120 | + |
| 121 | + if (not_all) { |
| 122 | + i = 0; |
| 123 | + while (i < rules.len) : (i += 1) { |
| 124 | + const norm = normalizeRule(rules[i]); |
| 125 | + if (norm.negated) continue; |
| 126 | + if (ruleMatches(norm.entry, pkg)) return true; |
| 127 | + } |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + i = 0; |
| 132 | + while (i < rules.len) : (i += 1) { |
| 133 | + const norm = normalizeRule(rules[i]); |
| 134 | + if (!norm.negated) continue; |
| 135 | + if (ruleMatches(norm.entry, pkg)) return false; |
| 136 | + } |
| 137 | + i = 0; |
| 138 | + while (i < rules.len) : (i += 1) { |
| 139 | + const norm = normalizeRule(rules[i]); |
| 140 | + if (norm.negated) continue; |
| 141 | + if (ruleMatches(norm.entry, pkg)) return true; |
| 142 | + } |
| 143 | + if (has_exclusive) return false; |
| 144 | + return true; |
| 145 | +} |
| 146 | + |
| 147 | +pub fn loadMultiSzPolicy(allocator: std.mem.Allocator, value_name: []const u8) ![]const []const u8 { |
| 148 | + const policy_hives = [_]registry.Hive{ .hkey_local_machine, .hkey_current_user }; |
| 149 | + if (registry.queryMultiStringOptionalWithFallback(allocator, &policy_hives, config.policy_registry_root, value_name) catch null) |vals| { |
| 150 | + return vals; |
| 151 | + } |
| 152 | + if (registry.queryMultiStringOptionalWithFallback(allocator, registry.preferenceHives(), config.preference_registry_root, value_name) catch null) |vals| { |
| 153 | + return vals; |
| 154 | + } |
| 155 | + return &[_][]const u8{}; |
| 156 | +} |
| 157 | + |
| 158 | +pub fn freeMultiSz(allocator: std.mem.Allocator, vals: []const []const u8) void { |
| 159 | + if (vals.len == 0) return; |
| 160 | + for (vals) |v| allocator.free(v); |
| 161 | + allocator.free(vals); |
| 162 | +} |
| 163 | + |
| 164 | +test "not all with exception" { |
| 165 | + const rules = [_][]const u8{ "NOT ALL", "porthog" }; |
| 166 | + const ok = isPackageAllowed(.{ .name = "porthog", .version = "", .raw = "porthog" }, &rules); |
| 167 | + try std.testing.expect(ok != null and ok.?); |
| 168 | + const deny = isPackageAllowed(.{ .name = "eslint", .version = "", .raw = "eslint" }, &rules); |
| 169 | + try std.testing.expect(deny != null and !deny.?); |
| 170 | +} |
0 commit comments