diff --git a/packages/dns/googledns/src/index.ts b/packages/dns/googledns/src/index.ts index dbb4c535..a54e5be0 100644 --- a/packages/dns/googledns/src/index.ts +++ b/packages/dns/googledns/src/index.ts @@ -123,14 +123,20 @@ export default defineDns({ const token = await getAccessToken(); const project = config.projectId ?? _secret('GOOGLE_PROJECT_ID'); if (!project) throw new Error('GOOGLE_PROJECT_ID not set'); - const [type, name] = recordId.split('/'); + const separatorIndex = recordId.indexOf('/'); + if (separatorIndex <= 0 || separatorIndex === recordId.length - 1) { + throw new Error(`Google Cloud DNS deleteRecord: invalid record id "${recordId}"`); + } + const type = recordId.slice(0, separatorIndex); + const name = recordId.slice(separatorIndex + 1); // Need to fetch the rrset to get current rrdatas for the deletion entry. const existing = (await this.listRecords(zoneId, config)).filter( r => r.type === type && (r.name === name || r.name === name.replace(/\.$/, '')), ); - if (existing.length === 0) return; + const firstRecord = existing[0]; + if (!firstRecord) return; const fqdn = name.endsWith('.') ? name : `${name}.`; - const ttl = existing[0].ttl; + const ttl = firstRecord.ttl; const res = await fetch(`${API}/projects/${project}/managedZones/${zoneId}/changes`, { method: 'POST', headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },