# Observable Timing Discrepancy

## Overview

* **Rule ID**: `javascript_lang_observable_timing`
* **Applicable Languages**: Javascript
* **Weakness ID**: CWE-208

## Description

Observable Timing Discrepancy happens when attackers can measure and observe the time it takes for specific operations to complete. This vulnerability is especially problematic when operations involve sensitive information, like password checks or secret comparisons. By analyzing the duration of these operations, attackers might infer confidential details, thereby compromising your data.

## Remediation Guidelines

* **Do** implement algorithms that process sensitive information in constant time. This approach helps prevent attackers from inferring secrets based on the duration of operations.
* **Do** use built-in security features and cryptographic libraries that offer functions safe from timing attacks for comparing secret values.
* **Do** not use direct string comparisons for sensitive information, as this can lead to early termination of the function if a mismatch is found, revealing timing information.

  ```javascript
   if (apiToken === "zDE9ET!TDq2uZx2oM!FD2") { // unsafe
      ...
   }
  ```
* **Do not** design application logic that changes execution paths in a manner that could introduce timing discrepancies based on user input or secret values.

## References

* [**OWASP Guide to Cryptography**](https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html)
* [**MDN Web Docs on SubtleCrypto API**](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto)
* [**CWE-208: Observable Timing Discrepancy**](https://cwe.mitre.org/data/definitions/208.html)

## Configuration

To omit this rule during a scan, and to provide you with continuous 24/7 code-level scanning, you can employ our [**SAST TOOL**](https://scopy.sec1.io/login)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sec1.io/user-docs/4-sast/3-javascript/observable-timing-discrepancy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
