계: Time and State

분산 컴퓨팅에서 중요한 것은 시간과 상태입니다. 즉, 둘 이상의 구성 요소가 통신하려면 상태를 공유해야 하며 시간이 걸립니다.

대부분의 프로그래머들은 자신들의 작업을 의인화합니다. 하나의 컨트롤 스레드로 마치 자신이 직접 작업한 것처럼 전체 프로그램을 수행할 수 있을 것으로 생각합니다. 하지만 최신 컴퓨터는 작업 간에 매우 빠르게 전환되므로 멀티코어, 멀티 CPU 또는 분산 시스템에서 두 이벤트가 정확히 동시에 발생할 수 있습니다. 프로그래머가 만든 프로그램 실행 모델과 실제 현실 간에 빠르게 결함이 생기기 마련입니다. 이러한 결함은 스레드, 프로세스, 시간 및 정보 간의 예기치 않은 상호작용과 관련이 있습니다. 이러한 상호 작용은 공유 상태를 통해 발생합니다. 세마포, 변수, 파일 시스템 그리고 정보를 저장할 수 있는 모든 것이 여기에 포함됩니다.

Race Condition

Abstract
이러한 콜백이 설정되면 race condition이 발생할 수 있습니다.
Explanation
개발자는 Node.js를 사용하여 IO 차단 이벤트에 콜백을 할당할 수 있습니다. 콜백은 비동기로 실행되므로 기본 응용 프로그램이 IO에 의해 차단되지 않기 때문에 이렇게 하면 성능이 개선됩니다. 그러나 콜백 외부의 항목을 실행하려면 콜백 내의 코드가 먼저 실행되어야 하는 경우에는 race condition이 발생할 수 있습니다.

예제 1: 다음 코드에서는 authentication을 위해 데이터베이스를 기준으로 사용자를 확인합니다.

 
...
var authenticated = true;
...
database_connect.query('SELECT * FROM users WHERE name == ? AND password = ? LIMIT 1', userNameFromUser, passwordFromUser, function(err, results){
if (!err && results.length > 0){
authenticated = true;
}else{
authenticated = false;
}
});

if (authenticated){
//do something privileged stuff
authenticatedActions();
}else{
sendUnathenticatedMessage();
}


이 예제에서는 로그인용 사용자 자격 증명을 확인하기 위해 백엔드 데이터베이스를 호출하며, 자격 증명이 확인되면 변수를 true로 설정하고 확인되지 않으면 false로 설정합니다. 하지만 콜백은 IO에 의해 차단되므로 비동기로 실행되며 if (authenticated) 확인 후에 실행될 수 있습니다. 기본값은 true이기 때문에 사용자가 실제로 인증되었는지 여부와 관계없이 if 문이 실행됩니다.
References
[1] Kristopher Kowal Documentation for q
[2] Piotr Pelczar Asynchronous programming done right.
[3] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1.0
[4] Standards Mapping - CIS Microsoft Azure Foundations Benchmark complete
[5] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 2.0
[6] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 4
[7] Standards Mapping - CIS Google Kubernetes Engine Benchmark integrity
[8] Standards Mapping - CIS Kubernetes Benchmark partial
[9] Standards Mapping - Common Weakness Enumeration CWE ID 362, CWE ID 367
[10] Standards Mapping - Common Weakness Enumeration Top 25 2022 [22] CWE ID 362
[11] Standards Mapping - Common Weakness Enumeration Top 25 2023 [21] CWE ID 362
[12] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-000366, CCI-003178
[13] Standards Mapping - General Data Protection Regulation (GDPR) Indirect Access to Sensitive Data
[14] Standards Mapping - NIST Special Publication 800-53 Revision 4 CM-6 Configuration Settings (P1)
[15] Standards Mapping - NIST Special Publication 800-53 Revision 5 CM-6 Configuration Settings
[16] Standards Mapping - OWASP Top 10 2021 A04 Insecure Design
[17] Standards Mapping - OWASP Application Security Verification Standard 4.0 1.11.2 Business Logic Architectural Requirements (L2 L3), 1.11.3 Business Logic Architectural Requirements (L3), 1.11.2 Business Logic Architectural Requirements (L2 L3), 1.11.3 Business Logic Architectural Requirements (L3), 11.1.6 Business Logic Security Requirements (L2 L3)
[18] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.6
[19] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.6
[20] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.6
[21] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.6
[22] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0 Requirement 6.2.4
[23] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 4.2 - Critical Asset Protection
[24] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 4.2 - Critical Asset Protection, Control Objective B.3.3 - Terminal Software Attack Mitigation
[25] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 4.2 - Critical Asset Protection, Control Objective B.3.3 - Terminal Software Attack Mitigation
[26] Standards Mapping - SANS Top 25 2009 Insecure Interaction - CWE ID 362
[27] Standards Mapping - SANS Top 25 2010 Insecure Interaction - CWE ID 362
[28] Standards Mapping - Security Technical Implementation Guide Version 3.1 APP3630.1 CAT II
[29] Standards Mapping - Security Technical Implementation Guide Version 3.4 APP3630.1 CAT II
[30] Standards Mapping - Security Technical Implementation Guide Version 3.5 APP3630.1 CAT II
[31] Standards Mapping - Security Technical Implementation Guide Version 3.6 APP3630.1 CAT II
[32] Standards Mapping - Security Technical Implementation Guide Version 3.7 APP3630.1 CAT II
[33] Standards Mapping - Security Technical Implementation Guide Version 3.9 APP3630.1 CAT II
[34] Standards Mapping - Security Technical Implementation Guide Version 3.10 APP3630.1 CAT II
[35] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-001995 CAT II
[36] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-001995 CAT II
[37] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-001995 CAT II
[38] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-001995 CAT II
[39] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-001995 CAT II
[40] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-001995 CAT II
[41] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-001995 CAT II
[42] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-001995 CAT II
[43] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-001995 CAT II
[44] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-001995 CAT II
[45] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-001995 CAT II
[46] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-001995 CAT II
[47] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-001995 CAT II
[48] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-001995 CAT II
desc.structural.javascript.race_condition