Skip to content

Commit

Permalink
refactor(ses): getenv detects more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Jan 13, 2025
1 parent e02b0f6 commit 573a6d7
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 55 deletions.
4 changes: 0 additions & 4 deletions packages/ses/src/error/tame-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export const tameConsole = (
unhandledRejectionTrapping = 'report',
optGetStackString = undefined,
) => {
consoleTaming === 'safe' ||
consoleTaming === 'unsafe' ||
failFast(`unrecognized consoleTaming ${consoleTaming}`);

let loggedErrorHandler;
if (optGetStackString === undefined) {
loggedErrorHandler = defaultHandler;
Expand Down
11 changes: 0 additions & 11 deletions packages/ses/src/error/tame-error-constructor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
FERAL_ERROR,
TypeError,
apply,
construct,
defineProperties,
Expand Down Expand Up @@ -36,16 +35,6 @@ export default function tameErrorConstructor(
errorTaming = 'safe',
stackFiltering = 'concise',
) {
if (
errorTaming !== 'safe' &&
errorTaming !== 'unsafe' &&
errorTaming !== 'unsafe-debug'
) {
throw TypeError(`unrecognized errorTaming ${errorTaming}`);
}
if (stackFiltering !== 'concise' && stackFiltering !== 'verbose') {
throw TypeError(`unrecognized stackFiltering ${stackFiltering}`);
}
const ErrorPrototype = FERAL_ERROR.prototype;

const { captureStackTrace: originalCaptureStackTrace } = FERAL_ERROR;
Expand Down
45 changes: 24 additions & 21 deletions packages/ses/src/lockdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,39 @@ export const repairIntrinsics = (options = {}) => {
// for an explanation.

const {
errorTaming = getenv('LOCKDOWN_ERROR_TAMING', 'safe'),
errorTaming = getenv('LOCKDOWN_ERROR_TAMING', 'safe', [
'unsafe',
'unsafe-debug',
]),
errorTrapping = /** @type {"platform" | "none" | "report" | "abort" | "exit"} */ (
getenv('LOCKDOWN_ERROR_TRAPPING', 'platform')
getenv('LOCKDOWN_ERROR_TRAPPING', 'platform', [
'none',
'report',
'abort',
'exit',
])
),
reporting = /** @type {"platform" | "console" | "none"} */ (
getenv('LOCKDOWN_REPORTING', 'platform')
getenv('LOCKDOWN_REPORTING', 'platform', ['console', 'none'])
),
unhandledRejectionTrapping = /** @type {"none" | "report"} */ (
getenv('LOCKDOWN_UNHANDLED_REJECTION_TRAPPING', 'report')
getenv('LOCKDOWN_UNHANDLED_REJECTION_TRAPPING', 'report', ['none'])
),
regExpTaming = getenv('LOCKDOWN_REGEXP_TAMING', 'safe'),
localeTaming = getenv('LOCKDOWN_LOCALE_TAMING', 'safe'),
regExpTaming = getenv('LOCKDOWN_REGEXP_TAMING', 'safe', ['unsafe']),
localeTaming = getenv('LOCKDOWN_LOCALE_TAMING', 'safe', ['unsafe']),

consoleTaming = /** @type {'unsafe' | 'safe'} */ (
getenv('LOCKDOWN_CONSOLE_TAMING', 'safe')
getenv('LOCKDOWN_CONSOLE_TAMING', 'safe', ['unsafe'])
),
overrideTaming = /** @type {'moderate' | 'min' | 'severe'} */ (
getenv('LOCKDOWN_OVERRIDE_TAMING', 'moderate')
getenv('LOCKDOWN_OVERRIDE_TAMING', 'moderate', ['min', 'severe'])
),
stackFiltering = getenv('LOCKDOWN_STACK_FILTERING', 'concise'),
domainTaming = getenv('LOCKDOWN_DOMAIN_TAMING', 'safe'),
evalTaming = getenv('LOCKDOWN_EVAL_TAMING', 'safeEval'),
stackFiltering = getenv('LOCKDOWN_STACK_FILTERING', 'concise', ['verbose']),
domainTaming = getenv('LOCKDOWN_DOMAIN_TAMING', 'safe', ['unsafe']),
evalTaming = getenv('LOCKDOWN_EVAL_TAMING', 'safeEval', [
'unsafeEval',
'noEval',
]),
overrideDebug = arrayFilter(
stringSplit(getenv('LOCKDOWN_OVERRIDE_DEBUG', ''), ','),
/** @param {string} debugName */
Expand All @@ -192,22 +203,14 @@ export const repairIntrinsics = (options = {}) => {
legacyRegeneratorRuntimeTaming = getenv(
'LOCKDOWN_LEGACY_REGENERATOR_RUNTIME_TAMING',
'safe',
['unsafe-ignore'],
),
__hardenTaming__ = getenv('LOCKDOWN_HARDEN_TAMING', 'safe'),
__hardenTaming__ = getenv('LOCKDOWN_HARDEN_TAMING', 'safe', ['unsafe']),
dateTaming, // deprecated
mathTaming, // deprecated
...extraOptions
} = options;

legacyRegeneratorRuntimeTaming === 'safe' ||
legacyRegeneratorRuntimeTaming === 'unsafe-ignore' ||
Fail`lockdown(): non supported option legacyRegeneratorRuntimeTaming: ${q(legacyRegeneratorRuntimeTaming)}`;

evalTaming === 'unsafeEval' ||
evalTaming === 'safeEval' ||
evalTaming === 'noEval' ||
Fail`lockdown(): non supported option evalTaming: ${q(evalTaming)}`;

// Assert that only supported options were passed.
// Use Reflect.ownKeys to reject symbol-named properties as well.
const extraOptionsNames = ownKeys(extraOptions);
Expand Down
5 changes: 1 addition & 4 deletions packages/ses/src/reporting.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TypeError, functionBind, globalThis } from './commons.js';
import { functionBind, globalThis } from './commons.js';
import { assert } from './error/assert.js';

/**
Expand Down Expand Up @@ -51,9 +51,6 @@ export const chooseReporter = reporting => {
if (reporting === 'none') {
return makeReportPrinter(mute);
}
if (reporting !== 'platform' && reporting !== 'console') {
throw new TypeError(`Invalid lockdown reporting option: ${reporting}`);
}
if (
reporting === 'console' ||
globalThis.window === globalThis ||
Expand Down
4 changes: 0 additions & 4 deletions packages/ses/src/tame-domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import {
} from './commons.js';

export function tameDomains(domainTaming = 'safe') {
if (domainTaming !== 'safe' && domainTaming !== 'unsafe') {
throw TypeError(`unrecognized domainTaming ${domainTaming}`);
}

if (domainTaming === 'unsafe') {
return;
}
Expand Down
6 changes: 1 addition & 5 deletions packages/ses/src/tame-harden.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
/* eslint-disable no-restricted-globals */
import { TypeError, freeze } from './commons.js';
import { freeze } from './commons.js';

/** @import {Harden} from '../types.js'; */

/** @type {(safeHarden: Harden, hardenTaming: 'safe' | 'unsafe') => Harden} */
export const tameHarden = (safeHarden, hardenTaming) => {
if (hardenTaming !== 'safe' && hardenTaming !== 'unsafe') {
throw TypeError(`unrecognized fakeHardenOption ${hardenTaming}`);
}

if (hardenTaming === 'safe') {
return safeHarden;
}
Expand Down
3 changes: 0 additions & 3 deletions packages/ses/src/tame-locale-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ const nonLocaleCompare = tamedMethods.localeCompare;
const numberToString = tamedMethods.toString;

export default function tameLocaleMethods(intrinsics, localeTaming = 'safe') {
if (localeTaming !== 'safe' && localeTaming !== 'unsafe') {
throw TypeError(`unrecognized localeTaming ${localeTaming}`);
}
if (localeTaming === 'unsafe') {
return;
}
Expand Down
3 changes: 0 additions & 3 deletions packages/ses/src/tame-regexp-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import {
} from './commons.js';

export default function tameRegExpConstructor(regExpTaming = 'safe') {
if (regExpTaming !== 'safe' && regExpTaming !== 'unsafe') {
throw TypeError(`unrecognized regExpTaming ${regExpTaming}`);
}
const RegExpPrototype = FERAL_REG_EXP.prototype;

const makeRegExpConstructor = (_ = {}) => {
Expand Down

0 comments on commit 573a6d7

Please sign in to comment.