fullstack-build / tslog

📝 tslog - Universal Logger for TypeScript and JavaScript
https://tslog.js.org
MIT License
1.35k stars 63 forks source link

Bug: Browser Polyfill does not validate that `inspect` is actually a function #216

Closed bradleygore closed 1 year ago

bradleygore commented 1 year ago

Describe the bug

util.inspect.polyfill.ts presumes that any function given to it has an inspect method:

https://github.com/fullstack-build/tslog/blob/master/src/runtime/browser/util.inspect.polyfil.ts#L154-L164

To Reproduce

Here's a slightly simplified (but same logic) copy of that area of the function that can be ran in browser console:

function formatValue(ctx, value) {
    if (ctx.customInspect && 
        value != null && 
        typeof value === 'function' && 
        !(value.constructor && value.constructor.prototype === value)
    ) { value.inspect(); }
}
// call the formatter with a function passed in
formatValue({customInspect:true}, () => {}); // throws error b/c value.inspect is not defined

I think it can be easily fixed by just checking that value.inspect is a function:

function formatValue(ctx, value) {
    if (ctx.customInspect && 
        value != null && 
        typeof value === 'function' && 
        typeof value.inspect === 'function' &&
        !(value.constructor && value.constructor.prototype === value)
    ) { value.inspect(); }
}

// call the formatter with a function passed in
formatValue({customInspect:true}, () => {}); // throws no error b/c it did not attempt to call value.inspect

Additional context We ran into this while logging a class that has functions on it using the default pretty logger. We didn't realize it would recurse through all fields and presume that any functions it comes across will have this inspect function.

tslog version 4.7.2

terehov commented 1 year ago

Thank you, I'll fix it shortly.

mkcode commented 1 year ago

Just encountered this myself.

terehov commented 1 year ago

V4.7.5 is out.