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

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

Closed
bradleygore opened this issue Feb 21, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@bradleygore
Copy link

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

@bradleygore bradleygore added the bug Something isn't working label Feb 21, 2023
@terehov
Copy link
Contributor

terehov commented Feb 22, 2023

Thank you, I'll fix it shortly.

@mkcode
Copy link

mkcode commented Feb 22, 2023

Just encountered this myself.

@terehov
Copy link
Contributor

terehov commented Feb 23, 2023

V4.7.5 is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants