From adccc9c94aa1ac6088e8fa80912615effde061a8 Mon Sep 17 00:00:00 2001 From: Rijubrata Bhaumik Date: Mon, 19 Aug 2019 10:22:27 -0700 Subject: [PATCH] [sensors] Round off Ambient Light Sensor readouts to the nearest 50 Lux. There have been some concerns regarding the precision of readouts from the Ambient Light Sensor. To decrease the entropy, we are rounding off the illuminance value to the nearest 50 Lux to mitigate the known attack vectors as summarized in [1]. sensor-helpers.js now has a small RingBuffer class that is used to wrap the readings passed via MockSensor.setSensorReading() so that we iterate over the array of readings (and wrap around) and every time a reading event is fired a potentially different value is emitted. This is useful for ALS because we will no longer be able to use the same value in succession and expect two reading events to be fired (only one will). In order to make it easier to implement those changes in sensor-helpers.js, runGenericSensorTests()'s signature has been simplified and all raw readings and expected values have a standardized format, an array of arrays, where each item corresponds to one reading. This also allows us to stop using Function.prototype.bind() for the reading verification function and be more explicit in telling which expected value we want in a specific reading callback. [1] https://github.com/w3c/ambient-light/issues/13#issuecomment-302393458 Co-author: Raphael Kubo da Costa Bug: 642731, 606766 Change-Id: Icc4cfa6b87bd5a87c2cac6ebe322201d7dda6c33 --- resources/chromium/generic_sensor_mocks.js | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/resources/chromium/generic_sensor_mocks.js b/resources/chromium/generic_sensor_mocks.js index 531c3b8fe0542b..5642504bdbdb3d 100644 --- a/resources/chromium/generic_sensor_mocks.js +++ b/resources/chromium/generic_sensor_mocks.js @@ -18,6 +18,7 @@ var GenericSensorTest = (() => { this.binding_.setConnectionErrorHandler(() => { this.reset(); }); + this.readingsProducerFunction_ = null; } getDefaultConfiguration() { @@ -64,6 +65,7 @@ var GenericSensorTest = (() => { this.requestedFrequencies_ = []; this.buffer_.fill(0); this.binding_.close(); + this.readingsProducerFunction_ = null; } startReading() { @@ -81,6 +83,17 @@ var GenericSensorTest = (() => { const maxFrequencyHz = this.requestedFrequencies_[0]; const timeoutMs = (1 / maxFrequencyHz) * 1000; this.sensorReadingTimerId_ = window.setInterval(() => { + // By default, each reading will contain the same value (0), unless + // |readingsProducerFunction_| is defined, in which case its return + // value is used instead. + if (this.readingsProducerFunction_) { + const readings = this.readingsProducerFunction_(); + if (!Array.isArray(readings)) { + throw new TypeError("The readings generator function must return " + + "an array"); + } + this.buffer_.set(readings, 2); + } // For all tests sensor reading should have monotonically // increasing timestamp in seconds. this.buffer_[1] = window.performance.now() * 0.001; @@ -100,6 +113,13 @@ var GenericSensorTest = (() => { get isReading() { this.sensorReadingTimerId_ !== null; } + + // Can be used to make each reading produced by this sensor to come from + // |readingsProducer|, a function that returns an array that is passed to + // this.buffer_ and which represents a raw reading. + setReadingsProducerFunction(readingsProducerFunction) { + this.readingsProducerFunction_ = readingsProducerFunction; + } } // Class that mocks SensorProvider interface defined in @@ -151,6 +171,17 @@ var GenericSensorTest = (() => { maxAllowedFrequencyHz = 10; } + // Chromium applies some rounding and other privacy-related measures that + // can cause ALS not to report a reading when it has not changed beyond a + // certain threshold compared to the previous illuminance value. Make + // each reading return a different value that is significantly different + // from the previous one. + if (type == device.mojom.SensorType.AMBIENT_LIGHT) { + this.activeSensor_.setReadingsProducerFunction(() => { + return [window.performance.now() * 100]; + }) + } + let initParams = new device.mojom.SensorInitParams({ sensor: sensorPtr, clientRequest: mojo.makeRequest(this.activeSensor_.client_),