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

Add escaping of HTML in logs by default #57

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate-type = ["cdylib", "rlib"]
js-sys = "0.3"
log = "0.4"
fern = "0.6"
html-escape = "0.2"
screeps-game-api = "0.21"
# If you'd like to use a locally-cloned out version of the game API crate
# (for testing PRs, etc), you can use a local path (replacing the above line):
Expand All @@ -30,14 +31,14 @@ lto = true
# Replace the following to enable wasm-opt optimization
# wasm-pack will try to install wasm-opt automatically, but it must be installed by hand on some
# operating systems.
wasm-opt = false
#wasm-opt = false
# See wasm-opt for full available options; handy examples:
# -O4 - optimize aggressively for performance
# -Oz - optimize aggressively for code size
# -g - leave debug info in place, allowing for more descriptive stack traces on panic
# --disable-sign-ext - prevents opcodes that the screeps servers can't load (see
# https://github.com/rustyscreeps/screeps-game-api/issues/391)
#wasm-opt = ["-O4", "--disable-sign-ext"]
wasm-opt = ["-O4", "--signext-lowering"]

[features]
default = []
Expand Down
12 changes: 8 additions & 4 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::panic::PanicInfo;
use std::{fmt::Write, panic};

use html_escape::encode_text;
use js_sys::JsString;
use log::*;
use screeps::game;
Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn setup_logging(verbosity: log::LevelFilter) {
"({}) {}: {}",
record.level(),
record.target(),
message
encode_text(&message.to_string())
))
})
.chain(Box::new(JsLog) as Box<dyn log::Log>)
Expand All @@ -48,7 +48,11 @@ pub fn setup_logging(verbosity: log::LevelFilter) {
.level(log::LevelFilter::Warn)
.format(|out, message, _record| {
let time = game::time();
out.finish(format_args!("[{}] {}", time, message))
out.finish(format_args!(
"[{}] {}",
time,
encode_text(&message.to_string())
))
})
.chain(Box::new(JsNotify) as Box<dyn log::Log>),
)
Expand All @@ -71,7 +75,7 @@ extern "C" {
fn stack_trace_limit(size: f32);
}

fn panic_hook(info: &PanicInfo) {
fn panic_hook(info: &panic::PanicHookInfo) {
// import JS Error API to get backtrace info (backtraces don't work in wasm)
// Node 8 does support this API: https://nodejs.org/docs/latest-v8.x/api/errors.html#errors_error_stack

Expand Down