Fossil SCM

Improvements to comments on the filename shell quoting logic and test logic. No changes to code.

drh 2021-06-24 16:40 trunk
Commit 255a28b37a2241300b708b457f577bfcafe4bda7b3cd5a21318c3475f99ff672
2 files changed +9 -5 +1 -1
+9 -5
--- src/blob.c
+++ src/blob.c
@@ -1367,25 +1367,27 @@
13671367
**
13681368
** The argument is escaped if it contains white space or other characters
13691369
** that need to be escaped for the shell. If zIn contains characters
13701370
** that cannot be safely escaped, then throw a fatal error.
13711371
**
1372
-** The argument is expected to a filename of some kinds. As shell commands
1372
+** The argument is expected to be a filename. As shell commands
13731373
** commonly have command-line options that begin with "-" and since we
13741374
** do not want an attacker to be able to invoke these switches using
13751375
** filenames that begin with "-", if zIn begins with "-", prepend
1376
-** an additional "./".
1376
+** an additional "./" (or ".\\" on Windows).
13771377
*/
13781378
void blob_append_escaped_arg(Blob *pBlob, const char *zIn){
13791379
int i;
13801380
unsigned char c;
13811381
int needEscape = 0;
13821382
int n = blob_size(pBlob);
13831383
char *z = blob_buffer(pBlob);
13841384
1385
- /* Any control character is illegal. This prevents \n and \r in an
1386
- ** argument. */
1385
+ /* Look for illegal byte-sequences and byte-sequences that require
1386
+ ** escaping. No control-characters are allowed. All spaces and
1387
+ ** non-ASCII unicode characters and some punctuation characters require
1388
+ ** escaping. */
13871389
for(i=0; (c = (unsigned char)zIn[i])!=0; i++){
13881390
if( aSafeChar[c] ){
13891391
unsigned char x = aSafeChar[c];
13901392
needEscape = 1;
13911393
if( x==2 ){
@@ -1544,14 +1546,16 @@
15441546
unsigned char m, k;
15451547
int rc;
15461548
unsigned char zWord[100];
15471549
sqlite3_randomness(sizeof(m), &m);
15481550
m = (m%40)+5;
1549
- sqlite3_randomness(m, zWord);
1551
+ sqlite3_randomness(m, zWord); /* Between 5 and 45 bytes of randomness */
15501552
for(k=0; k<m; k++){
15511553
unsigned char cx = zWord[k];
15521554
if( cx<0x20 || cx>=0x7f ){
1555
+ /* Translate illegal bytes into various non-ASCII unicode
1556
+ ** characters in order to exercise those code paths */
15531557
unsigned int u;
15541558
if( cx>=0x7f ){
15551559
u = cx;
15561560
}else if( cx>=0x08 ){
15571561
u = 0x800 + cx;
15581562
--- src/blob.c
+++ src/blob.c
@@ -1367,25 +1367,27 @@
1367 **
1368 ** The argument is escaped if it contains white space or other characters
1369 ** that need to be escaped for the shell. If zIn contains characters
1370 ** that cannot be safely escaped, then throw a fatal error.
1371 **
1372 ** The argument is expected to a filename of some kinds. As shell commands
1373 ** commonly have command-line options that begin with "-" and since we
1374 ** do not want an attacker to be able to invoke these switches using
1375 ** filenames that begin with "-", if zIn begins with "-", prepend
1376 ** an additional "./".
1377 */
1378 void blob_append_escaped_arg(Blob *pBlob, const char *zIn){
1379 int i;
1380 unsigned char c;
1381 int needEscape = 0;
1382 int n = blob_size(pBlob);
1383 char *z = blob_buffer(pBlob);
1384
1385 /* Any control character is illegal. This prevents \n and \r in an
1386 ** argument. */
 
 
1387 for(i=0; (c = (unsigned char)zIn[i])!=0; i++){
1388 if( aSafeChar[c] ){
1389 unsigned char x = aSafeChar[c];
1390 needEscape = 1;
1391 if( x==2 ){
@@ -1544,14 +1546,16 @@
1544 unsigned char m, k;
1545 int rc;
1546 unsigned char zWord[100];
1547 sqlite3_randomness(sizeof(m), &m);
1548 m = (m%40)+5;
1549 sqlite3_randomness(m, zWord);
1550 for(k=0; k<m; k++){
1551 unsigned char cx = zWord[k];
1552 if( cx<0x20 || cx>=0x7f ){
 
 
1553 unsigned int u;
1554 if( cx>=0x7f ){
1555 u = cx;
1556 }else if( cx>=0x08 ){
1557 u = 0x800 + cx;
1558
--- src/blob.c
+++ src/blob.c
@@ -1367,25 +1367,27 @@
1367 **
1368 ** The argument is escaped if it contains white space or other characters
1369 ** that need to be escaped for the shell. If zIn contains characters
1370 ** that cannot be safely escaped, then throw a fatal error.
1371 **
1372 ** The argument is expected to be a filename. As shell commands
1373 ** commonly have command-line options that begin with "-" and since we
1374 ** do not want an attacker to be able to invoke these switches using
1375 ** filenames that begin with "-", if zIn begins with "-", prepend
1376 ** an additional "./" (or ".\\" on Windows).
1377 */
1378 void blob_append_escaped_arg(Blob *pBlob, const char *zIn){
1379 int i;
1380 unsigned char c;
1381 int needEscape = 0;
1382 int n = blob_size(pBlob);
1383 char *z = blob_buffer(pBlob);
1384
1385 /* Look for illegal byte-sequences and byte-sequences that require
1386 ** escaping. No control-characters are allowed. All spaces and
1387 ** non-ASCII unicode characters and some punctuation characters require
1388 ** escaping. */
1389 for(i=0; (c = (unsigned char)zIn[i])!=0; i++){
1390 if( aSafeChar[c] ){
1391 unsigned char x = aSafeChar[c];
1392 needEscape = 1;
1393 if( x==2 ){
@@ -1544,14 +1546,16 @@
1546 unsigned char m, k;
1547 int rc;
1548 unsigned char zWord[100];
1549 sqlite3_randomness(sizeof(m), &m);
1550 m = (m%40)+5;
1551 sqlite3_randomness(m, zWord); /* Between 5 and 45 bytes of randomness */
1552 for(k=0; k<m; k++){
1553 unsigned char cx = zWord[k];
1554 if( cx<0x20 || cx>=0x7f ){
1555 /* Translate illegal bytes into various non-ASCII unicode
1556 ** characters in order to exercise those code paths */
1557 unsigned int u;
1558 if( cx>=0x7f ){
1559 u = cx;
1560 }else if( cx>=0x08 ){
1561 u = 0x800 + cx;
1562
+1 -1
--- src/util.c
+++ src/util.c
@@ -182,11 +182,11 @@
182182
183183
/*
184184
** Check the input string to ensure that it is safe to pass into system().
185185
** A string is unsafe for system() on unix if it contains any of the following:
186186
**
187
-** * Any occurrance of '$' or '`' except after \
187
+** * Any occurrance of '$' or '`' except single-quoted or after \
188188
** * Any of the following characters, unquoted: ;|& or \n except
189189
** these characters are allowed as the very last character in the
190190
** string.
191191
** * Unbalanced single or double quotes
192192
**
193193
--- src/util.c
+++ src/util.c
@@ -182,11 +182,11 @@
182
183 /*
184 ** Check the input string to ensure that it is safe to pass into system().
185 ** A string is unsafe for system() on unix if it contains any of the following:
186 **
187 ** * Any occurrance of '$' or '`' except after \
188 ** * Any of the following characters, unquoted: ;|& or \n except
189 ** these characters are allowed as the very last character in the
190 ** string.
191 ** * Unbalanced single or double quotes
192 **
193
--- src/util.c
+++ src/util.c
@@ -182,11 +182,11 @@
182
183 /*
184 ** Check the input string to ensure that it is safe to pass into system().
185 ** A string is unsafe for system() on unix if it contains any of the following:
186 **
187 ** * Any occurrance of '$' or '`' except single-quoted or after \
188 ** * Any of the following characters, unquoted: ;|& or \n except
189 ** these characters are allowed as the very last character in the
190 ** string.
191 ** * Unbalanced single or double quotes
192 **
193

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button