2011-01-10 6 views
0

SQL Server 2008 CLRプロジェクトにストアドプロシージャがあります。この関数は、xmlを含むパラメータでコンソールアプリケーションを呼び出します。System.Diagnostics.Process.Xmlでparamを含むSQL CLR関数を呼び出す

[Microsoft.SqlServer.Server.SqlProcedure()] 
public static SqlInt32 ExecuteApp(SqlString utilPath, SqlString arguments, ref SqlString result) 
{ 
    ProcessStartInfo info = new ProcessStartInfo("cmd.exe"); 
    Process p = new Process(); 

    try 
    { 
     //ProcessStartInfo to run the DOS command attrib 

     info.RedirectStandardOutput = true; 
     info.RedirectStandardError = true; 
     info.UseShellExecute = false; 
     info.CreateNoWindow = true; 
     info.Arguments = string.Format(@"/c ""{0}"" {1}", utilPath, arguments); 
     SqlContext.Pipe.Send(info.Arguments); 

     p.StartInfo = info; 
     p.Start(); 
     String processResults = p.StandardOutput.ReadToEnd(); 
     SqlContext.Pipe.Send(processResults); 
     result = processResults; 
     if (String.IsNullOrEmpty((String)result)) 
      result = p.StandardError.ReadToEnd(); 

     return 1; 
    } 
    finally 
    { 
     p.Close(); 
    } 
} 

xmlを含むparamの問題はここにあります。 これは、ストアドプロシージャの実行のための式である:私はのようなエラーを持っている

declare @Result nvarchar(max) 
exec ExecuteApp 'C:\Console.exe', 'send "<messages><message>my message</message></messages>"', @Result out 
select @Result 

ストアドプロシージャを実行した後:

<は、この時点では予想外でしたが。

xmlがすべて正しく機能していないことに気づきたいと思います。

+0

セキュリティ上の脆弱性が深刻です。 – SLaks

+1

なぜあなたは 'xp_cmdshell'を使用していませんか? – SLaks

+0

xp_cmdshellはサーバーレベルでも有効にする必要があり、注入攻撃のために全く新しい方法を開きます。 – Shiv

答えて

1

XMLには、コマンドシェル(>および<)によって解釈される特殊文字が含まれています。
パラメータの引用符とその内部の引用符は、""でエスケープする必要があります。

また、プログラムを直接実行する必要があります(cmd /cではなく)。あなたの問題のいくつかを解決します。

+0

迅速な対応をありがとうございます。しかし、実際の実行文字列は次のようになります。 – apros

+0

exec ExecuteApp 'C:\ Console.exe'、 'send "私のメッセージ"'、@Result out select @Result。だから、 "<"記号で。だから、コンソールアプリのレスポンスに "<今のところ予期しなかった"という出力が含まれているのはなぜですか? – apros

+0

あなたが何を言っているのか分かりません。私の答えを読む。あなたの問題を説明します。 – SLaks

関連する問題