From 812e4a570e6bfc7ceadb7934bc9c5ef118e227c1 Mon Sep 17 00:00:00 2001 From: luozhiya Date: Tue, 21 May 2024 15:54:36 +0800 Subject: [PATCH] Encode JSON safely --- lua/fittencode/key_storage.lua | 42 +++++++++-------------- lua/fittencode/rest/backend/curl/init.lua | 7 +++- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/lua/fittencode/key_storage.lua b/lua/fittencode/key_storage.lua index bfa36e74..0d1d3bd9 100644 --- a/lua/fittencode/key_storage.lua +++ b/lua/fittencode/key_storage.lua @@ -1,9 +1,12 @@ local fn = vim.fn local uv = vim.uv or vim.loop +local Base = require('fittencode.base') local FS = require('fittencode.fs') local Log = require('fittencode.log') +local schedule = Base.schedule + ---@class Key ---@field name string|nil ---@field key string|nil @@ -33,30 +36,22 @@ function KeyStorage:load(on_success, on_error) Log.debug('Loading key file: {}', self.path) if not FS.exists(self.path) then Log.error('Key file not found') - if on_error then - on_error() - end + schedule(on_error) return end FS.read(self.path, function(data) local success, result = pcall(fn.json_decode, data) if success == false then Log.error('Failed to parse key file; error: {}', result) - if on_error then - on_error() - end + schedule(on_error) return end self.keys = result Log.debug('Key file loaded successful') - if on_success ~= nil then - on_success(self.keys.name) - end + schedule(on_success, self.keys.name) end, function(err) Log.error('Failed to load Key file; error: {}', err) - if on_error then - on_error() - end + schedule(on_error) end) end @@ -64,17 +59,18 @@ end ---@param on_error function|nil function KeyStorage:save(on_success, on_error) Log.debug('Saving key file: {}', self.path) - local encode_keys = fn.json_encode(self.keys) + local success, encode_keys = pcall(fn.json_encode, self.keys) + if not success then + Log.error('Failed to encode key file; error: {}', encode_keys) + schedule(on_error) + return + end FS.write_mkdir(encode_keys, self.path, function() Log.info('Key file saved successful') - if on_success ~= nil then - on_success() - end + schedule(on_success) end, function(err) Log.error('Failed to save key file; error: {}', err) - if on_error then - on_error() - end + schedule(on_error) end) end @@ -86,14 +82,10 @@ function KeyStorage:clear(on_success, on_error) uv.fs_unlink(self.path, function(err) if err then Log.error('Failed to delete key file; error: {}', err) - if on_error then - on_error() - end + schedule(on_error) else Log.info('Delete key file successful') - if on_success then - on_success() - end + schedule(on_success) end end) end diff --git a/lua/fittencode/rest/backend/curl/init.lua b/lua/fittencode/rest/backend/curl/init.lua index 5e6a2a35..01ed8aa0 100644 --- a/lua/fittencode/rest/backend/curl/init.lua +++ b/lua/fittencode/rest/backend/curl/init.lua @@ -94,7 +94,12 @@ local function post_largedata(url, encoded_data, on_success, on_error) end function M:post(url, data, on_success, on_error) - local encoded_data = fn.json_encode(data) + local success, encoded_data = pcall(fn.json_encode, data) + if not success then + Log.error('Failed to encode data: {}', data) + schedule(on_error) + return + end if #encoded_data > 200 then return post_largedata(url, encoded_data, on_success, on_error) end