Skip to content

Commit

Permalink
SQLite: check for base64 for text content in blob
Browse files Browse the repository at this point in the history
If the SQLite driver returns the value of a blob column a string, don't make the assumption that the value is base64-encoded. Instead, check for actual base64 content by trying to decode the string as base64. See dimitri#415 .
  • Loading branch information
darioleanbit authored Mar 15, 2023
1 parent 999791d commit fa9cf8c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/sources/sqlite/sqlite.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

(in-package :pgloader.source.sqlite)

(in-package :pgloader.source.sqlite)

(declaim (inline has-base64-sequence-p))

;;; Return the decoded value of a base64 string, ignoring errors.
;;; Returns nil if the string doesn't contain a valid base64 string.
;;;
(defun has-base64-value-p (string)
(ignore-errors
(base64:base64-string-to-string string)))

;;; Map a function to each row extracted from SQLite
;;;
(declaim (inline parse-value))
Expand All @@ -20,10 +31,16 @@
(babel:octets-to-string value :encoding encoding))

((and (string-equal "bytea" pgsql-type)
(has-base64-value-p value)
(stringp value))
;; we expected bytes and got a string instead, must be base64 encoded
;; we expected bytes and got a be base64 encoded, string instead
(base64:base64-string-to-usb8-array value))

((and (string-equal "bytea" pgsql-type)
(stringp value))
;; we expected bytes and got a string instead, convert it to octets
(babel:string-to-octets value :encoding :utf-8))

;; default case, just use what's been given to us
(t value)))

Expand Down

0 comments on commit fa9cf8c

Please sign in to comment.