diff options
| -rw-r--r-- | core/colors.zig | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/core/colors.zig b/core/colors.zig index 7e692fe5..5579ed05 100644 --- a/core/colors.zig +++ b/core/colors.zig @@ -687,6 +687,29 @@ fn revertThemeFile(p: *Pardes) void { p.emit(.{ .theme_file = .{ .generation = generation, .on = loaded.len > 0 } }); } +/// The deepest a theme file's brackets nest: its schema is three levels +/// deep, and std's ZON parser recurses a level a bracket with no bound of +/// its own (a file 5 000 deep overflowed a 16 MiB stack in a Debug build: +/// the session's end, at every start that loaded it). +const max_theme_nesting = 64; + +/// How deep `source`'s brackets nest, as std's tokenizer reads it (strings +/// and comments are tokens of their own). +fn nesting(source: [:0]const u8) usize { + var tokens: std.zig.Tokenizer = .init(source); + var depth: usize = 0; + var most: usize = 0; + while (true) switch (tokens.next().tag) { + .eof => return most, + .l_brace, .l_bracket, .l_paren => { + depth += 1; + most = @max(most, depth); + }, + .r_brace, .r_bracket, .r_paren => depth -|= 1, + else => {}, + }; +} + pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8) bool { // ZON's generated field dispatch grows with the complete theme schema. @setEvalBranchQuota(10000); @@ -700,6 +723,10 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8) bool { return false; }; defer p.gpa.free(source); + if (nesting(source) > max_theme_nesting) { + themeFileFailure(p, request, std.fmt.comptimePrint("nested past {d} levels", .{max_theme_nesting})); + return false; + } // The parse's results live in `arena`; the theme kept owns only its name. var arena: std.heap.ArenaAllocator = .init(p.gpa); defer arena.deinit(); @@ -931,6 +958,31 @@ test "a theme file that cannot be read is named in the failure" { try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "theme file /nowhere/mine.zon: ") != null); } +test "a theme file nested 10, 1 000 and 100 000 deep is refused in words, never a stack overflow" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .config_dir = "/tmp/pardes-config" }); + defer p.deinit(); + for ([_]usize{ 10, 1000, 100_000 }) |depth| { + requestThemeFile(p, 0, "themes/deep.zon"); + var source: std.ArrayList(u8) = .empty; + defer source.deinit(gpa); + try source.appendSlice(gpa, ".{ .name = \"deep\", .bg = "); + for (0..depth) |_| try source.appendSlice(gpa, ".{"); + try source.appendSlice(gpa, "1"); + for (0..depth) |_| try source.append(gpa, '}'); + try source.appendSlice(gpa, " }\n"); + p.panes[0].?.msg_len = 0; + // Up to 2026-11-23 std's parser took it: 5 000 deep overflowed the + // stack (a Debug build's 16 MiB). + try std.testing.expect(!loadThemeFile(p, p.theme_file_generation, source.items)); + const said = p.panes[0].?.msg[0..p.panes[0].?.msg_len]; + std.testing.expect(std.mem.indexOf(u8, said, "theme file /tmp/pardes-config/themes/deep.zon: ") != null) catch |err| { + std.debug.print("{d} deep: said {s}\n", .{ depth, said }); + return err; + }; + } +} + test "ThemeFile parses ZON atomically and Theme returns to the compiled ring" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, |
