Groovy,创建MSSQL数据库的安全方法

时间:2016-06-13 09:40:11

标签: sql-server database groovy sql-injection

我正在尝试使用groovy.sql.Sql在MSSQL(Microsoft SQL Server)服务器中创建数据库。似乎准备好的语句在最后一个打破查询的参数周围添加了额外的引号。

此测试代码:

Failed to execute: IF EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE name = ?) DROP DATABASE ? because: Incorrect syntax near '@P1'.

给出错误:

#region Assembly OpcComRcw, Version=2.0.105.1, Culture=neutral, PublicKeyToken=9a40e993cbface53
// C:\WINDOWS\assembly\GAC_MSIL\OpcComRcw\2.0.105.1__9a40e993cbface53\OpcComRcw.dll
#endregion

using System;
using System.Runtime.InteropServices;

namespace OpcRcw.Comn
{
    [Guid( "9DD0B56C-AD9E-43ee-8305-487F3188BF7A" )]
    [InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
    public interface IOPCServerList2
    {
        void CLSIDFromProgID( string szProgId, out Guid clsid );
        void EnumClassesOfCategories( int cImplemented, Guid[ ] rgcatidImpl, int cRequired, Guid[ ] rgcatidReq, out IOPCEnumGUID ppenumClsid );
        void GetClassDetails( ref Guid clsid, out string ppszProgID, out string ppszUserType, out string ppszVerIndProgID );
    }
}

如何在没有在参数1周围添加单引号的情况下使用预准备语句(DROP DATABASE 似乎被重写为DROP DATABASE '?')或者我可以以不同的方式编写查询,以便添加的单引号不会产生语法错误?

如果有人能给我一个有效的例子,我对其他框架也没关系。

1 个答案:

答案 0 :(得分:0)

你可以尝试:

connection.execute(
    "IF EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE name = $databaseName) DROP DATABASE ${Sql.expand(databseName)}"
)