fix: block VM creation/save with invalid CloudInit YAML (#1040)

* fix(vm): block VM creation with invalid CloudInit YAML content

* fix(vm): add isWindows guard to validateCloudInit in template version

* fix: validate Windows CloudInit YAML on save

Co-authored-by: a110605 <5744158+a110605@users.noreply.github.com>

* fix: validate cloud-init by format in the VM mixin

Cloud-init picks the user data format from the first line, so scripts,
MIME messages and Jinja templates are valid without being YAML. Parsing
all user data blocked them, and it stopped any existing VM holding one
from being saved, even with nothing changed.

Only parse user data that starts with #cloud-config, and require a
mapping for it and for network data, because plain words are valid YAML
and were still saved.

Both forms already use the VM mixin, so the check moves there and shares
the header pattern with hasCloudConfigComment.

Signed-off-by: Marcelo Fukumoto <marcelo.fukumoto@suse.com>

* fix: save other user data formats as typed and check text without a header

The form added #cloud-config to any user data that parses as YAML, so a
script was saved with the header and its lines folded together, and Edit
Config then failed the check on it. User data that starts with a script,
MIME or another cloud-init or Cloudbase-Init format is now saved as typed,
and any other text is checked as cloud-config, with or without the header,
because the form adds the header when saving it.

Editing only checks cloud-init that changed, so a VM saved before this
change can still be edited with the cloud-init it already has.

Signed-off-by: Marcelo Fukumoto <marcelo.fukumoto@suse.com>

---------

Signed-off-by: Marcelo Fukumoto <marcelo.fukumoto@suse.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: a110605 <5744158+a110605@users.noreply.github.com>
Co-authored-by: Marcelo Fukumoto <marcelo.fukumoto@suse.com>
(cherry picked from commit aa6d11a840)
This commit is contained in:
Copilot
2026-11-09 16:00:19 +00:00
committed by Mergify
parent 3322162d9a
commit 136991ba26
8 changed files with 272 additions and 1 deletions
+35 -1
View File
@@ -2,6 +2,8 @@ import YAML from 'yaml';
import jsyaml from 'js-yaml';
import isEqual from 'lodash/isEqual';
import isEmpty from 'lodash/isEmpty';
import isNil from 'lodash/isNil';
import isPlainObject from 'lodash/isPlainObject';
import difference from 'lodash/difference';
import { sortBy } from '@shell/utils/sort';
import { set } from '@shell/utils/object';
@@ -23,7 +25,7 @@ import {
import { HCI_SETTING } from '../../config/settings';
import { HCI } from '../../types';
import { parseVolumeClaimTemplates, EMPTY_IMAGE } from '../../utils/vm';
import impl, { QGA_JSON, USB_TABLET } from './impl';
import impl, { isCloudConfig, QGA_JSON, USB_TABLET } from './impl';
import { GIBIBYTE } from '../../utils/unit';
import { VOLUME_MODE, ACCESS_MODE, FILESYSTEM_SOURCE_TYPE } from '@pkg/harvester/config/types';
@@ -157,6 +159,8 @@ export default {
installUSBTablet: true,
networkScript: '',
userScript: '',
// The cloud-init the form loaded, so editing a VM never blocks saving it unchanged.
savedCloudInit: {},
imageId: '',
diskRows: [],
networkRows: [],
@@ -434,6 +438,7 @@ export default {
this['accessCredentials'] = accessCredentials;
this['userScript'] = userData;
this['networkScript'] = networkData;
this['savedCloudInit'] = { userScript: userData, networkScript: networkData };
this['sshKey'] = sshKey;
this['osType'] = osType;
@@ -1283,6 +1288,11 @@ export default {
* @param config
*/
getUserData(config) {
// Scripts, MIME and the other formats are saved as typed: only cloud-config gets the SSH keys, the guest agent and the header.
if (!isCloudConfig(this.userScript)) {
return this.userScript;
}
try {
// https://github.com/eemeli/yaml/issues/136
let userDataDoc = this.userScript ? YAML.parseDocument(this.userScript) : YAML.parseDocument({});
@@ -1475,6 +1485,30 @@ export default {
this.networkScript = value;
},
validateCloudInit() {
const isYamlMapping = (text) => {
try {
const doc = jsyaml.load(text);
return isNil(doc) || isPlainObject(doc);
} catch (e) {
return false;
}
};
// Editing only checks what changed, so a VM can still be saved with the cloud-init it already has.
const isChanged = (key) => !this.isEdit || this[key] !== this.savedCloudInit[key];
// Scripts, MIME and the other formats are valid without being YAML. Anything else is saved as cloud-config.
if (this.userScript && isChanged('userScript') && isCloudConfig(this.userScript) && !isYamlMapping(this.userScript)) {
this.errors.push(this.t('harvester.virtualMachine.cloudConfig.user.invalidYaml'));
}
if (this.networkScript && isChanged('networkScript') && !isYamlMapping(this.networkScript)) {
this.errors.push(this.t('harvester.virtualMachine.cloudConfig.network.invalidYaml'));
}
},
mergeSSHAuthorizedKeys(yaml) {
try {
const sshAuthorizedKeys = YAML.parseDocument(yaml)