Compare commits

..

6 Commits

Author SHA1 Message Date
MengMengCode 39ccbad29a release: v1.1.29 2026-08-04 05:17:08 +08:00
MengMengCode a736e156bb fix something 2026-08-04 05:13:45 +08:00
MengMengCode 23a0541f7c feat: rename functions and variables for clarity in safehttp package; enhance URL validation tests 2026-08-04 05:05:45 +08:00
Meng Meng a28ae727f3 Merge pull request #37 from LusineStar/fix/windows-firstlogon-drive-scan
fix(kvm): Windows 首启脚本盘符扫描的批处理语法错误,导致 RDP/入站端口全被丢弃
2026-07-26 21:47:11 +08:00
LusineStar 57d6b19c05 fix(kvm): correct Windows FirstLogon drive-scan batch syntax
The unattend FirstLogonCommands runs via 'cmd.exe /c' (command-line context),
where a FOR loop variable must be %d, not the batch-file form %%d. The %%d
form raised '%%d was unexpected at this time', so the else branch that locates
and runs FirstLogon.ps1 from the unattend ISO never executed.

Combined with $OEM$ not being processed from a separate ISO (so
C:\CLICD\FirstLogon.ps1 is never staged), the entire Windows post-install
init was skipped: RDP was never enabled, the firewall was never opened, and
the network profile stayed Public, so the guest dropped all inbound traffic.
The host NAT DNAT for 3389 was correct, but the guest silently dropped it,
making the mapping appear to have no effect.
2026-07-26 21:42:06 +08:00
Meng Meng bae028ade4 Merge pull request #36 from MengMengCode/copilot/fix-code-scanning-alerts-146
Triage CodeQL alert #146 as false positive (go/request-forgery)
2026-07-26 04:49:15 +08:00
7 changed files with 110 additions and 39 deletions
+2 -2
View File
@@ -84,12 +84,12 @@ func TestCustomLXCImageCreateRejectsInvalidSource(t *testing.T) {
}
}
func TestCustomImageCreateRejectsPrivateNetworkSource(t *testing.T) {
func TestCustomImageCreateRejectsMetadataSource(t *testing.T) {
for _, imageType := range []string{"lxc", "kvm"} {
t.Run(imageType, func(t *testing.T) {
payload := map[string]string{
"type": imageType,
"name": "Private Network Source",
"name": "Metadata Source",
"distro": "ubuntu",
"release": "noble",
"arch": runtime.GOARCH,
+1 -1
View File
@@ -1906,7 +1906,7 @@ func windowsAutounattendXML(hostname, adminPassword string, windows11 bool) stri
hostname = "clicd-win"
}
hostname = sanitizeWindowsComputerName(hostname)
setupCommand := `cmd.exe /c if exist C:\CLICD\FirstLogon.ps1 (powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\CLICD\FirstLogon.ps1) else (for %%d in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do @if exist %%d:\FirstLogon.ps1 powershell.exe -NoProfile -ExecutionPolicy Bypass -File %%d:\FirstLogon.ps1)`
setupCommand := `cmd.exe /c if exist C:\CLICD\FirstLogon.ps1 (powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\CLICD\FirstLogon.ps1) else (for %d in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do @if exist %d:\FirstLogon.ps1 powershell.exe -NoProfile -ExecutionPolicy Bypass -File %d:\FirstLogon.ps1)`
compatibilityCommands := ""
if windows11 {
compatibilityCommands = `
+24 -24
View File
@@ -14,17 +14,14 @@ import (
const maxRedirects = 10
var blockedPrefixes = []netip.Prefix{
var blockedDownloadPrefixes = []netip.Prefix{
netip.MustParsePrefix("0.0.0.0/8"),
netip.MustParsePrefix("10.0.0.0/8"),
netip.MustParsePrefix("100.64.0.0/10"),
netip.MustParsePrefix("100.100.100.200/32"),
netip.MustParsePrefix("127.0.0.0/8"),
netip.MustParsePrefix("169.254.0.0/16"),
netip.MustParsePrefix("172.16.0.0/12"),
netip.MustParsePrefix("192.0.0.0/24"),
netip.MustParsePrefix("192.0.2.0/24"),
netip.MustParsePrefix("192.88.99.0/24"),
netip.MustParsePrefix("192.168.0.0/16"),
netip.MustParsePrefix("198.18.0.0/15"),
netip.MustParsePrefix("198.51.100.0/24"),
netip.MustParsePrefix("203.0.113.0/24"),
@@ -40,7 +37,7 @@ var blockedPrefixes = []netip.Prefix{
netip.MustParsePrefix("2001:db8::/32"),
netip.MustParsePrefix("2001:20::/28"),
netip.MustParsePrefix("2002::/16"),
netip.MustParsePrefix("fc00::/7"),
netip.MustParsePrefix("fd00:ec2::254/128"),
netip.MustParsePrefix("fec0::/10"),
netip.MustParsePrefix("fe80::/10"),
netip.MustParsePrefix("ff00::/8"),
@@ -74,13 +71,15 @@ func ValidateURL(rawURL string) (*url.URL, error) {
return nil, fmt.Errorf("download URL contains an invalid port")
}
}
if addr, err := netip.ParseAddr(parsed.Hostname()); err == nil && !isPublicAddress(addr) {
return nil, fmt.Errorf("download URL resolves to a non-public address")
if addr, err := netip.ParseAddr(parsed.Hostname()); err == nil && !isAllowedDownloadAddress(addr) {
return nil, fmt.Errorf("download URL resolves to a blocked address")
}
return parsed, nil
}
// Get retrieves a resource only when every resolved destination is public.
// Get retrieves a resource only when every resolved destination is safe for
// image downloads. Private network image mirrors are allowed; loopback,
// link-local, metadata, multicast and reserved destinations remain blocked.
func Get(ctx context.Context, rawURL, userAgent string, timeout time.Duration) (*http.Response, error) {
parsed, err := ValidateURL(rawURL)
if err != nil {
@@ -98,7 +97,7 @@ func Get(ctx context.Context, rawURL, userAgent string, timeout time.Duration) (
client := &http.Client{
Timeout: timeout,
Transport: publicTransport(net.DefaultResolver),
Transport: restrictedTransport(net.DefaultResolver),
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) >= maxRedirects {
return fmt.Errorf("too many redirects")
@@ -117,13 +116,14 @@ func Get(ctx context.Context, rawURL, userAgent string, timeout time.Duration) (
},
}
// All URL components, redirects, DNS answers and dial destinations are
// constrained above and in publicTransport.
// lgtm[go/request-forgery]
// The URL, redirects, DNS answers and dial destinations are constrained
// above and in restrictedTransport. CodeQL cannot infer those checks across
// the custom transport boundary.
// codeql[go/request-forgery]
return client.Do(request)
}
func publicTransport(resolver *net.Resolver) *http.Transport {
func restrictedTransport(resolver *net.Resolver) *http.Transport {
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
@@ -135,7 +135,7 @@ func publicTransport(resolver *net.Resolver) *http.Transport {
if err != nil {
return nil, fmt.Errorf("invalid download destination: %v", err)
}
addresses, err := resolvePublicHost(ctx, resolver, host)
addresses, err := resolveAllowedHost(ctx, resolver, host)
if err != nil {
return nil, err
}
@@ -159,11 +159,11 @@ func publicTransport(resolver *net.Resolver) *http.Transport {
}
func validateHost(ctx context.Context, resolver *net.Resolver, host string) error {
_, err := resolvePublicHost(ctx, resolver, host)
_, err := resolveAllowedHost(ctx, resolver, host)
return err
}
func resolvePublicHost(ctx context.Context, resolver *net.Resolver, host string) ([]netip.Addr, error) {
func resolveAllowedHost(ctx context.Context, resolver *net.Resolver, host string) ([]netip.Addr, error) {
host = strings.TrimSpace(strings.TrimSuffix(host, "."))
if host == "" {
return nil, fmt.Errorf("download URL host is empty")
@@ -174,8 +174,8 @@ func resolvePublicHost(ctx context.Context, resolver *net.Resolver, host string)
if addr, err := netip.ParseAddr(host); err == nil {
addr = addr.Unmap()
if !isPublicAddress(addr) {
return nil, fmt.Errorf("download URL resolves to a non-public address")
if !isAllowedDownloadAddress(addr) {
return nil, fmt.Errorf("download URL resolves to a blocked address")
}
return []netip.Addr{addr}, nil
}
@@ -190,22 +190,22 @@ func resolvePublicHost(ctx context.Context, resolver *net.Resolver, host string)
result := make([]netip.Addr, 0, len(addresses))
for _, address := range addresses {
address = address.Unmap()
if !isPublicAddress(address) {
return nil, fmt.Errorf("download host resolves to a non-public address")
if !isAllowedDownloadAddress(address) {
return nil, fmt.Errorf("download host resolves to a blocked address")
}
result = append(result, address)
}
return result, nil
}
func isPublicAddress(address netip.Addr) bool {
if !address.IsValid() || address.Zone() != "" || !address.IsGlobalUnicast() || address.IsPrivate() ||
func isAllowedDownloadAddress(address netip.Addr) bool {
if !address.IsValid() || address.Zone() != "" || !address.IsGlobalUnicast() ||
address.IsLoopback() || address.IsLinkLocalUnicast() || address.IsLinkLocalMulticast() ||
address.IsMulticast() || address.IsUnspecified() {
return false
}
address = address.Unmap()
for _, prefix := range blockedPrefixes {
for _, prefix := range blockedDownloadPrefixes {
if prefix.Contains(address) {
return false
}
+80 -9
View File
@@ -2,6 +2,9 @@ package safehttp
import (
"context"
"io"
"net"
"net/http"
"net/netip"
"testing"
"time"
@@ -15,9 +18,8 @@ func TestValidateURLRejectsUnsafeDestinations(t *testing.T) {
"http://127.0.0.1/image",
"http://[::1]/image",
"http://169.254.169.254/latest/meta-data",
"http://10.0.0.1/image",
"http://192.168.1.10/image",
"http://100.64.0.1/image",
"http://100.100.100.200/latest/meta-data",
"http://[fd00:ec2::254]/latest/meta-data",
"http://example.com:99999/image",
} {
if _, err := ValidateURL(rawURL); err == nil {
@@ -26,6 +28,21 @@ func TestValidateURLRejectsUnsafeDestinations(t *testing.T) {
}
}
func TestValidateURLAcceptsPrivateNetworkMirror(t *testing.T) {
t.Parallel()
for _, rawURL := range []string{
"http://10.0.0.10/images/rootfs.tar.xz",
"http://172.16.20.30:8080/images/vm.qcow2",
"https://192.168.1.10/image.iso",
"http://100.64.0.10/image.qcow2",
"http://[fd00::10]/rootfs.tar.xz",
} {
if _, err := ValidateURL(rawURL); err != nil {
t.Errorf("ValidateURL(%q) returned error: %v", rawURL, err)
}
}
}
func TestValidateURLAcceptsPublicHTTPURL(t *testing.T) {
t.Parallel()
parsed, err := ValidateURL("https://example.com/images/rootfs.tar.xz?variant=default")
@@ -37,29 +54,33 @@ func TestValidateURLAcceptsPublicHTTPURL(t *testing.T) {
}
}
func TestIsPublicAddress(t *testing.T) {
func TestIsAllowedDownloadAddress(t *testing.T) {
t.Parallel()
tests := map[string]bool{
"8.8.8.8": true,
"1.1.1.1": true,
"2606:4700:4700::1111": true,
"127.0.0.1": false,
"10.0.0.1": false,
"100.64.0.1": false,
"10.0.0.1": true,
"100.64.0.1": true,
"172.16.0.1": true,
"192.168.1.1": true,
"169.254.169.254": false,
"100.100.100.200": false,
"192.0.2.1": false,
"198.18.0.1": false,
"::1": false,
"64:ff9b::127.0.0.1": false,
"2002:7f00:1::1": false,
"fc00::1": false,
"fc00::1": true,
"fd00:ec2::254": false,
"fec0::1": false,
"fe80::1": false,
"2001:db8::1": false,
}
for raw, expected := range tests {
if actual := isPublicAddress(netip.MustParseAddr(raw)); actual != expected {
t.Errorf("isPublicAddress(%s) = %v, want %v", raw, actual, expected)
if actual := isAllowedDownloadAddress(netip.MustParseAddr(raw)); actual != expected {
t.Errorf("isAllowedDownloadAddress(%s) = %v, want %v", raw, actual, expected)
}
}
}
@@ -72,3 +93,53 @@ func TestGetRejectsLoopbackBeforeRequest(t *testing.T) {
t.Fatal("Get accepted a loopback destination")
}
}
func TestGetAllowsPrivateNetworkMirror(t *testing.T) {
privateIP := privateInterfaceIPv4(t)
listener, err := net.Listen("tcp4", net.JoinHostPort(privateIP.String(), "0"))
if err != nil {
t.Fatalf("failed to listen on private interface: %v", err)
}
defer listener.Close()
server := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(w, "private mirror ok")
})}
go func() { _ = server.Serve(listener) }()
defer server.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
response, err := Get(ctx, "http://"+listener.Addr().String()+"/image", "test", 5*time.Second)
if err != nil {
t.Fatalf("Get rejected private mirror: %v", err)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != "private mirror ok" {
t.Fatalf("body = %q, want private mirror response", body)
}
}
func privateInterfaceIPv4(t *testing.T) netip.Addr {
t.Helper()
addresses, err := net.InterfaceAddrs()
if err != nil {
t.Fatal(err)
}
for _, rawAddress := range addresses {
prefix, err := netip.ParsePrefix(rawAddress.String())
if err != nil {
continue
}
address := prefix.Addr().Unmap()
if address.Is4() && address.IsPrivate() && isAllowedDownloadAddress(address) {
return address
}
}
t.Skip("no private IPv4 interface is available")
return netip.Addr{}
}
+1 -1
View File
@@ -1,7 +1,7 @@
package version
var (
Version = "1.1.28"
Version = "1.1.29"
Repo = "MengMengCode/CLICD"
)
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "clicd-frontend",
"private": true,
"version": "1.1.28",
"version": "1.1.29",
"type": "module",
"scripts": {
"dev": "vite",
+1 -1
View File
@@ -132,7 +132,7 @@ export default function Login() {
</form>
</div>
<p className="text-center text-xs text-gray-400 mt-6">CLICD v1.1.28</p>
<p className="text-center text-xs text-gray-400 mt-6">CLICD v1.1.29</p>
</div>
</div>
)