|
| 1 | +// First of all, create a controllable clock object that underlies the |
| 2 | +// functions in the Temporal.now namespace, that we can tick forward or backward |
| 3 | +// at will. |
| 4 | +// We'll use the clock to remove Temporal's access to the system clock below. |
| 5 | + |
| 6 | +class Clock { |
| 7 | + epochNs = 0n; |
| 8 | + tick(ticks = 1) { |
| 9 | + this.epochNs += BigInt(ticks); |
| 10 | + } |
| 11 | +} |
| 12 | +const clock = new Clock(); |
| 13 | + |
| 14 | +// Save the original Temporal functions that we will override but still need |
| 15 | +// access to internally. |
| 16 | + |
| 17 | +const realTemporalCalendar = Temporal.Calendar; |
| 18 | +const realCalendarFrom = Temporal.Calendar.from; |
| 19 | +const realTemporalTimeZone = Temporal.TimeZone; |
| 20 | +const realTimeZoneFrom = Temporal.TimeZone.from; |
| 21 | +const realTemporalNow = Temporal.now; |
| 22 | + |
| 23 | +// Override the Temporal.Calendar constructor and Temporal.Calendar.from to |
| 24 | +// disallow all calendars except the iso8601 calendar, otherwise insecure code |
| 25 | +// might be able to tell something about the version of the host system's |
| 26 | +// locale data. |
| 27 | + |
| 28 | +class Calendar extends realTemporalCalendar { |
| 29 | + constructor(identifier) { |
| 30 | + if (identifier !== 'iso8601') { |
| 31 | + // match error message |
| 32 | + throw new RangeError(`Invalid calendar: ${identifier}`); |
| 33 | + } |
| 34 | + super(identifier); |
| 35 | + } |
| 36 | + |
| 37 | + static from(item) { |
| 38 | + const calendar = realCalendarFrom.call(realTemporalCalendar, item); |
| 39 | + const identifier = calendar.toString(); |
| 40 | + const constructor = Object.is(this, realTemporalCalendar) ? Calendar : this; |
| 41 | + return new constructor(identifier); |
| 42 | + } |
| 43 | +} |
| 44 | +Object.getOwnPropertyNames(realTemporalCalendar.prototype).forEach((name) => { |
| 45 | + if (name === 'constructor') return; |
| 46 | + const desc = Object.getOwnPropertyDescriptor(realTemporalCalendar.prototype, name); |
| 47 | + Object.defineProperty(Calendar.prototype, name, desc); |
| 48 | +}); |
| 49 | + |
| 50 | +// Do the same for the Temporal.TimeZone constructor and Temporal.TimeZone.from |
| 51 | +// to allow only offset time zones and the various aliases for UTC, otherwise |
| 52 | +// insecure code might be able to tell something about the version of the host |
| 53 | +// system's time zone database. |
| 54 | + |
| 55 | +class TimeZone extends realTemporalTimeZone { |
| 56 | + constructor(identifier) { |
| 57 | + const matchOffset = /^[+\u2212-][0-2][0-9](?::?[0-5][0-9](?::?[0-5][0-9](?:[.,]\d{1,9})?)?)?$/; |
| 58 | + const matchUTC = /^UTC|Etc\/UTC|Etc\/GMT(?:[-+]\d{1,2})?$/; |
| 59 | + if (!matchUTC.test(identifier) && !matchOffset.test(identifier)) { |
| 60 | + // match error message |
| 61 | + throw new RangeError(`Invalid time zone specified: ${identifier}`); |
| 62 | + } |
| 63 | + super(identifier); |
| 64 | + } |
| 65 | + |
| 66 | + static from(item) { |
| 67 | + const timeZone = realTimeZoneFrom.call(realTemporalTimeZone, item); |
| 68 | + const identifier = timeZone.toString(); |
| 69 | + const constructor = Object.is(this, realTemporalTimeZone) ? TimeZone : this; |
| 70 | + return new constructor(identifier); |
| 71 | + } |
| 72 | +} |
| 73 | +Object.getOwnPropertyNames(realTemporalTimeZone.prototype).forEach((name) => { |
| 74 | + if (name === 'constructor') return; |
| 75 | + const desc = Object.getOwnPropertyDescriptor(realTemporalTimeZone.prototype, name); |
| 76 | + Object.defineProperty(TimeZone.prototype, name, desc); |
| 77 | +}); |
| 78 | + |
| 79 | +// Override the functions in the Temporal.now namespace using our patched clock, |
| 80 | +// calendar, and time zone. |
| 81 | + |
| 82 | +function instant() { |
| 83 | + return new Temporal.Instant(clock.epochNs); |
| 84 | +} |
| 85 | + |
| 86 | +function plainDateTime(calendarLike, temporalTimeZoneLike = timeZone()) { |
| 87 | + const timeZone = TimeZone.from(temporalTimeZoneLike); |
| 88 | + const calendar = Calendar.from(calendarLike); |
| 89 | + const inst = instant(); |
| 90 | + return timeZone.getPlainDateTimeFor(inst, calendar); |
| 91 | +} |
| 92 | + |
| 93 | +function plainDateTimeISO(temporalTimeZoneLike = timeZone()) { |
| 94 | + const timeZone = TimeZone.from(temporalTimeZoneLike); |
| 95 | + const calendar = new Calendar('iso8601'); |
| 96 | + const inst = instant(); |
| 97 | + return timeZone.getPlainDateTimeFor(inst, calendar); |
| 98 | +} |
| 99 | + |
| 100 | +function zonedDateTime(calendarLike, temporalTimeZoneLike = timeZone()) { |
| 101 | + const timeZone = TimeZone.from(temporalTimeZoneLike); |
| 102 | + const calendar = Calendar.from(calendarLike); |
| 103 | + return new Temporal.ZonedDateTime(clock.epochNs, timeZone, calendar); |
| 104 | +} |
| 105 | + |
| 106 | +function zonedDateTimeISO(temporalTimeZoneLike = timeZone()) { |
| 107 | + const timeZone = TimeZone.from(temporalTimeZoneLike); |
| 108 | + const calendar = new Calendar('iso8601'); |
| 109 | + return new Temporal.ZonedDateTime(clock.epochNs, timeZone, calendar); |
| 110 | +} |
| 111 | + |
| 112 | +function plainDate(calendarLike, temporalTimeZoneLike = timeZone()) { |
| 113 | + const pdt = plainDateTime(calendarLike, temporalTimeZoneLike); |
| 114 | + const f = pdt.getISOFields(); |
| 115 | + return new Temporal.PlainDate(f.isoYear, f.isoMonth, f.isoDay, f.calendar); |
| 116 | +} |
| 117 | + |
| 118 | +function plainDateISO(temporalTimeZoneLike = timeZone()) { |
| 119 | + const pdt = plainDateTimeISO(temporalTimeZoneLike); |
| 120 | + const f = pdt.getISOFields(); |
| 121 | + return new Temporal.PlainDate(f.isoYear, f.isoMonth, f.isoDay, f.calendar); |
| 122 | +} |
| 123 | + |
| 124 | +function plainTimeISO(temporalTimeZoneLike = timeZone()) { |
| 125 | + const pdt = plainDateTimeISO(temporalTimeZoneLike); |
| 126 | + const f = pdt.getISOFields(); |
| 127 | + return new Temporal.PlainTime( |
| 128 | + f.isoHour, |
| 129 | + f.isoMinute, |
| 130 | + f.isoSecond, |
| 131 | + f.isoMillisecond, |
| 132 | + f.isoMicrosecond, |
| 133 | + f.isoNanosecond |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +function timeZone() { |
| 138 | + return new TimeZone('UTC'); |
| 139 | +} |
| 140 | + |
| 141 | +// We now have everything we need to lock down Temporal, but if we want the |
| 142 | +// insecure code to run in an indistinguishable environment from an unlocked |
| 143 | +// Temporal, then we have to do a few more things, such as make sure that |
| 144 | +// toString() gives the same result for the patched functions as it would for |
| 145 | +// the original functions. |
| 146 | + |
| 147 | +// This example code is not exhaustive, but this is a sample of the concerns |
| 148 | +// that a secure environment would have to address. |
| 149 | + |
| 150 | +const realFunctionToString = Function.prototype.toString; |
| 151 | +const functionToString = function toString() { |
| 152 | + const patchedFunctions = new Map([ |
| 153 | + [Calendar, realTemporalCalendar], |
| 154 | + [Calendar.from, realCalendarFrom], |
| 155 | + [instant, realTemporalNow.instant], |
| 156 | + [plainDate, realTemporalNow.plainDate], |
| 157 | + [plainDateISO, realTemporalNow.plainDateISO], |
| 158 | + [plainDateTime, realTemporalNow.plainDateTime], |
| 159 | + [plainDateTimeISO, realTemporalNow.plainDateTimeISO], |
| 160 | + [plainTimeISO, realTemporalNow.plainTimeISO], |
| 161 | + [timeZone, realTemporalNow.timeZone], |
| 162 | + [TimeZone, realTemporalTimeZone], |
| 163 | + [TimeZone.from, realTimeZoneFrom], |
| 164 | + [toString, realFunctionToString], |
| 165 | + [zonedDateTime, realTemporalNow.zonedDateTime], |
| 166 | + [zonedDateTimeISO, realTemporalNow.zonedDateTimeISO] |
| 167 | + ]); |
| 168 | + if (patchedFunctions.has(this)) { |
| 169 | + return realFunctionToString.apply(patchedFunctions.get(this), arguments); |
| 170 | + } |
| 171 | + return realFunctionToString.apply(this, arguments); |
| 172 | +}; |
| 173 | + |
| 174 | +// Finally, freeze the Temporal object and all of its properties. |
| 175 | +// (Because this is done before any user code runs, we can use Temporal APIs in |
| 176 | +// the functions above. Otherwise we'd need to save the original APIs in case |
| 177 | +// user code overrode them.) |
| 178 | + |
| 179 | +function deepFreeze(object, path) { |
| 180 | + Object.getOwnPropertyNames(object).forEach((name) => { |
| 181 | + // Avoid .prototype.constructor endless loop |
| 182 | + if (name === 'constructor') return; |
| 183 | + |
| 184 | + const desc = Object.getOwnPropertyDescriptor(object, name); |
| 185 | + |
| 186 | + if (desc.value) { |
| 187 | + const value = desc.value; |
| 188 | + if (typeof value === 'object' || typeof value === 'function') { |
| 189 | + deepFreeze(value, `${path}.${name}`); |
| 190 | + } |
| 191 | + } |
| 192 | + if (desc.get) { |
| 193 | + deepFreeze(desc.get, `${path}.get ${name}`); |
| 194 | + } |
| 195 | + if (desc.set) { |
| 196 | + deepFreeze(desc.set, `${path}.set ${name}`); |
| 197 | + } |
| 198 | + }); |
| 199 | + |
| 200 | + return Object.freeze(object); |
| 201 | +} |
| 202 | + |
| 203 | +// This is the function that does the actual patching to lock down Temporal. It |
| 204 | +// must run before any user code does. |
| 205 | + |
| 206 | +function makeMockTemporal() { |
| 207 | + realTemporalTimeZone.from = TimeZone.from; |
| 208 | + realTemporalCalendar.from = Calendar.from; |
| 209 | + Temporal.Calendar = Calendar; |
| 210 | + Temporal.TimeZone = TimeZone; |
| 211 | + Temporal.now = { |
| 212 | + instant, |
| 213 | + plainDateTime, |
| 214 | + plainDateTimeISO, |
| 215 | + plainDate, |
| 216 | + plainDateISO, |
| 217 | + plainTimeISO, |
| 218 | + timeZone, |
| 219 | + zonedDateTime, |
| 220 | + zonedDateTimeISO |
| 221 | + }; |
| 222 | + deepFreeze(Temporal, 'Temporal'); |
| 223 | + Function.prototype.toString = functionToString; |
| 224 | +} |
| 225 | + |
| 226 | +// Check that we cannot distinguish the mock Temporal from the real one by |
| 227 | +// looking at some metadata; save the original metadata for later |
| 228 | +const realTemporalNowPlainDateToString = Temporal.now.plainDate.toString(); |
| 229 | +const realTemporalNowPlainDateOwnProperties = Object.getOwnPropertyDescriptors(Temporal.now.plainDate); |
| 230 | + |
| 231 | +// After this call, Temporal is locked down. |
| 232 | +makeMockTemporal(); |
| 233 | + |
| 234 | +// The clock starts at midnight UTC January 1, 1970, and is advanced manually. |
| 235 | +assert.equal(Temporal.now.instant().toString(), '1970-01-01T00:00:00Z'); |
| 236 | +clock.tick(1_000_000_000n); |
| 237 | +assert.equal(Temporal.now.instant().toString(), '1970-01-01T00:00:01Z'); |
| 238 | +clock.tick(86400_000_000_000n); |
| 239 | +assert.equal(Temporal.now.instant().toString(), '1970-01-02T00:00:01Z'); |
| 240 | + |
| 241 | +// The other functions in the Temporal.now namespace use the same clock. |
| 242 | +assert.equal(Temporal.now.plainDateTimeISO().toString(), '1970-01-02T00:00:01'); |
| 243 | +assert.equal(Temporal.now.plainDateISO().toString(), '1970-01-02'); |
| 244 | +assert.equal(Temporal.now.plainTimeISO().toString(), '00:00:01'); |
| 245 | +assert.equal(Temporal.now.zonedDateTimeISO().toString(), '1970-01-02T00:00:01+00:00[UTC]'); |
| 246 | + |
| 247 | +// Time zones other than UTC and calendars other than ISO are not provided. |
| 248 | +assert.throws(() => Temporal.ZonedDateTime.from('2021-02-12T16:18[America/Vancouver]'), RangeError); |
| 249 | +assert.throws(() => Temporal.PlainDate.from('2021-02-12[u-ca-gregory]'), RangeError); |
| 250 | + |
| 251 | +// Constructing unsupported time zones directly doesn't work either. |
| 252 | +assert.throws(() => new Temporal.TimeZone('America/Vancouver'), RangeError); |
| 253 | +assert.throws(() => Temporal.TimeZone.from('America/Vancouver'), RangeError); |
| 254 | +assert.throws(() => new Temporal.Calendar('gregory'), RangeError); |
| 255 | +assert.throws(() => Temporal.Calendar.from('gregory'), RangeError); |
| 256 | + |
| 257 | +// UTC, offset time zones, and their aliases are still supported. |
| 258 | +assert.equal(new Temporal.TimeZone('-08:00').toString(), '-08:00'); |
| 259 | +assert.equal(new Temporal.TimeZone('Etc/UTC').toString(), 'UTC'); |
| 260 | +assert.equal(new Temporal.TimeZone('Etc/GMT+8').toString(), 'Etc/GMT+8'); |
| 261 | + |
| 262 | +// Check that our function metadata is equal to what we saved earlier... |
| 263 | +assert.equal(Temporal.now.plainDate.toString(), realTemporalNowPlainDateToString); |
| 264 | + |
| 265 | +// ...except take into account that we've frozen the Temporal object. |
| 266 | +Object.values(realTemporalNowPlainDateOwnProperties).forEach((desc) => { |
| 267 | + desc.configurable = false; |
| 268 | + desc.writable = false; |
| 269 | +}); |
| 270 | +assert.deepEqual(Object.getOwnPropertyDescriptors(Temporal.now.plainDate), realTemporalNowPlainDateOwnProperties); |
0 commit comments