-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes for issues found by Semmle #1011
Changes from 9 commits
680ddd9
821692c
87948c2
a485029
3ce5e27
d86f1b9
f7fe1e7
2497950
1b108df
cbdc84c
574c7a6
0cb5b09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -704,22 +704,50 @@ bool core_is_conn_opt_value_escaped( _Inout_ const char* value, _Inout_ size_t v | |
{ | ||
// if the value is already quoted, then only analyse the part inside the quotes and return it as | ||
// unquoted since we quote it when adding it to the connection string. | ||
if( value_len > 0 && value[0] == '{' && value[value_len - 1] == '}' ) { | ||
if (value_len > 0 && value[0] == '{' && value[value_len - 1] == '}') { | ||
++value; | ||
value_len -= 2; | ||
} | ||
// check to make sure that all right braces are escaped | ||
size_t i = 0; | ||
while( ( value[i] != '}' || ( value[i] == '}' && value[i+1] == '}' )) && i < value_len ) { | ||
// skip both braces | ||
if( value[i] == '}' ) | ||
++i; | ||
++i; | ||
|
||
// Check to make sure that all right braces are escaped | ||
// First check the case where there is one character | ||
if (value_len == 1) { | ||
if (value[0] == '}') { | ||
return false; | ||
} | ||
else { | ||
return true; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can simplify the above with the following:
|
||
if( i < value_len && value[i] == '}' ) { | ||
|
||
// Check the case where there is one } at the end | ||
if (value[value_len - 2] != '}' && value[value_len - 1] == '}') { | ||
return false; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code seems to assume In fact, consider using strchr() for parsing the string, something like this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe the last call to strchr may overshoot the end of the buffer. But I will try it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes @david-puglielli please double check whether this might happen, fix the formatting of this section and add an equivalent test for pdo_sqlsrv There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want to get some idea from the deleted tests in #905 |
||
// Now run through the string starting at the second position | ||
size_t i = 1; | ||
while (i < value_len) { | ||
// If there are two }, the } is escaped | ||
// Otherwise check for one } | ||
// Otherwise simply advance the index | ||
if (value[i] == '}'&&value[i - 1] == '}') { | ||
i += 2; | ||
|
||
// Since i advanced by 2, check the case where three } lie at the end of the string | ||
// Note if there are 2 } at the end, i is value_len+1 so does not trigger this | ||
if (i == value_len && value[value_len - 1] == '}') { | ||
return false; | ||
} | ||
} | ||
else if (value[i - 1] == '}' && value[i] != '}') { | ||
return false; | ||
} | ||
else { | ||
++i; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--TEST-- | ||
Test that right braces are escaped correctly and that error messages are correct when they're not | ||
--SKIPIF-- | ||
<?php require('skipif.inc'); ?> | ||
--FILE-- | ||
<?php | ||
$server = 'fakeserver'; | ||
$uid = 'sa'; | ||
$password = 'fakepassword'; | ||
|
||
// If the braces are fine, then we expect the connection to fail with a login timeout error | ||
$braceError = "An unescaped right brace (}) was found"; | ||
$connError = (strtoupper(substr(php_uname('s'), 0, 3)) === 'WIN') ? "Could not open a connection to SQL Server" : "Login timeout expired"; | ||
|
||
// Every combination of one, two, three, or more right braces I can think of | ||
$testStrings = array(array("}", $braceError), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As shown in #905 please add test cases with strings that consist of punctuation and/or space characters? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That doesn't really have anything to do with escaping braces though - the code changes only affect braces. Should there be a separate test for nonalphanumeric characters in strings? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok you can leave that. I think we have a backlog item created already |
||
array("{", $connError), | ||
array("{t}", $connError), | ||
array("{}}", $braceError), | ||
array("}}", $connError), | ||
array("}}}", $braceError), | ||
array("}}}}", $connError), | ||
array("{}}}", $connError), | ||
array("}{", $braceError), | ||
array("}{{", $braceError), | ||
array("test", $connError), | ||
array("{test}", $connError), | ||
array("{test", $connError), | ||
array("test}", $braceError), | ||
array("{{test}}", $braceError), | ||
array("{{test}", $connError), | ||
array("{{test", $connError), | ||
array("test}}", $connError), | ||
array("{test}}", $braceError), | ||
array("test}}}", $braceError), | ||
array("{test}}}", $connError), | ||
array("{test}}}}", $braceError), | ||
array("{test}}}}}", $connError), | ||
array("{test}}}}}}", $braceError), | ||
array("te}st", $braceError), | ||
array("{te}st}", $braceError), | ||
array("{te}}st}", $connError), | ||
array("{te}}}st}", $braceError), | ||
array("te}}s}t", $braceError), | ||
array("te}}s}}t", $connError), | ||
array("te}}}st", $braceError), | ||
array("te}}}}st", $connError), | ||
array("tes}}t", $connError), | ||
array("te}s}}t", $braceError), | ||
array("tes}}t}}", $connError), | ||
array("tes}}t}}}", $braceError), | ||
array("tes}t}}", $braceError), | ||
); | ||
|
||
foreach ($testStrings as $test) { | ||
|
||
$conn = sqlsrv_connect($server, array('uid'=>$test[0], 'pwd'=>$password, 'LoginTimeout'=>1)); | ||
|
||
if (strpos(sqlsrv_errors()[0][2], $test[1]) === false) { | ||
print_r("Wrong error message returned for test string ".$test[0].". Expected ".$test[1].", actual output:\n"); | ||
print_r(sqlsrv_errors()); | ||
} | ||
|
||
unset($conn); | ||
} | ||
|
||
echo "Done.\n"; | ||
?> | ||
--EXPECT-- | ||
Done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should test if
value_len == 0
right at the beginning