```typescript
/** The KEY=VALUE pairs of a config file. */
export function parseConfig(text: string): Record<string, string> {
  const config: Record<string, string> = {};
  for (const line of text.split("\n")) {
    // Trim leading and trailing whitespace from the line
    let trimmedLine = line.trim();
    
    // Skip empty lines or lines starting with a comment
    if (!trimmedLine || trimmedLine.startsWith("#")) continue;

    // Split the line on the first '=' character
    const [key, value] = trimmedLine.split("=", 2);

    // Validate key and value
    if (!key) {
      throw new Error(`Invalid config file: empty key at line ${lineNumber}`);
    }
    if (value === undefined || value.trim() === "") {
      throw new Error(`Invalid config file: no value for key "${key}" at line ${lineNumber}`);
    }

    // Ensure the key does not reach Object.prototype
    if (key === "__proto__") {
      throw new Error(`Invalid config file: cannot set "__proto__" as a key`);
    }

    // Store the parsed key-value pair
    config[key] = value.trim();
  }
  return config;
}
```