Stairway to SQLCLR Level 3: Security (General and SAFE Assemblies)

  • Comments posted to this topic are about the item Stairway to SQLCLR Level 3: Security (General and SAFE Assemblies)

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • Solomon, thx for your great articles about CLR.

    For quite some time now I'm looking for a way to protect my CLR functions from unauthorized distribution to an arbitrary number of sql servers. Is there a possibility to bring in some sort of license into an assembly that needs to be varified and cannot be re-distributed by script?
    I already thought of a license key that is stored inside the master database of the licensed server. It could be queried/validated from inside the assembly, but that would mean a horrible performance loss to the CLR functions. I didn't find any way to validate a license once and then keep it validated for it's next calls.

    Does anyone have any further ideas on this?

  • flatmail - Wednesday, April 11, 2018 9:50 AM

    Solomon, thx for your great articles about CLR.

    For quite some time now I'm looking for a way to protect my CLR functions from unauthorized distribution to an arbitrary number of sql servers. Is there a possibility to bring in some sort of license into an assembly that needs to be varified and cannot be re-distributed by script?
    I already thought of a license key that is stored inside the master database of the licensed server. It could be queried/validated from inside the assembly, but that would mean a horrible performance loss to the CLR functions. I didn't find any way to validate a license once and then keep it validated for it's next calls.

    You are quite welcome :).

    I, too, have had similar thoughts with respect to protecting my SQL# project. I have thought that maybe using a static constructor to load a static, readonly, class variable (bool) that could be checked by each function would alleviate the performance hit, though that would still require testing since it is additional steps that need to be take per call to even just check that variable. But the problem with using a static constructor is that there is no SqlContext / Context Connection when it executes since that happens as the class is being loaded. So the timing of when it executes is correct, and happening only once is correct, but no chance of getting data from the DB or even file system. That is, unless you have an EXTERNAL_ACCESS or UNSAFE assembly that can read from the file system or connect back to the host DB via a regular / external connection. However, for my purposes I need something that works with a SAFE Assembly.

    One option seems to be that you could reference another Assembly that contains only the license key, but then how to dynamically create and distribute that assembly containing only a single variable?

    The only other thought I had been considering was using the application config file (i.e. sqlservr.exe.Config ) as that is readable in a SAFE Assembly, as long as you stick the license key into one of the two pre-defined lists (i.e. ConnectionStrings or AppSettings ). The problem there is that, as far as my testing showed, the config file was not accessible on Linux, and I need something that will work everywhere. Also, now that I think about it, that same concern would be true for the new Managed Instances of Azure SQL Database. So that's 2 of the 3 platforms that SQLCLR works on that wouldn't support this.

    So for now I have opted to not worry about it. But it is still an interesting problem that I think about from time to time. If you have any other thoughts about this I would love to hear them.

    Take care,
    Solomon..

    SQL#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • Solomon Rutzky - Wednesday, April 11, 2018 10:35 AM

    flatmail - Wednesday, April 11, 2018 9:50 AM

    Solomon, thx for your great articles about CLR.

    For quite some time now I'm looking for a way to protect my CLR functions from unauthorized distribution to an arbitrary number of sql servers. Is there a possibility to bring in some sort of license into an assembly that needs to be varified and cannot be re-distributed by script?
    I already thought of a license key that is stored inside the master database of the licensed server. It could be queried/validated from inside the assembly, but that would mean a horrible performance loss to the CLR functions. I didn't find any way to validate a license once and then keep it validated for it's next calls.

    You are quite welcome :).

    I, too, have had similar thoughts with respect to protecting my SQL# project. I have thought that maybe using a static constructor to load a static, readonly, class variable (bool) that could be checked by each function would alleviate the performance hit, though that would still require testing since it is additional steps that need to be take per call to even just check that variable. But the problem with using a static constructor is that there is no SqlContext / Context Connection when it executes since that happens as the class is being loaded. So the timing of when it executes is correct, and happening only once is correct, but no chance of getting data from the DB or even file system. That is, unless you have an EXTERNAL_ACCESS or UNSAFE assembly that can read from the file system or connect back to the host DB via a regular / external connection. However, for my purposes I need something that works with a SAFE Assembly.

    One option seems to be that you could reference another Assembly that contains only the license key, but then how to dynamically create and distribute that assembly containing only a single variable?

    The only other thought I had been considering was using the application config file (i.e. sqlservr.exe.Config ) as that is readable in a SAFE Assembly, as long as you stick the license key into one of the two pre-defined lists (i.e. ConnectionStrings or AppSettings ). The problem there is that, as far as my testing showed, the config file was not accessible on Linux, and I need something that will work everywhere. Also, now that I think about it, that same concern would be true for the new Managed Instances of Azure SQL Database. So that's 2 of the 3 platforms that SQLCLR works on that wouldn't support this.

    So for now I have opted to not worry about it. But it is still an interesting problem that I think about from time to time. If you have any other thoughts about this I would love to hear them.

    Take care,
    Solomon..

    Thanks you for your quick reply.
    You are absolutely right about the static constructor. I also tested this approach by creating a singleton that holds the key, but it forced me to sign the assembly UNSAFE. Since most db admins are not willing to inject UNSAFE assemblies in their sql server, this solution won't work for me either.
    Thanks for mentioning the point about Linux and Azure. I didn't think about that at all.
    Well, I don't have any breaking new ideas now, but if I should come across some, I will be pleased to post them here.

    Thx a lot for sharing your experiences.

    Happy coding,
    Jens

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply