srctree

Gregory Mullen parent 727b74f4 844b3f70
fix bleach api to be more specific

src/bleach.zig added: 64, removed: 56, total 8
@@ -26,7 +26,7 @@ pub const Rules = enum {
pub const RuleFn = *const fn (u8, ?[]u8) Error!usize;
 
pub const Options = struct {
rules: Rules = .html,
rules: Rules,
skip_chars: ?[]const u8 = null,
// when true, sanitizer functions will return an error instead of replacing the char.
error_on_replace: bool = false,
@@ -35,6 +35,10 @@ pub const Options = struct {
pub const Html = struct {
text: []const u8,
 
pub fn sanitizeAlloc(a: std.mem.Allocator, in: []const u8) Error![]u8 {
return try Bleach.sanitizeAlloc(a, in, .{ .rules = .html });
}
 
pub fn format(self: Html, comptime _: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
var buf: [6]u8 = undefined;
for (self.text) |c| {
@@ -47,6 +51,10 @@ pub const Filename = struct {
text: []const u8,
permit_directories: bool = false,
 
pub fn sanitizeAlloc(a: std.mem.Allocator, in: []const u8) Error![]u8 {
return try Bleach.sanitizeAlloc(a, in, .{ .rules = .filename });
}
 
pub fn format(self: Filename, comptime _: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
const bleach_fn = if (self.permit_directories) bleachPath else bleachFilename;
var buf: [2]u8 = undefined;
 
src/endpoints/commit-flex.zig added: 64, removed: 56, total 8
@@ -148,8 +148,8 @@ fn buildJournal(
if (commit_time < until) break;
if (std.mem.eql(u8, email.?, commit.author.email)) {
try list.append(.{
.name = try Bleach.sanitizeAlloc(a, commit.author.name, .{}),
.title = try Bleach.sanitizeAlloc(a, commit.title, .{}),
.name = try Bleach.Html.sanitizeAlloc(a, commit.author.name),
.title = try Bleach.Html.sanitizeAlloc(a, commit.title),
.date = DateTime.fromEpoch(commit_time),
.sha = commit.sha,
.repo = try a.dupe(u8, gitdir[8..]),
 
src/endpoints/gist.zig added: 64, removed: 56, total 8
@@ -123,8 +123,8 @@ fn toTemplate(a: Allocator, files: []const Gist.File) ![]Template.Structs.GistFi
const out = try a.alloc(Template.Structs.GistFiles, files.len);
for (files, out) |file, *o| {
o.* = .{
.file_name = try Bleach.sanitizeAlloc(a, file.name, .{}),
.file_blob = try Bleach.sanitizeAlloc(a, file.blob, .{}),
.file_name = try Bleach.Html.sanitizeAlloc(a, file.name),
.file_blob = try Bleach.Html.sanitizeAlloc(a, file.blob),
};
}
return out;
 
src/endpoints/repos.zig added: 64, removed: 56, total 8
@@ -499,7 +499,7 @@ fn blame(ctx: *Context) Error!void {
const formatted = if (Highlighting.Language.guessFromFilename(blame_file)) |lang| fmt: {
var pre = try Highlighting.highlight(ctx.alloc, lang, source_lines.items);
break :fmt pre[28..][0 .. pre.len - 38];
} else Bleach.sanitizeAlloc(ctx.alloc, source_lines.items, .{}) catch return error.Unknown;
} else Bleach.Html.sanitizeAlloc(ctx.alloc, source_lines.items) catch return error.Unknown;
 
var litr = std.mem.splitScalar(u8, formatted, '\n');
for (parsed.lines) |*line| {
@@ -516,7 +516,7 @@ fn blame(ctx: *Context) Error!void {
var page = BlamePage.init(.{
.meta_head = .{ .open_graph = .{} },
.body_header = .{ .nav = .{ .nav_auth = undefined, .nav_buttons = &btns } },
.filename = Bleach.sanitizeAlloc(ctx.alloc, blame_file, .{}) catch unreachable,
.filename = Bleach.Html.sanitizeAlloc(ctx.alloc, blame_file) catch unreachable,
.blame_lines = wrapped_blames,
});
 
@@ -537,8 +537,8 @@ fn wrapLineNumbersBlame(
.repo_name = repo_name,
.sha = bcommit.sha[0..8],
.author_email = .{
.author = Bleach.sanitizeAlloc(a, bcommit.author.name, .{}) catch unreachable,
.email = Bleach.sanitizeAlloc(a, bcommit.author.email, .{}) catch unreachable,
.author = Bleach.Html.sanitizeAlloc(a, bcommit.author.name) catch unreachable,
.email = Bleach.Html.sanitizeAlloc(a, bcommit.author.email) catch unreachable,
},
.time = try Humanize.unix(bcommit.author.timestamp).printAlloc(a),
.num = i + 1,
@@ -608,7 +608,7 @@ fn blob(ctx: *Context, repo: *Git.Repo, pfiles: Git.Tree) Error!void {
} else if (excludedExt(blb.name)) {
formatted = "This file type is currently unsupported";
} else {
formatted = Bleach.sanitizeAlloc(ctx.alloc, resolve.data.?, .{}) catch return error.Unknown;
formatted = Bleach.Html.sanitizeAlloc(ctx.alloc, resolve.data.?) catch return error.Unknown;
}
 
const wrapped = try wrapLineNumbers(ctx.alloc, formatted);
@@ -617,7 +617,7 @@ fn blob(ctx: *Context, repo: *Git.Repo, pfiles: Git.Tree) Error!void {
_ = ctx.uri.next();
const uri_repo = ctx.uri.next() orelse return error.Unrouteable;
_ = ctx.uri.next();
const uri_filename = Bleach.sanitizeAlloc(ctx.alloc, ctx.uri.rest(), .{}) catch return error.Unknown;
const uri_filename = Bleach.Html.sanitizeAlloc(ctx.alloc, ctx.uri.rest()) catch return error.Unknown;
 
ctx.response.status = .ok;
 
@@ -655,7 +655,7 @@ fn htmlReadme(a: Allocator, readme: []const u8) ![]HTML.E {
dom = dom.open(HTML.element("code", null, null));
var litr = std.mem.splitScalar(u8, readme, '\n');
while (litr.next()) |dirty| {
const clean = Bleach.sanitizeAlloc(a, dirty, .{}) catch return error.Unknown;
const clean = Bleach.Html.sanitizeAlloc(a, dirty) catch return error.Unknown;
dom.push(HTML.element("ln", clean, null));
}
dom = dom.close();
 
src/endpoints/repos/commits.zig added: 64, removed: 56, total 8
@@ -111,7 +111,7 @@ fn commitHtml(ctx: *Context, sha: []const u8, repo_name: []const u8, repo: Git.R
 
const diffstat = patch.patchStat();
const og_title = try allocPrint(ctx.alloc, "Commit by {s}: {} file{s} changed +{} -{}", .{
Bleach.sanitizeAlloc(ctx.alloc, current.author.name, .{}) catch unreachable,
Bleach.Html.sanitizeAlloc(ctx.alloc, current.author.name) catch unreachable,
diffstat.files,
if (diffstat.files > 1) "s" else "",
diffstat.additions,
@@ -120,7 +120,7 @@ fn commitHtml(ctx: *Context, sha: []const u8, repo_name: []const u8, repo: Git.R
const meta_head = S.MetaHeadHtml{
.open_graph = .{
.title = og_title,
.desc = Bleach.sanitizeAlloc(ctx.alloc, current.message, .{}) catch unreachable,
.desc = Bleach.Html.sanitizeAlloc(ctx.alloc, current.message) catch unreachable,
},
};
 
@@ -138,9 +138,9 @@ fn commitHtml(ctx: *Context, sha: []const u8, repo_name: []const u8, repo: Git.R
switch (msg.kind) {
.comment => |cmt| {
pg_comment.* = .{
.author = try Bleach.sanitizeAlloc(ctx.alloc, cmt.author, .{}),
.author = try Bleach.Html.sanitizeAlloc(ctx.alloc, cmt.author),
.date = try allocPrint(ctx.alloc, "{}", .{Humanize.unix(msg.updated)}),
.message = try Bleach.sanitizeAlloc(ctx.alloc, cmt.message, .{}),
.message = try Bleach.Html.sanitizeAlloc(ctx.alloc, cmt.message),
.direct_reply = null,
.sub_thread = null,
};
@@ -242,13 +242,13 @@ pub fn commitCtxParents(a: Allocator, c: Git.Commit, repo: []const u8) ![]Templa
 
pub fn commitCtx(a: Allocator, c: Git.Commit, repo: []const u8) !Template.Structs.Commit {
return .{
.author = Bleach.sanitizeAlloc(a, c.author.name, .{}) catch unreachable,
.author = Bleach.Html.sanitizeAlloc(a, c.author.name) catch unreachable,
.parents = try commitCtxParents(a, c, repo),
.sha_uri = try allocPrint(a, "/repo/{s}/commit/{s}", .{ repo, c.sha.hex[0..8] }),
.sha_short = try a.dupe(u8, c.sha.hex[0..8]),
//.sha = try a.dupe(u8, c.sha),
.title = Bleach.sanitizeAlloc(a, c.title, .{}) catch unreachable,
.body = Bleach.sanitizeAlloc(a, c.body, .{}) catch unreachable,
.title = Bleach.Html.sanitizeAlloc(a, c.title) catch unreachable,
.body = Bleach.Html.sanitizeAlloc(a, c.body) catch unreachable,
};
}
 
@@ -264,10 +264,10 @@ pub fn htmlCommit(a: Allocator, c: Git.Commit, repo: []const u8, comptime top: b
try std.fmt.allocPrint(a, "/repo/{s}/commit/{s}", .{ repo, c.sha[0..8] }),
));
cd_dom.push(HTML.br());
cd_dom.push(HTML.text(Bleach.sanitizeAlloc(a, c.title, .{}) catch unreachable));
cd_dom.push(HTML.text(Bleach.Html.sanitizeAlloc(a, c.title) catch unreachable));
if (c.body.len > 0) {
cd_dom.push(HTML.br());
cd_dom.push(HTML.text(Bleach.sanitizeAlloc(a, c.body, .{}) catch unreachable));
cd_dom.push(HTML.text(Bleach.Html.sanitizeAlloc(a, c.body) catch unreachable));
}
cd_dom = cd_dom.close();
const cdata = cd_dom.done();
@@ -317,8 +317,8 @@ fn commitContext(a: Allocator, c: Git.Commit, repo_name: []const u8) !S.Commits
return .{
.sha = try allocPrint(a, "{s}", .{c.sha.hex[0..8]}),
.uri = try allocPrint(a, "/repo/{s}/commit/{s}", .{ repo_name, c.sha.hex[0..8] }),
.msg_title = try Bleach.sanitizeAlloc(a, c.title, .{}),
.msg = try Bleach.sanitizeAlloc(a, c.body, .{}),
.msg_title = try Bleach.Html.sanitizeAlloc(a, c.title),
.msg = try Bleach.Html.sanitizeAlloc(a, c.body),
.author = c.author.name,
.commit_parent = parents,
};
 
src/endpoints/repos/diffs.zig added: 64, removed: 56, total 8
@@ -456,12 +456,12 @@ fn resolveLineRefRepo(
else if (Highlighting.Language.guessFromFilename(filename)) |lang| fmt: {
var pre = try Highlighting.highlight(a, lang, found_line[1..]);
break :fmt pre[28..][0 .. pre.len - 41];
} else try Bleach.sanitizeAlloc(a, found_line[1..], .{});
} else try Bleach.Html.sanitizeAlloc(a, found_line[1..]);
 
const wrapped_line = try allocPrint(
a,
"<div title=\"{s}\" class=\"coderef\">{s}</div>",
.{ try Bleach.sanitizeAlloc(a, line, .{}), formatted },
.{ try Bleach.Html.sanitizeAlloc(a, line), formatted },
);
try found_lines.append(wrapped_line);
return try found_lines.toOwnedSlice();
@@ -505,12 +505,12 @@ fn resolveLineRefDiff(
else if (Highlighting.Language.guessFromFilename(filename)) |lang| fmt: {
var pre = try Highlighting.highlight(a, lang, found_line[1..]);
break :fmt pre[28..][0 .. pre.len - 41];
} else try Bleach.sanitizeAlloc(a, found_line[1..], .{});
} else try Bleach.Html.sanitizeAlloc(a, found_line[1..]);
 
const wrapped_line = try allocPrint(
a,
"<div title=\"{s}\" class=\"coderef {s}\">{s}</div>",
.{ try Bleach.sanitizeAlloc(a, line, .{}), color, formatted },
.{ try Bleach.Html.sanitizeAlloc(a, line), color, formatted },
);
try found_lines.append(wrapped_line);
}
@@ -595,7 +595,7 @@ fn translateComment(a: Allocator, comment: []const u8, patch: Patch, repo: *cons
end += 1;
}
if (end < line.len) try message_lines.append(
try Bleach.sanitizeAlloc(a, line[end..], .{}),
try Bleach.Html.sanitizeAlloc(a, line[end..]),
);
} else if (resolveLineRefRepo(
a,
@@ -614,14 +614,14 @@ fn translateComment(a: Allocator, comment: []const u8, patch: Patch, repo: *cons
try message_lines.append(try allocPrint(
a,
"<span title=\"line not found in this diff\">{s}</span>",
.{try Bleach.sanitizeAlloc(a, line, .{})},
.{try Bleach.Html.sanitizeAlloc(a, line)},
));
}
}
break;
}
} else {
try message_lines.append(try Bleach.sanitizeAlloc(a, line, .{}));
try message_lines.append(try Bleach.Html.sanitizeAlloc(a, line));
}
}
 
@@ -651,8 +651,8 @@ fn view(ctx: *Context) Error!void {
} orelse return error.Unrouteable;
 
const patch_header = S.Header{
.title = Bleach.sanitizeAlloc(ctx.alloc, delta.title, .{}) catch unreachable,
.message = Bleach.sanitizeAlloc(ctx.alloc, delta.message, .{}) catch unreachable,
.title = Bleach.Html.sanitizeAlloc(ctx.alloc, delta.title) catch unreachable,
.message = Bleach.Html.sanitizeAlloc(ctx.alloc, delta.message) catch unreachable,
};
 
// meme saved to protect history
@@ -702,12 +702,12 @@ fn view(ctx: *Context) Error!void {
switch (msg.kind) {
.comment => |comment| {
c_ctx.* = .{
.author = try Bleach.sanitizeAlloc(ctx.alloc, comment.author, .{}),
.author = try Bleach.Html.sanitizeAlloc(ctx.alloc, comment.author),
.date = try allocPrint(ctx.alloc, "{}", .{Humanize.unix(msg.updated)}),
.message = if (patch) |pt|
translateComment(ctx.alloc, comment.message, pt, &repo) catch unreachable
else
try Bleach.sanitizeAlloc(ctx.alloc, comment.message, .{}),
try Bleach.Html.sanitizeAlloc(ctx.alloc, comment.message),
.direct_reply = .{ .uri = try allocPrint(ctx.alloc, "{}/direct_reply/{x}", .{
index,
fmtSliceHexLower(msg.hash[0..]),
@@ -781,13 +781,13 @@ fn list(ctx: *Context) Error!void {
"/repo/{s}/{s}/{x}",
.{ d.repo, if (d.attach == .issue) "issues" else "diffs", d.index },
),
.title = try Bleach.sanitizeAlloc(ctx.alloc, d.title, .{}),
.title = try Bleach.Html.sanitizeAlloc(ctx.alloc, d.title),
.comments_icon = try allocPrint(
ctx.alloc,
"<span><span class=\"icon{s}\">\xee\xa0\x9c</span> {}</span>",
.{ if (cmtsmeta.new) " new" else "", cmtsmeta.count },
),
.desc = try Bleach.sanitizeAlloc(ctx.alloc, d.message, .{}),
.desc = try Bleach.Html.sanitizeAlloc(ctx.alloc, d.message),
});
}
var default_search_buf: [0xFF]u8 = undefined;
 
src/endpoints/repos/issues.zig added: 64, removed: 56, total 8
@@ -139,9 +139,9 @@ fn view(ctx: *Context) Error!void {
switch (msg.kind) {
.comment => |comment| {
c_ctx.* = .{
.author = try Bleach.sanitizeAlloc(ctx.alloc, comment.author, .{}),
.author = try Bleach.Html.sanitizeAlloc(ctx.alloc, comment.author),
.date = try allocPrint(ctx.alloc, "{}", .{Humanize.unix(msg.updated)}),
.message = try Bleach.sanitizeAlloc(ctx.alloc, comment.message, .{}),
.message = try Bleach.Html.sanitizeAlloc(ctx.alloc, comment.message),
.direct_reply = .{ .uri = try allocPrint(ctx.alloc, "{}/direct_reply/{x}", .{
index,
fmtSliceHexLower(msg.hash[0..]),
@@ -166,8 +166,8 @@ fn view(ctx: *Context) Error!void {
.nav_buttons = &try Repos.navButtons(ctx),
.nav_auth = undefined,
} },
.title = Bleach.sanitizeAlloc(ctx.alloc, delta.title, .{}) catch unreachable,
.desc = Bleach.sanitizeAlloc(ctx.alloc, delta.message, .{}) catch unreachable,
.title = Bleach.Html.sanitizeAlloc(ctx.alloc, delta.title) catch unreachable,
.desc = Bleach.Html.sanitizeAlloc(ctx.alloc, delta.message) catch unreachable,
.delta_id = delta_id,
.comments = .{
.thread = root_thread,
@@ -203,13 +203,13 @@ fn list(ctx: *Context) Error!void {
"/repo/{s}/{s}/{x}",
.{ d.repo, if (d.attach == .issue) "issues" else "diffs", d.index },
),
.title = try Bleach.sanitizeAlloc(ctx.alloc, d.title, .{}),
.title = try Bleach.Html.sanitizeAlloc(ctx.alloc, d.title),
.comments_icon = try allocPrint(
ctx.alloc,
"<span><span class=\"icon{s}\">\xee\xa0\x9c</span> {}</span>",
.{ if (cmtsmeta.new) " new" else "", cmtsmeta.count },
),
.desc = try Bleach.sanitizeAlloc(ctx.alloc, d.message, .{}),
.desc = try Bleach.Html.sanitizeAlloc(ctx.alloc, d.message),
});
}
 
 
src/endpoints/search.zig added: 64, removed: 56, total 8
@@ -89,13 +89,13 @@ fn custom(ctx: *Context, search_str: []const u8) Error!void {
"/repo/{s}/{s}/{x}",
.{ d.repo, if (d.attach == .issue) "issues" else "diffs", d.index },
),
.title = try Bleach.sanitizeAlloc(ctx.alloc, d.title, .{}),
.title = try Bleach.Html.sanitizeAlloc(ctx.alloc, d.title),
.comments_icon = try allocPrint(
ctx.alloc,
"<span><span class=\"icon{s}\">\xee\xa0\x9c</span> {}</span>",
.{ if (cmtsmeta.new) " new" else "", cmtsmeta.count },
),
.desc = try Bleach.sanitizeAlloc(ctx.alloc, d.message, .{}),
.desc = try Bleach.Html.sanitizeAlloc(ctx.alloc, d.message),
});
}
 
@@ -115,7 +115,7 @@ fn custom(ctx: *Context, search_str: []const u8) Error!void {
.nav_auth = undefined,
} },
.delta_list = try d_list.toOwnedSlice(),
.search = Bleach.sanitizeAlloc(ctx.alloc, search_str, .{}) catch unreachable,
.search = Bleach.Html.sanitizeAlloc(ctx.alloc, search_str) catch unreachable,
});
 
try ctx.sendPage(&page);
 
src/patch.zig added: 64, removed: 56, total 8
@@ -399,7 +399,7 @@ pub fn diffLineHtmlSplit(a: Allocator, diff: []const u8) ![]HTML.Element {
const a_del = &HTML.Attr.class("del");
const a_block = &HTML.Attr.class("block");
 
const clean = Bleach.sanitizeAlloc(a, diff, .{}) catch unreachable;
const clean = Bleach.Html.sanitizeAlloc(a, diff) catch unreachable;
const line_count = std.mem.count(u8, clean, "\n");
var litr = std.mem.splitScalar(u8, clean, '\n');
const nbsp = "&nbsp;";
@@ -473,7 +473,7 @@ pub fn diffLineHtmlUnified(a: Allocator, diff: []const u8) []HTML.Element {
var dom = DOM.new(a);
dom = dom.open(HTML.span(null, null));
 
const clean = Bleach.sanitizeAlloc(a, diff, .{}) catch unreachable;
const clean = Bleach.Html.sanitizeAlloc(a, diff) catch unreachable;
const line_count = std.mem.count(u8, clean, "\n");
var litr = splitScalar(u8, clean, '\n');
for (0..line_count + 1) |_| {