|
| 1 | +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')] |
| 2 | +param () |
| 3 | + |
| 4 | +BeforeDiscovery { |
| 5 | + try |
| 6 | + { |
| 7 | + if (-not (Get-Module -Name 'DscResource.Test')) |
| 8 | + { |
| 9 | + # Assumes dependencies have been resolved, so if this module is not available, run 'noop' task. |
| 10 | + if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) |
| 11 | + { |
| 12 | + # Redirect all streams to $null, except the error stream (stream 2) |
| 13 | + & "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null |
| 14 | + } |
| 15 | + |
| 16 | + # If the dependencies have not been resolved, this will throw an error. |
| 17 | + Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' |
| 18 | + } |
| 19 | + } |
| 20 | + catch [System.IO.FileNotFoundException] |
| 21 | + { |
| 22 | + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks noop" first.' |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +BeforeAll { |
| 27 | + $script:moduleName = 'SqlServerDsc' |
| 28 | + |
| 29 | + $env:SqlServerDscCI = $true |
| 30 | + |
| 31 | + Import-Module -Name $script:moduleName -ErrorAction 'Stop' |
| 32 | + |
| 33 | + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName |
| 34 | + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName |
| 35 | + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName |
| 36 | +} |
| 37 | + |
| 38 | +AfterAll { |
| 39 | + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') |
| 40 | + $PSDefaultParameterValues.Remove('Mock:ModuleName') |
| 41 | + $PSDefaultParameterValues.Remove('Should:ModuleName') |
| 42 | + |
| 43 | + Remove-Item -Path 'env:SqlServerDscCI' |
| 44 | +} |
| 45 | + |
| 46 | +Describe 'ConvertTo-EscapedQueryString' -Tag 'Private' { |
| 47 | + Context 'When escaping single quotes in query arguments' { |
| 48 | + It 'Should escape a single quote in an argument' { |
| 49 | + InModuleScope -ScriptBlock { |
| 50 | + Set-StrictMode -Version 1.0 |
| 51 | + |
| 52 | + $result = ConvertTo-EscapedQueryString -Query "SELECT * FROM Users WHERE Name = N'{0}'" -Argument "O'Brien" |
| 53 | + |
| 54 | + $result | Should -Be "SELECT * FROM Users WHERE Name = N'O''Brien'" |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + It 'Should escape multiple single quotes in an argument' { |
| 59 | + InModuleScope -ScriptBlock { |
| 60 | + Set-StrictMode -Version 1.0 |
| 61 | + |
| 62 | + $result = ConvertTo-EscapedQueryString -Query "SELECT * FROM Users WHERE Name = N'{0}'" -Argument "O'Brien's" |
| 63 | + |
| 64 | + $result | Should -Be "SELECT * FROM Users WHERE Name = N'O''Brien''s'" |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + It 'Should handle arguments without single quotes' { |
| 69 | + InModuleScope -ScriptBlock { |
| 70 | + Set-StrictMode -Version 1.0 |
| 71 | + |
| 72 | + $result = ConvertTo-EscapedQueryString -Query "SELECT * FROM Users WHERE Name = N'{0}'" -Argument 'Smith' |
| 73 | + |
| 74 | + $result | Should -Be "SELECT * FROM Users WHERE Name = N'Smith'" |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + Context 'When formatting a query with multiple arguments' { |
| 80 | + It 'Should escape single quotes in all arguments' { |
| 81 | + InModuleScope -ScriptBlock { |
| 82 | + Set-StrictMode -Version 1.0 |
| 83 | + |
| 84 | + $result = ConvertTo-EscapedQueryString -Query "EXECUTE sys.sp_adddistributor @distributor = N'{0}', @password = N'{1}';" -Argument 'Server1', "Pass'word;123" |
| 85 | + |
| 86 | + $result | Should -Be "EXECUTE sys.sp_adddistributor @distributor = N'Server1', @password = N'Pass''word;123';" |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + It 'Should handle multiple arguments with single quotes' { |
| 91 | + InModuleScope -ScriptBlock { |
| 92 | + Set-StrictMode -Version 1.0 |
| 93 | + |
| 94 | + $result = ConvertTo-EscapedQueryString -Query "INSERT INTO Users (FirstName, LastName) VALUES (N'{0}', N'{1}')" -Argument "Mary's", "O'Connor" |
| 95 | + |
| 96 | + $result | Should -Be "INSERT INTO Users (FirstName, LastName) VALUES (N'Mary''s', N'O''Connor')" |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + Context 'When handling special characters that could be used for SQL injection' { |
| 102 | + It 'Should escape single quotes in passwords with special characters' { |
| 103 | + InModuleScope -ScriptBlock { |
| 104 | + Set-StrictMode -Version 1.0 |
| 105 | + |
| 106 | + # Password with single quote, semicolon, and dashes |
| 107 | + $result = ConvertTo-EscapedQueryString -Query "EXECUTE sys.sp_adddistributor @password = N'{0}';" -Argument "Pass'word;--DROP TABLE Users" |
| 108 | + |
| 109 | + $result | Should -Be "EXECUTE sys.sp_adddistributor @password = N'Pass''word;--DROP TABLE Users';" |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + It 'Should handle argument with only single quotes' { |
| 114 | + InModuleScope -ScriptBlock { |
| 115 | + Set-StrictMode -Version 1.0 |
| 116 | + |
| 117 | + $result = ConvertTo-EscapedQueryString -Query "SELECT N'{0}'" -Argument "'''" |
| 118 | + |
| 119 | + $result | Should -Be "SELECT N''''''''" |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + It 'Should handle empty string argument' { |
| 124 | + InModuleScope -ScriptBlock { |
| 125 | + Set-StrictMode -Version 1.0 |
| 126 | + |
| 127 | + $result = ConvertTo-EscapedQueryString -Query "SELECT N'{0}'" -Argument '' |
| 128 | + |
| 129 | + $result | Should -Be "SELECT N''" |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments