BUGFIX: JS errors could crash our process
This commit is contained in:
parent
06cdb1f070
commit
1556548ff6
|
@ -125,7 +125,7 @@ module PrettyText
|
||||||
|
|
||||||
baked = nil
|
baked = nil
|
||||||
|
|
||||||
@mutex.synchronize do
|
protect do
|
||||||
context = v8
|
context = v8
|
||||||
# we need to do this to work in a multi site environment, many sites, many settings
|
# we need to do this to work in a multi site environment, many sites, many settings
|
||||||
decorate_context(context)
|
decorate_context(context)
|
||||||
|
@ -152,14 +152,12 @@ module PrettyText
|
||||||
|
|
||||||
# leaving this here, cause it invokes v8, don't want to implement twice
|
# leaving this here, cause it invokes v8, don't want to implement twice
|
||||||
def self.avatar_img(avatar_template, size)
|
def self.avatar_img(avatar_template, size)
|
||||||
r = nil
|
protect do
|
||||||
@mutex.synchronize do
|
|
||||||
v8['avatarTemplate'] = avatar_template
|
v8['avatarTemplate'] = avatar_template
|
||||||
v8['size'] = size
|
v8['size'] = size
|
||||||
decorate_context(v8)
|
decorate_context(v8)
|
||||||
r = v8.eval("Discourse.Utilities.avatarImg({ avatarTemplate: avatarTemplate, size: size });")
|
v8.eval("Discourse.Utilities.avatarImg({ avatarTemplate: avatarTemplate, size: size });")
|
||||||
end
|
end
|
||||||
r
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.cook(text, opts={})
|
def self.cook(text, opts={})
|
||||||
|
@ -253,6 +251,32 @@ module PrettyText
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
class JavaScriptError < StandardError
|
||||||
|
attr_accessor :message, :backtrace
|
||||||
|
|
||||||
|
def initialize(message, backtrace)
|
||||||
|
@message = message
|
||||||
|
@backtrace = backtrace
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.protect
|
||||||
|
rval = nil
|
||||||
|
@mutex.synchronize do
|
||||||
|
begin
|
||||||
|
rval = yield
|
||||||
|
# This may seem a bit odd, but we don't want to leak out
|
||||||
|
# objects that require locks on the v8 vm, to get a backtrace
|
||||||
|
# you need a lock, if this happens in the wrong spot you can
|
||||||
|
# deadlock a process
|
||||||
|
rescue V8::Error => e
|
||||||
|
raise JavaScriptError.new(e.message, e.backtrace)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
rval
|
||||||
|
end
|
||||||
|
|
||||||
def self.ctx_load(ctx, *files)
|
def self.ctx_load(ctx, *files)
|
||||||
files.each do |file|
|
files.each do |file|
|
||||||
ctx.load(app_root + file)
|
ctx.load(app_root + file)
|
||||||
|
|
Loading…
Reference in New Issue