Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

8.5 KB
Raw Blame
// Guards the guard. The point of validate-no-real-environments.js is to fail when a
// real environment identifier is committed to this PUBLIC repository, so the tests
// that matter most are the ones asserting it does NOT stay silent on real inputs.
// Every "real" case below is an identifier that was genuinely committed to this repo
// and removed, or a shape the Dataverse service actually generates.

const test = require('node:test');
const assert = require('node:assert');

const {
  isPlaceholder,
  isPlaceholderEmailDomain,
  scanText,
} = require('../validate-no-real-environments.js');

test('placeholder subdomains are accepted', () => {
  for (const value of [
    'contoso',
    'contoso-dev',
    'contosobapenv0001',
    'fabrikamenv001',
    'example',
    'test',
    'your-env',
    'org',
    'org1',
    'ORG2',
    'other',
    'x',
  ]) {
    assert.equal(isPlaceholder(value), true, `${value} should be treated as a placeholder`);
  }
});

test('real environment names are rejected', () => {
  for (const value of [
    // Realistic non-placeholder names (invented, not values that were ever committed here).
    'zenithbapenv77c1',
    'zenithbapenv22d4',
    'qtxbapenv31a9',
    // Shapes seen elsewhere in the repo / generated by Dataverse.
    'org1e98cc97',
    'pascalepipelineshost',
    '1841communityv2fresh',
  ]) {
    assert.equal(isPlaceholder(value), false, `${value} should be rejected`);
  }
});

test('the org<8hex> shape is rejected even though it starts with the allowed word "org"', () => {
  // Regression guard for the ordering inside isPlaceholder: the hex-shape check must
  // run BEFORE the placeholder-root and length checks, otherwise a real auto-generated
  // Dataverse org is rescued by the generic "org" allowance.
  assert.equal(isPlaceholder('org1e98cc97'), false);
  assert.equal(isPlaceholder('orgc4f78248'), false);
  // ...while short generic org stand-ins still pass.
  assert.equal(isPlaceholder('org123'), true);
});

test('a real Dataverse host in text is reported', () => {
  const violations = scanText('- URL: https://zenithbapenv77c1.crmtest.dynamics.com/');
  assert.ok(violations.length > 0, 'expected at least one violation');
  assert.ok(
    violations.some((v) => /non-placeholder Dataverse host/.test(v.detail)),
    `expected a non-placeholder violation, got ${JSON.stringify(violations)}`,
  );
});

test('a real tenant in a UPN is reported', () => {
  const violations = scanText('user: [email protected]');
  assert.ok(
    violations.some((v) => /non-placeholder tenant/.test(v.detail)),
    `expected a non-placeholder violation, got ${JSON.stringify(violations)}`,
  );
});

test('a previously-unseen real org host is reported by shape alone', () => {
  // Caught by the shape rule, which is now the only mechanism — there is no denylist.
  // rule. Without that rule the guard would only ever re-catch yesterday's leak.
  const violations = scanText('envUrl: https://org4a2942d9.crm17.dynamics.com');
  assert.equal(violations.length, 1, JSON.stringify(violations));
  assert.match(violations[0].detail, /non-placeholder Dataverse host/);
});

test('placeholder-only content produces no violations', () => {
  const clean = [
    'https://contoso.crm.dynamics.com',
    '[email protected]',
    'https://contosobapenv0001.crmtest.dynamics.com/',
    '[email protected]',
    'https://org.crm.dynamics.com',
  ].join('\n');
  assert.deepEqual(scanText(clean), []);
});

test('violations carry the 1-based line number', () => {
  const violations = scanText(['clean line', '', 'https://org9cf0ed45.crm.dynamics.com'].join('\n'));
  assert.equal(violations.length, 1);
  assert.equal(violations[0].line, 3);
});

test('CRLF content is scanned with correct line numbers', () => {
  // Plugin scripts are CRLF while eval fixtures are LF; a split on "\n" alone would
  // leave a trailing "\r" and could shift or mangle reported lines.
  const violations = scanText('clean\r\nhttps://org5fbe4359.crm5.dynamics.com\r\n');
  assert.equal(violations.length, 1);
  assert.equal(violations[0].line, 2);
});

test('a real environment whose name STARTS with a generic word is rejected', () => {
  // Review finding: PLACEHOLDER_ROOTS is matched as a prefix, so generic English words like
  // `test`/`demo`/`sample` waved through an unbounded family of names — and environments genuinely
  // called `TestEnv01` or `demo-prod-01` are common. A false negative here is a leak, which is the
  // failure direction that matters, so those roots were removed.
  for (const value of [
    'testenv12345',
    'TestEnv01',
    'demo-prod-01',
    'demoorg9931',
    'sampleorg99',
    'my-real-tenant',
    'samplecorp-prod',
  ]) {
    assert.equal(isPlaceholder(value), false, `${value} must NOT be treated as a placeholder`);
  }
});

test('bare generic stand-ins still pass, via the length rule rather than a prefix', () => {
  // These are what the in-tree fixtures actually use; they are short enough that the length ceiling
  // covers them without needing a prefix entry that would also cover `testenv12345`.
  for (const value of ['test', 'demo', 'dev', 'stg', 'uat', 'x']) {
    assert.equal(isPlaceholder(value), true, `${value} should still be accepted`);
  }
});

test('the fictional brands still match as prefixes', () => {
  // Contoso/Fabrikam are Microsoft's documented sample organizations, so a name built on them reads
  // as obviously fake even when long.
  for (const value of ['contosobapenv0001', 'fabrikamtenant01', 'exampleorg', 'your-env']) {
    assert.equal(isPlaceholder(value), true, `${value} should be accepted`);
  }
});

test('a real-looking tenant beginning with a generic word is reported', () => {
  const violations = scanText('user: [email protected]');
  assert.equal(violations.length, 1, JSON.stringify(violations));
  assert.match(violations[0].detail, /non-placeholder tenant/);
});

// -- e-mail rule ---------------------------------------------------------------------------------
// Added after review found a real employee work UPN surviving in three committed fixtures. A
// hostname-only scrub misses it precisely because an address does not look like infrastructure.

test('a real employee e-mail domain is reported', () => {
  const violations = scanText('[1] UNIVERSAL [email protected] Public OperatingSystem');
  assert.equal(violations.length, 1, JSON.stringify(violations));
  assert.match(violations[0].detail, /real-looking e-mail domain/);
});

test('placeholder e-mail domains are accepted', () => {
  for (const line of [
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]',
  ]) {
    assert.deepEqual(scanText(line), [], line);
  }
});

test('OData annotations are not mistaken for e-mail addresses', () => {
  // These parse as addresses but are payload syntax; flagging them would make the guard unusable
  // against the eval fixtures, which are full of them.
  const odata = [
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '_ownerid_value@Microsoft.Dynamics.CRM.associatednavigationproperty',
  ].join('\n');
  assert.deepEqual(scanText(odata), []);
  // Only the exact annotation names: a real-looking domain that merely begins with the spelling is an address.
  for (const address of ['[email protected]', '[email protected]',
    '[email protected]', '[email protected]', '[email protected]']) {
    assert.equal(scanText(address).length, 1, address);
  }
  assert.equal(isPlaceholderEmailDomain('microsoft.dynamics.crm.example.net'), false);
  assert.equal(isPlaceholderEmailDomain('Microsoft.Dynamics.CRM.lookuplogicalname'), true);
  assert.equal(isPlaceholderEmailDomain('odata.nextLink'), true);
});

test('isPlaceholderEmailDomain: real vs illustrative', () => {
  assert.equal(isPlaceholderEmailDomain('microsoft.com'), false);
  assert.equal(isPlaceholderEmailDomain('gmail.com'), false);
  assert.equal(isPlaceholderEmailDomain('contoso.com'), true);
  assert.equal(isPlaceholderEmailDomain('contosotest1.onmicrosoft.com'), true);
  assert.equal(isPlaceholderEmailDomain('odata.bind'), true);
});