Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

65 rindas
4.2KB

  1. $ErrorActionPreference = 'Stop'
  2. $projectRoot = Split-Path -Parent $PSScriptRoot
  3. $workPath = Join-Path $projectRoot ('temp\template-flow-' + [guid]::NewGuid().ToString('N'))
  4. $templateRoot = Join-Path $workPath 'templates'
  5. $assetRoot = Join-Path $templateRoot 'images'
  6. $databasePath = Join-Path $workPath 'test.mdb'
  7. $configPath = Join-Path $workPath 'test.ini'
  8. $previewRoot = Join-Path $workPath 'previews'
  9. $templatePath = Join-Path $templateRoot 'postcard.html'
  10. $imagePath = Join-Path $assetRoot 'logo.png'
  11. $scriptHost = Join-Path $env:SystemRoot 'SysWOW64\cscript.exe'
  12. $powerShellX86 = Join-Path $env:SystemRoot 'SysWOW64\WindowsPowerShell\v1.0\powershell.exe'
  13. try {
  14. $null = New-Item -ItemType Directory -Path $assetRoot -Force
  15. $example = Get-Content -LiteralPath (Join-Path $projectRoot 'config.example.ini') -Raw
  16. $example.Replace('DB_PATH=data/MassMailer.mdb', ('DB_PATH=' + $databasePath)).Replace(
  17. 'TEMPLATE_ROOT=templates', ('TEMPLATE_ROOT=' + $templateRoot)) |
  18. Set-Content -LiteralPath $configPath -Encoding UTF8
  19. & $scriptHost //nologo (Join-Path $PSScriptRoot 'init-db.vbs') $configPath
  20. if ($LASTEXITCODE -ne 0) { throw 'MDB initialization failed' }
  21. $png = [Convert]::FromBase64String('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII=')
  22. [System.IO.File]::WriteAllBytes($imagePath, $png)
  23. $safe = '<html><body><h1 style="color:#112233; font-size:20px">Postcard</h1><p>See our offer.</p><img src="images/logo.png" width="1" height="1" alt="Logo" /><a href="http://example.invalid">Details</a></body></html>'
  24. [System.IO.File]::WriteAllText($templatePath, $safe, [System.Text.Encoding]::UTF8)
  25. & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'import-template.ps1') -Path $templatePath -ConfigPath $configPath
  26. if ($LASTEXITCODE -ne 0) { throw 'Valid template import failed' }
  27. Remove-Item -LiteralPath $imagePath -Force
  28. $preview = & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'preview-template.ps1') -TemplateId 1 -ConfigPath $configPath -OutputRoot $previewRoot
  29. if ($LASTEXITCODE -ne 0 -or -not (Test-Path -LiteralPath $preview -PathType Leaf)) { throw 'Snapshot preview failed after source image deletion' }
  30. $rendered = Get-Content -LiteralPath $preview -Raw
  31. if ($rendered -notmatch 'image-1.png' -or $rendered -match 'cid:|file:|<script') { throw 'Preview contains an unsafe or missing image reference' }
  32. $savedImage = Join-Path (Split-Path -Parent $preview) 'image-1.png'
  33. if (([Convert]::ToBase64String([System.IO.File]::ReadAllBytes($savedImage))) -ne ([Convert]::ToBase64String($png))) {
  34. throw 'Preview image differs from imported snapshot'
  35. }
  36. foreach ($invalid in @(
  37. $safe.Replace('<p>See our offer.</p>', '<script>alert(1)</script>'),
  38. $safe.Replace('images/logo.png', '../secret.png'),
  39. $safe.Replace('color:#112233', 'background-image:url(file:///C:/secret)'),
  40. $safe.Replace('http://example.invalid', 'javascript:alert(1)')
  41. )) {
  42. [System.IO.File]::WriteAllText($templatePath, $invalid, [System.Text.Encoding]::UTF8)
  43. $ErrorActionPreference = 'Continue'
  44. $null = & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'import-template.ps1') -Path $templatePath -ConfigPath $configPath 2>$null
  45. $rejected = $LASTEXITCODE -ne 0
  46. $ErrorActionPreference = 'Stop'
  47. if (-not $rejected) { throw 'Unsafe template was accepted' }
  48. }
  49. & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'test-template-state.ps1') -ConfigPath $configPath
  50. if ($LASTEXITCODE -ne 0) { throw 'Invalid imports changed the template library' }
  51. Write-Output 'Template import, image snapshot, safe preview, and rejection checks passed'
  52. }
  53. finally {
  54. $resolvedWork = [System.IO.Path]::GetFullPath($workPath)
  55. $resolvedTemp = [System.IO.Path]::GetFullPath((Join-Path $projectRoot 'temp'))
  56. if ($resolvedWork.StartsWith($resolvedTemp + [System.IO.Path]::DirectorySeparatorChar,
  57. [StringComparison]::OrdinalIgnoreCase) -and (Test-Path -LiteralPath $resolvedWork)) {
  58. Remove-Item -LiteralPath $resolvedWork -Recurse -Force
  59. }
  60. }

Powered by TurnKey Linux.