{"record":{"id":"5ea329f72c8e7cab","repo":"usebruno/bruno","slug":"variable-name-key-contains-invalid-character","errorCode":null,"errorMessage":"Variable name: \"${key}\" contains invalid characters! Names must only contain alpha-numeric characters, \"-\", \"_\", \".\"","messagePattern":"Variable name: \"(.+?)\" contains invalid characters! Names must only contain alpha-numeric characters, \"-\", \"_\", \"\\.\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/bruno-js/src/bru.js","lineNumber":204,"sourceCode":"  getProcessEnv(key) {\n    return this.processEnvVars[key];\n  }\n\n  hasEnvVar(key) {\n    return Object.hasOwn(this.envVariables, key);\n  }\n\n  getEnvVar(key) {\n    return this.interpolate(this.envVariables[key]);\n  }\n\n  setEnvVar(key, value) {\n    if (!key) {\n      throw new Error('Creating a env variable without specifying a name is not allowed.');\n    }\n\n    if (variableNameRegex.test(key) === false) {\n      throw new Error(\n        `Variable name: \"${key}\" contains invalid characters! Names must only contain alpha-numeric characters, \"-\", \"_\", \".\"`\n      );\n    }\n\n    // Deep-equal compare so object/array writes that mutate in place\n    // (e.g. `const c = bru.getEnvVar('cfg'); c.port = 4000; bru.setEnvVar('cfg', c);`)\n    // still flip the dirty flag — strict `!==` returned false for same-reference writes.\n    if (!Object.hasOwn(this.envVariables, key) || !isEqual(this.envVariables[key], value)) {\n      this.envVariables[key] = value;\n      this._envDirty = true;\n    }\n  }\n\n  deleteEnvVar(key) {\n    if (key === '__name__') return;\n    if (Object.hasOwn(this.envVariables, key)) {\n      delete this.envVariables[key];\n      this._envDirty = true;","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/usebruno/bruno/blob/9bdd81c7bdc57006e5f5ebffb79321a8d979f712/packages/bruno-js/src/bru.js#L186-L222","documentation":"Thrown by bru.setEnvVar when the key contains characters outside the allowed set. The regex /^\\w-.]*$/ at bru.js:8 permits only alphanumeric characters, underscores, hyphens, and dots. Keys with spaces, dollar signs, slashes, or other special characters are rejected at bru.js:203-207.","triggerScenarios":"Calling bru.setEnvVar('my var', value) (space), bru.setEnvVar('$ref', value) (dollar sign), bru.setEnvVar('a/b', value) (slash), or bru.setEnvVar('key:val', value) (colon). Any character outside [A-Za-z0-9_-.] triggers the error.","commonSituations":"Using a header name or URL path segment as a variable key (may contain colons or slashes). Using template-literal interpolation that introduces spaces. Copying a variable name from a system that allows special characters.","solutions":["Rename the key to use only alphanumeric characters, underscores, hyphens, and dots (e.g., 'myVar', 'my-var', 'my.var').","Sanitize the key before calling setEnvVar: key = key.replace(/[^\\w.-]/g, '_').","Avoid using request-derived strings (URLs, headers) directly as variable names."],"exampleFix":"// before\nbru.setEnvVar('Content-Type', 'application/json'); // hyphen is ok but space after colon\nbru.setEnvVar('user id', 42); // space not allowed\n\n// after\nbru.setEnvVar('Content-Type', 'application/json'); // valid: hyphen allowed\nbru.setEnvVar('user_id', 42); // use underscore instead of space","handlingStrategy":"validation","validationCode":"const variableNameRegex = /^[\\w-.]*$/;\nfunction isValidVarName(key) {\n  return typeof key === 'string' && key.length > 0 && variableNameRegex.test(key);\n}\n// before calling: if (isValidVarName(key)) bru.setEnvVar(key, value);","typeGuard":null,"tryCatchPattern":"try {\n  bru.setEnvVar(key, value);\n} catch (e) {\n  if (e.message.includes('invalid characters')) {\n    const safeKey = key.replace(/[^\\w.-]/g, '_');\n    bru.setEnvVar(safeKey, value);\n  }\n}","preventionTips":["Use only alphanumeric characters, underscores, hyphens, and dots in variable names.","Sanitize dynamically derived keys with key.replace(/[^\\w.-]/g, '_').","Avoid using request/response field names directly as variable keys without validation."],"tags":["env-var","validation","regex","setenvvar","invalid-characters"],"backgroundTag":null,"analyzedSha":"9bdd81c7bdc57006e5f5ebffb79321a8d979f712","analyzedAt":"2026-08-13T04:09:25.751Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}