Skip to content

Commit

Permalink
Redact secrets in bunfig.toml and npmrc logs (#14919)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-conway authored Nov 1, 2024
1 parent 6933208 commit 62881ee
Show file tree
Hide file tree
Showing 39 changed files with 949 additions and 609 deletions.
5 changes: 3 additions & 2 deletions src/api/schema.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const bun = @import("root").bun;
const js_ast = bun.JSAst;
const OOM = bun.OOM;

pub const Reader = struct {
const Self = @This();
Expand Down Expand Up @@ -2825,11 +2826,11 @@ pub const Api = struct {
}
}

pub fn parseRegistryURLString(this: *Parser, str: *js_ast.E.String) !Api.NpmRegistry {
pub fn parseRegistryURLString(this: *Parser, str: *js_ast.E.String) OOM!Api.NpmRegistry {
return try this.parseRegistryURLStringImpl(str.data);
}

pub fn parseRegistryURLStringImpl(this: *Parser, str: []const u8) !Api.NpmRegistry {
pub fn parseRegistryURLStringImpl(this: *Parser, str: []const u8) OOM!Api.NpmRegistry {
const url = bun.URL.parse(str);
var registry = std.mem.zeroes(Api.NpmRegistry);

Expand Down
8 changes: 4 additions & 4 deletions src/bake/DevServer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -777,14 +777,14 @@ fn bundle(dev: *DevServer, files: []const BakeEntryPoint) BundleError!void {
const bundle_result = bv2.runFromBakeDevServer(files) catch |err| {
bun.handleErrorReturnTrace(err, @errorReturnTrace());

bv2.bundler.log.printForLogLevel(Output.errorWriter()) catch {};
bv2.bundler.log.print(Output.errorWriter()) catch {};

Output.warn("BundleV2.runFromBakeDevServer returned error.{s}", .{@errorName(err)});

return;
};

bv2.bundler.log.printForLogLevel(Output.errorWriter()) catch {};
bv2.bundler.log.print(Output.errorWriter()) catch {};

try dev.finalizeBundle(bv2, bundle_result);

Expand Down Expand Up @@ -1256,7 +1256,7 @@ pub fn handleParseTaskFailure(
bun.path.relative(dev.cwd, abs_path),
});
Output.flush();
log.printForLogLevel(Output.errorWriter()) catch {};
log.print(Output.errorWriter()) catch {};

return switch (graph) {
.server => dev.server_graph.insertFailure(abs_path, log, false),
Expand Down Expand Up @@ -2967,7 +2967,7 @@ pub const SerializedFailure = struct {
inline else => |k| @intFromEnum(@field(ErrorKind, "bundler_log_" ++ @tagName(k))),
});
try writeLogData(msg.data, w);
const notes = msg.notes orelse &.{};
const notes = msg.notes;
try w.writeInt(u32, @intCast(notes.len), .little);
for (notes) |note| {
try writeLogData(note, w);
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/BuildMessage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub const BuildMessage = struct {
}

pub fn getNotes(this: *BuildMessage, globalThis: *JSC.JSGlobalObject) JSC.JSValue {
const notes: []const logger.Data = this.msg.notes orelse &[_]logger.Data{};
const notes = this.msg.notes;
const array = JSC.JSValue.createEmptyArray(globalThis, notes.len);
for (notes, 0..) |note, i| {
const cloned = note.clone(bun.default_allocator) catch {
Expand Down
8 changes: 5 additions & 3 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3909,7 +3909,7 @@ const TOMLObject = struct {
var input_slice = arguments[0].toSlice(globalThis, bun.default_allocator);
defer input_slice.deinit();
var source = logger.Source.initPathString("input.toml", input_slice.slice());
const parse_result = TOMLParser.parse(&source, &log, allocator) catch {
const parse_result = TOMLParser.parse(&source, &log, allocator, false) catch {
globalThis.throwValue(log.toJS(globalThis, default_allocator, "Failed to parse toml"));
return .zero;
};
Expand Down Expand Up @@ -5017,8 +5017,10 @@ const InternalTestingAPIs = struct {
var buffer = MutableString.initEmpty(bun.default_allocator);
defer buffer.deinit();
var writer = buffer.bufferedWriter();
var formatter = bun.fmt.fmtJavaScript(code.slice(), true);
formatter.limited = false;
const formatter = bun.fmt.fmtJavaScript(code.slice(), .{
.enable_colors = true,
.check_for_unhighlighted_write = false,
});
std.fmt.format(writer.writer(), "{}", .{formatter}) catch |err| {
globalThis.throwError(err, "Error formatting code");
return .zero;
Expand Down
5 changes: 5 additions & 0 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2986,6 +2986,11 @@ pub const JSGlobalObject = opaque {
JSGlobalObject__throwOutOfMemoryError(this);
}

pub fn throwOutOfMemoryValue(this: *JSGlobalObject) JSValue {
JSGlobalObject__throwOutOfMemoryError(this);
return .zero;
}

pub fn throwTODO(this: *JSGlobalObject, msg: []const u8) void {
const err = this.createErrorInstance("{s}", .{msg});
err.put(this, ZigString.static("name"), bun.String.static("TODOError").toJS(this));
Expand Down
21 changes: 9 additions & 12 deletions src/bun.js/javascript.zig
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,14 @@ pub const SavedSourceMap = struct {
try fail.toData(path).writeFormat(
Output.errorWriter(),
logger.Kind.warn,
false,
true,
);
} else {
try fail.toData(path).writeFormat(
Output.errorWriter(),
logger.Kind.warn,

false,
false,
);
}
Expand Down Expand Up @@ -1513,11 +1514,7 @@ pub const VirtualMachine = struct {
this.global.handleRejectedPromises();

if (this.log.msgs.items.len > 0) {
if (Output.enable_ansi_colors) {
this.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
} else {
this.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
}
this.log.print(Output.errorWriter()) catch {};
Output.prettyErrorln("\n", .{});
Output.flush();
}
Expand Down Expand Up @@ -3538,15 +3535,15 @@ pub const VirtualMachine = struct {
"<r><b>{d} |<r> {}" ++ fmt,
allow_ansi_color,
),
.{ display_line, bun.fmt.fmtJavaScript(clamped, allow_ansi_color) },
.{ display_line, bun.fmt.fmtJavaScript(clamped, .{ .enable_colors = allow_ansi_color }) },
);
} else {
try writer.print(
comptime Output.prettyFmt(
"<r><b>{d} |<r> {}\n",
allow_ansi_color,
),
.{ display_line, bun.fmt.fmtJavaScript(clamped, allow_ansi_color) },
.{ display_line, bun.fmt.fmtJavaScript(clamped, .{ .enable_colors = allow_ansi_color }) },
);
}
}
Expand Down Expand Up @@ -3583,15 +3580,15 @@ pub const VirtualMachine = struct {
"<r><b>- |<r> {}" ++ fmt,
allow_ansi_color,
),
.{bun.fmt.fmtJavaScript(text, allow_ansi_color)},
.{bun.fmt.fmtJavaScript(text, .{ .enable_colors = allow_ansi_color })},
);
} else {
try writer.print(
comptime Output.prettyFmt(
"<r><d>- |<r> {}\n",
allow_ansi_color,
),
.{bun.fmt.fmtJavaScript(text, allow_ansi_color)},
.{bun.fmt.fmtJavaScript(text, .{ .enable_colors = allow_ansi_color })},
);
}

Expand All @@ -3616,15 +3613,15 @@ pub const VirtualMachine = struct {
"<r><b>{d} |<r> {}" ++ fmt,
allow_ansi_color,
),
.{ display_line, bun.fmt.fmtJavaScript(clamped, allow_ansi_color) },
.{ display_line, bun.fmt.fmtJavaScript(clamped, .{ .enable_colors = allow_ansi_color }) },
);
} else {
try writer.print(
comptime Output.prettyFmt(
"<r><b>{d} |<r> {}\n",
allow_ansi_color,
),
.{ display_line, bun.fmt.fmtJavaScript(clamped, allow_ansi_color) },
.{ display_line, bun.fmt.fmtJavaScript(clamped, .{ .enable_colors = allow_ansi_color }) },
);

if (clamped.len < max_line_length_with_divot or top.position.column.zeroBased() > max_line_length_with_divot) {
Expand Down
2 changes: 1 addition & 1 deletion src/bun.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2357,7 +2357,7 @@ pub const win32 = struct {
return original_mode;
}

const watcherChildEnv: [:0]const u16 = strings.toUTF16LiteralZ("_BUN_WATCHER_CHILD");
const watcherChildEnv: [:0]const u16 = strings.toUTF16Literal("_BUN_WATCHER_CHILD");
// magic exit code to indicate to the watcher manager that the child process should be re-spawned
// this was randomly generated - we need to avoid using a common exit code that might be used by the script itself
const watcher_reload_exit: w.DWORD = 3224497970;
Expand Down
4 changes: 1 addition & 3 deletions src/bun_js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,7 @@ noinline fn dumpBuildError(vm: *JSC.VirtualMachine) void {

const writer = buffered_writer.writer();

switch (Output.enable_ansi_colors_stderr) {
inline else => |enable_colors| vm.log.printForLogLevelWithEnableAnsiColors(writer, enable_colors) catch {},
}
vm.log.print(writer) catch {};
}

pub noinline fn failWithBuildError(vm: *JSC.VirtualMachine) noreturn {
Expand Down
8 changes: 4 additions & 4 deletions src/bundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const lex = bun.js_lexer;
const logger = bun.logger;
const options = @import("options.zig");
const js_parser = bun.js_parser;
const json_parser = bun.JSON;
const JSON = bun.JSON;
const js_printer = bun.js_printer;
const js_ast = bun.JSAst;
const linker = @import("linker.zig");
Expand Down Expand Up @@ -1450,11 +1450,11 @@ pub const Bundler = struct {
// We allow importing tsconfig.*.json or jsconfig.*.json with comments
// These files implicitly become JSONC files, which aligns with the behavior of text editors.
if (source.path.isJSONCFile())
json_parser.parseTSConfig(&source, bundler.log, allocator, false) catch return null
JSON.parseTSConfig(&source, bundler.log, allocator, false) catch return null
else
json_parser.parse(&source, bundler.log, allocator, false) catch return null
JSON.parse(&source, bundler.log, allocator, false) catch return null
else if (kind == .toml)
TOML.parse(&source, bundler.log, allocator) catch return null
TOML.parse(&source, bundler.log, allocator, false) catch return null
else
@compileError("unreachable");

Expand Down
10 changes: 5 additions & 5 deletions src/bundler/bundle_v2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1810,8 +1810,8 @@ pub const BundleV2 = struct {
},
.err => |err| {
log.msgs.append(err) catch unreachable;
log.errors += @as(usize, @intFromBool(err.kind == .err));
log.warnings += @as(usize, @intFromBool(err.kind == .warn));
log.errors += @as(u32, @intFromBool(err.kind == .err));
log.warnings += @as(u32, @intFromBool(err.kind == .warn));

// An error occurred, prevent spinning the event loop forever
_ = @atomicRmw(usize, &this.graph.parse_pending, .Sub, 1, .monotonic);
Expand Down Expand Up @@ -1965,8 +1965,8 @@ pub const BundleV2 = struct {
},
.err => |err| {
log.msgs.append(err) catch unreachable;
log.errors += @as(usize, @intFromBool(err.kind == .err));
log.warnings += @as(usize, @intFromBool(err.kind == .warn));
log.errors += @as(u32, @intFromBool(err.kind == .err));
log.warnings += @as(u32, @intFromBool(err.kind == .warn));
},
.pending, .consumed => unreachable,
}
Expand Down Expand Up @@ -3439,7 +3439,7 @@ pub const ParseTask = struct {
.toml => {
const trace = tracer(@src(), "ParseTOML");
defer trace.end();
const root = try TOML.parse(&source, log, allocator);
const root = try TOML.parse(&source, log, allocator, false);
return JSAst.init((try js_parser.newLazyExportAST(allocator, bundler.options.define, opts, log, root, &source, "")).?);
},
.text => {
Expand Down
56 changes: 44 additions & 12 deletions src/bunfig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,20 @@ pub const Bunfig = struct {
ctx: Command.Context,

fn addError(this: *Parser, loc: logger.Loc, comptime text: string) !void {
this.log.addError(this.source, loc, text) catch unreachable;
this.log.addErrorOpts(text, .{
.source = this.source,
.loc = loc,
.redact_sensitive_information = true,
}) catch unreachable;
return error.@"Invalid Bunfig";
}

fn addErrorFormat(this: *Parser, loc: logger.Loc, allocator: std.mem.Allocator, comptime text: string, args: anytype) !void {
this.log.addErrorFmt(this.source, loc, allocator, text, args) catch unreachable;
this.log.addErrorFmtOpts(allocator, text, args, .{
.source = this.source,
.loc = loc,
.redact_sensitive_information = true,
}) catch unreachable;
return error.@"Invalid Bunfig";
}

Expand Down Expand Up @@ -791,20 +799,38 @@ pub const Bunfig = struct {
switch (expr.data) {
.e_string, .e_utf8_string => {},
else => {
this.log.addErrorFmt(this.source, expr.loc, this.allocator, "expected string but received {}", .{
@as(js_ast.Expr.Tag, expr.data),
}) catch unreachable;
this.log.addErrorFmtOpts(
this.allocator,
"expected string but received {}",
.{
@as(js_ast.Expr.Tag, expr.data),
},
.{
.source = this.source,
.loc = expr.loc,
.redact_sensitive_information = true,
},
) catch unreachable;
return error.@"Invalid Bunfig";
},
}
}

pub fn expect(this: *Parser, expr: js_ast.Expr, token: js_ast.Expr.Tag) !void {
if (@as(js_ast.Expr.Tag, expr.data) != token) {
this.log.addErrorFmt(this.source, expr.loc, this.allocator, "expected {} but received {}", .{
token,
@as(js_ast.Expr.Tag, expr.data),
}) catch unreachable;
this.log.addErrorFmtOpts(
this.allocator,
"expected {} but received {}",
.{
token,
@as(js_ast.Expr.Tag, expr.data),
},
.{
.source = this.source,
.loc = expr.loc,
.redact_sensitive_information = true,
},
) catch unreachable;
return error.@"Invalid Bunfig";
}
}
Expand All @@ -813,14 +839,20 @@ pub const Bunfig = struct {
pub fn parse(allocator: std.mem.Allocator, source: logger.Source, ctx: Command.Context, comptime cmd: Command.Tag) !void {
const log_count = ctx.log.errors + ctx.log.warnings;

const expr = if (strings.eqlComptime(source.path.name.ext[1..], "toml")) TOML.parse(&source, ctx.log, allocator) catch |err| {
const expr = if (strings.eqlComptime(source.path.name.ext[1..], "toml")) TOML.parse(&source, ctx.log, allocator, true) catch |err| {
if (ctx.log.errors + ctx.log.warnings == log_count) {
ctx.log.addErrorFmt(&source, logger.Loc.Empty, allocator, "Failed to parse", .{}) catch unreachable;
try ctx.log.addErrorOpts("Failed to parse", .{
.source = &source,
.redact_sensitive_information = true,
});
}
return err;
} else JSONParser.parseTSConfig(&source, ctx.log, allocator, true) catch |err| {
if (ctx.log.errors + ctx.log.warnings == log_count) {
ctx.log.addErrorFmt(&source, logger.Loc.Empty, allocator, "Failed to parse", .{}) catch unreachable;
try ctx.log.addErrorOpts("Failed to parse", .{
.source = &source,
.redact_sensitive_information = true,
});
}
return err;
};
Expand Down
20 changes: 4 additions & 16 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub const Cli = struct {
// var panicker = MainPanicHandler.init(log);
// MainPanicHandler.Singleton = &panicker;
Command.start(allocator, log) catch |err| {
log.printForLogLevel(Output.errorWriter()) catch {};
log.print(Output.errorWriter()) catch {};

bun.crash_handler.handleRootError(err, @errorReturnTrace());
};
Expand Down Expand Up @@ -362,11 +362,7 @@ pub const Arguments = struct {
if (getHomeConfigPath(&config_buf)) |path| {
loadConfigPath(allocator, true, path, ctx, comptime cmd) catch |err| {
if (ctx.log.hasAny()) {
switch (Output.enable_ansi_colors) {
inline else => |enable_ansi_colors| {
ctx.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), enable_ansi_colors) catch {};
},
}
ctx.log.print(Output.errorWriter()) catch {};
}
if (ctx.log.hasAny()) Output.printError("\n", .{});
Output.err(err, "failed to load bunfig", .{});
Expand Down Expand Up @@ -421,11 +417,7 @@ pub const Arguments = struct {

loadConfigPath(allocator, auto_loaded, config_path, ctx, comptime cmd) catch |err| {
if (ctx.log.hasAny()) {
switch (Output.enable_ansi_colors) {
inline else => |enable_ansi_colors| {
ctx.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), enable_ansi_colors) catch {};
},
}
ctx.log.print(Output.errorWriter()) catch {};
}
if (ctx.log.hasAny()) Output.printError("\n", .{});
Output.err(err, "failed to load bunfig", .{});
Expand Down Expand Up @@ -2277,11 +2269,7 @@ pub const Command = struct {
) catch |err| {
bun.handleErrorReturnTrace(err, @errorReturnTrace());

if (Output.enable_ansi_colors) {
ctx.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
} else {
ctx.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), false) catch {};
}
ctx.log.print(Output.errorWriter()) catch {};

Output.prettyErrorln("<r><red>error<r>: Failed to run <b>{s}<r> due to error <b>{s}<r>", .{
std.fs.path.basename(file_path),
Expand Down
Loading

0 comments on commit 62881ee

Please sign in to comment.