Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: check for unwanted extensions as a defense in depth #1437

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/docs_website/.gitattribues

This file was deleted.

36 changes: 36 additions & 0 deletions src/tidy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,42 @@ test "tidy no large blobs" {
if (has_large_blobs) return error.HasLargeBlobs;
}

// Sanity check for "unexpected" files in the repository.
test "tidy extensions" {
const allowed_extensions = std.ComptimeStringMap(void, .{
.{".bat"}, .{".c"}, .{".cs"}, .{".csproj"}, .{".css"}, .{".go"},
.{".h"}, .{".hcl"}, .{".java"}, .{".js"}, .{".json"}, .{".md"},
.{".mod"}, .{".props"}, .{".ps1"}, .{".service"}, .{".sh"}, .{".sln"},
.{".sum"}, .{".ts"}, .{".txt"}, .{".xml"}, .{".yml"}, .{".zig"},
});

const exceptions = std.ComptimeStringMap(void, .{
.{".editorconfig"}, .{".gitattributes"}, .{".gitignore"}, .{".nojekyll"},
.{"CNAME"}, .{"Dockerfile"}, .{"exclude-pmd.properties"}, .{"favicon.ico"},
.{"favicon.png"}, .{"LICENSE"}, .{"logo.svg"}, .{"module-info.test"},
});

const allocator = std.testing.allocator;
const shell = try Shell.create(allocator);
defer shell.destroy();

const files = try shell.exec_stdout("git ls-files", .{});
var lines = std.mem.split(u8, files, "\n");
var bad_extension = false;
while (lines.next()) |path| {
if (path.len == 0) continue;
const extension = std.fs.path.extension(path);
if (!allowed_extensions.has(extension)) {
const basename = std.fs.path.basename(path);
if (!exceptions.has(basename)) {
std.debug.print("bad extension: {s}\n", .{path});
bad_extension = true;
}
}
}
if (bad_extension) return error.BadExtension;
}

fn banned(source: []const u8) ?[]const u8 {
// Note: must avoid banning ourselves!
if (std.mem.indexOf(u8, source, "std." ++ "BoundedArray") != null) {
Expand Down
Loading