Private/Remote.ps1
|
# Who can read the place a context is pushed to. A saved context holds whole sessions and masking finds only what is # shaped like a secret, so a context is not pushed where anyone can read it unless the person says so. function ConvertTo-PCContinuityProbeUri { # The address a git client asks first, and asks without credentials: <repository>/info/refs?service=git-upload-pack. # Nothing for a remote that is not on a host (a folder, a file:// address): only this machine and its shares read it. param([string]$Remote) $r = "$Remote".Trim() if (-not $r -or $r -match '^[A-Za-z]:([\\/]|$)' -or $r -match '^(file:|[\\/.~])') { return $null } $base = $null if ($r -match '^https?://(?:[^/@]+@)?(?<host>[^/]+)/(?<path>.+)$') { $base = "https://$($Matches.host)/$($Matches.path)" } elseif ($r -match '^(?:ssh|git)://(?:[^/@]+@)?(?<host>[^/:]+)(?::\d+)?/(?<path>.+)$') { $base = "https://$($Matches.host)/$($Matches.path)" } elseif ($r -match '^(?:[^/@\s]+@)?(?<host>[A-Za-z0-9][A-Za-z0-9.-]+):(?<path>[^/\\].*)$') { $base = "https://$($Matches.host)/$($Matches.path)" } if (-not $base) { return $null } $base.TrimEnd('/') + '/info/refs?service=git-upload-pack' } function Test-PCContinuityRemoteAccess { # public : the host gave the repository to a request with no credentials, so anyone can read it. # private : the host asked who was asking. # local : the remote is not on a host. # unknown : the host could not be asked, or answered something else. Not knowing is not the same as private. param([string]$Remote) $uri = ConvertTo-PCContinuityProbeUri $Remote if (-not $uri) { return [pscustomobject]@{ Access = 'local'; Detail = '' } } try { $response = Invoke-WebRequest -Uri $uri -Method Get -SkipHttpErrorCheck -TimeoutSec 20 -UserAgent 'git/2.0 (PowerCraft.Continuity)' } catch { return [pscustomobject]@{ Access = 'unknown'; Detail = "the host could not be asked: $($_.Exception.Message)" } } $status = [int]$response.StatusCode if ($status -eq 200 -and "$($response.Headers['Content-Type'])" -match 'git-upload-pack') { return [pscustomobject]@{ Access = 'public'; Detail = '' } } if ($status -in 401, 403) { return [pscustomobject]@{ Access = 'private'; Detail = '' } } [pscustomobject]@{ Access = 'unknown'; Detail = "the host answered $status" } } |