Skip to content
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

Fetching varchar(max) as a stream results in Fatal Error with Always Encrypted #615

Closed
yitam opened this issue Dec 1, 2017 · 1 comment

Comments

@yitam
Copy link
Contributor

yitam commented Dec 1, 2017

Tested in Windows with the latest sqlsrv 5.1.2-preview

  1. Connect with ColumnEncryption enabled
  2. Create a table with one column of type varchar(max) with encryption
  3. Insert one row and retrieve data from this table as stream
  4. Fatal Error (seemingly stuck in an infinite loop).
  5. Same problem with UTF-8 option (connection or retrieval)

The repro script:

sqlsrv_query($conn, "CREATE TABLE [test_max_fields] ( [varchar_max_col] varchar(max) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (ENCRYPTION_TYPE = deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256', COLUMN_ENCRYPTION_KEY = AEColumnKey) )");

$inValue = str_repeat("ÃÜðßZZýA©", 1000);
$insertSql = "INSERT INTO [test_max_fields] (varchar_max_col) VALUES (?)";
$params = array($inValue);

$stmt = sqlsrv_prepare($conn, $insertSql, $params);
if ($stmt) {
    sqlsrv_execute($stmt);
} 

$query = "SELECT * FROM [test_max_fields]";
$stmt = sqlsrv_prepare($conn, $query);
if ($stmt) {
    sqlsrv_execute($stmt);
} 

 if (!sqlsrv_fetch($stmt)) { 
     fatalError("Failed to fetch row ");
 }

$stream = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR));

if ($stream === false) {
    fatalError("Failed to read field");
} else {
    $value = '';
    if ($stream) {
        $num = 0;
        while (!feof($stream)) {
            $value .= fread($stream, 8192); // infinite loop?! 
        }
        fclose($stream);
        
        if (!checkData($value, $inValue)) {  // compare the data to see if they match!
            fatalError("Data corruption!\n");
        }
    } else {
        fatalError("Error in getting stream!");
    }
}

@yitam yitam changed the title Fetching varchar(max) Fetching varchar(max) as a stream results in Fatal Error with Always Encrypted Dec 1, 2017
@yitam yitam added wontfix and removed wontfix labels Dec 14, 2017
@yitam
Copy link
Contributor Author

yitam commented Dec 20, 2017

Resolved in 5.2.0-RC

@yitam yitam closed this as completed Dec 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant