Skip to content

Commit

Permalink
Do not use optional chaining on a potentially undeclared root object
Browse files Browse the repository at this point in the history
The global "OC" variable may be undefined depending on the
initialization order, so optional chaining ("OC?.config" and
"OC?.debug") was added to prevent accessing fields on an undefined
variable.

However, "OC" could be not only undefined, but also undeclared. Optional
chaining can not be used on an undeclared root object, so it needs to be
explicitly guarded against that to prevent a ReferenceError to be
thrown.

Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
  • Loading branch information
danxuliu committed Aug 18, 2022
1 parent 1bebce3 commit 04a4931
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/LoggerBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export class LoggerBuilder {
this.context = {}
this.factory = factory
// Up to, including, nextcloud 24 the loglevel was not exposed
this.context.level = OC?.config?.loglevel !== undefined ? OC.config.loglevel : LogLevel.Warn
this.context.level = (window.hasOwnProperty('OC') && OC?.config?.loglevel !== undefined) ? OC.config.loglevel : LogLevel.Warn
// Override loglevel if we are in debug mode
if (OC?.debug) {
if (window.hasOwnProperty('OC') && OC?.debug) {
this.context.level = LogLevel.Debug
}
}
Expand Down

0 comments on commit 04a4931

Please sign in to comment.