Skip to content

Commit 6a80b70

Browse files
committed
Add: PRAGMA secure_delete
ADD: Connection PRAGMA ADD: Build tag for secure_delete mode: FAST
1 parent 764e391 commit 6a80b70

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Boolean values can be one of:
8787
| Mutex Locking | `_mutex` | <ul><li>no</li><li>full</li></ul> | Specify mutex mode. |
8888
| Query Only | `_query_only` | `boolean` | For more information see [PRAGMA query_only](https://www.sqlite.org/pragma.html#pragma_query_only) |
8989
| Recursive Triggers | `_recursive_triggers` \| `_rt` | `boolean` | For more information see [PRAGMA recursive_triggers](https://www.sqlite.org/pragma.html#pragma_recursive_triggers) |
90+
| Secure Delete | `_secure_delete` | `boolean` \| `FAST` | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) |
9091
| Shared-Cache Mode | `cache` | <ul><li>shared</li><li>private</li></ul> | Set cache mode for more information see [sqlite.org](https://www.sqlite.org/sharedcache.html) |
9192
| Time Zone Location | `_loc` | auto | Specify location of time format. |
9293
| Transaction Lock | `_txlock` | <ul><li>immediate</li><li>deferred</li><li>exclusive</li></ul> | Specify locking behavior for transactions. |
@@ -137,6 +138,7 @@ go build --tags "icu json1 fts5 secure_delete"
137138
| Introspect PRAGMAS | sqlite_introspect | This option adds some extra PRAGMA statements. <ul><li>PRAGMA function_list</li><li>PRAGMA module_list</li><li>PRAGMA pragma_list</li></ul> |
138139
| JSON SQL Functions | sqlite_json | When this option is defined in the amalgamation, the JSON SQL functions are added to the build automatically |
139140
| Secure Delete | sqlite_secure_delete | This compile-time option changes the default setting of the secure_delete pragma.<br><br>When this option is not used, secure_delete defaults to off. When this option is present, secure_delete defaults to on.<br><br>The secure_delete setting causes deleted content to be overwritten with zeros. There is a small performance penalty since additional I/O must occur.<br><br>On the other hand, secure_delete can prevent fragments of sensitive information from lingering in unused parts of the database file after it has been deleted. See the documentation on the secure_delete pragma for additional information |
141+
| Secure Delete (FAST) | sqlite_secure_delete_fast | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) |
140142
| Tracing / Debug | sqlite_trace | Activate trace functions |
141143

142144
# Compilation

sqlite3.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,10 @@ func errorString(err Error) string {
862862
// _recursive_triggers=Boolean | _rt=Boolean
863863
// Enable or disable recursive triggers.
864864
//
865+
// _secure_delete=Boolean|FAST
866+
// When secure_delete is on, SQLite overwrites deleted content with zeros.
867+
// https://www.sqlite.org/pragma.html#pragma_secure_delete
868+
//
865869
// _vacuum=X
866870
// 0 | none - Auto Vacuum disabled
867871
// 1 | full - Auto Vacuum FULL
@@ -889,6 +893,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
889893
lockingMode := "NORMAL"
890894
queryOnly := -1
891895
recursiveTriggers := -1
896+
secureDelete := "DEFAULT"
892897

893898
pos := strings.IndexRune(dsn, '?')
894899
if pos >= 1 {
@@ -1109,6 +1114,23 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
11091114
}
11101115
}
11111116

1117+
// Secure Delete (_secure_delete)
1118+
//
1119+
// https://www.sqlite.org/pragma.html#pragma_secure_delete
1120+
//
1121+
if val := params.Get("_secure_delete"); val != "" {
1122+
switch strings.ToLower(val) {
1123+
case "0", "no", "false", "off":
1124+
secureDelete = "OFF"
1125+
case "1", "yes", "true", "on":
1126+
secureDelete = "ON"
1127+
case "fast":
1128+
secureDelete = "FAST"
1129+
default:
1130+
return nil, fmt.Errorf("Invalid _recursive_triggers: %v, expecting boolean value of '0 1 false true no yes off on'", val)
1131+
}
1132+
}
1133+
11121134
if !strings.HasPrefix(dsn, "file:") {
11131135
dsn = dsn[:pos]
11141136
}
@@ -1214,6 +1236,18 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
12141236
}
12151237
}
12161238

1239+
// Secure Delete
1240+
//
1241+
// Because this package can set the compile time flag SQLITE_SECURE_DELETE with a build tag
1242+
// the default value for secureDelete var is 'DEFAULT' this way
1243+
// you can compile with secure_delete 'ON' and disable it for a specific database connection.
1244+
if secureDelete != "DEFAULT" {
1245+
if err := exec(fmt.Sprintf("PRAGMA secure_delete = %s;", secureDelete)); err != nil {
1246+
C.sqlite3_close_v2(db)
1247+
return nil, err
1248+
}
1249+
}
1250+
12171251
conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
12181252

12191253
if len(d.Extensions) > 0 {

sqlite3_opt_secure_delete.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
2+
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
23
//
34
// Use of this source code is governed by an MIT-style
45
// license that can be found in the LICENSE file.
@@ -8,7 +9,7 @@
89
package sqlite3
910

1011
/*
11-
#cgo CFLAGS: -DSQLITE_SECURE_DELETE
12+
#cgo CFLAGS: -DSQLITE_SECURE_DELETE=1
1213
#cgo LDFLAGS: -lm
1314
*/
1415
import "C"

sqlite3_opt_secure_delete_fast.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
2+
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
3+
//
4+
// Use of this source code is governed by an MIT-style
5+
// license that can be found in the LICENSE file.
6+
7+
// +build sqlite_secure_delete_fast
8+
9+
package sqlite3
10+
11+
/*
12+
#cgo CFLAGS: -DSQLITE_SECURE_DELETE=FAST
13+
#cgo LDFLAGS: -lm
14+
*/
15+
import "C"

0 commit comments

Comments
 (0)