From 0cafe20c74ed078b4f595704810980d43bda48b9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 1 Sep 2024 12:56:54 +0900 Subject: [PATCH] Added js samples --- javascript/error-sample.js | 78 +++++++++++++++++++++++++++++++++++ javascript/security-sample.js | 40 ++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 javascript/error-sample.js create mode 100644 javascript/security-sample.js diff --git a/javascript/error-sample.js b/javascript/error-sample.js new file mode 100644 index 0000000..0c36679 --- /dev/null +++ b/javascript/error-sample.js @@ -0,0 +1,78 @@ +// 構文エラーと潜在的な問題を含むJavaScriptファイル + +// 1. 変数宣言の問題 +let x = 5; +let x = 10; // 同じスコープで変数を再宣言 + +// 2. 関数定義の問題 +function myFunction(a, a) { // 重複した引数名 + return a + b; // 未定義の変数 'b' を使用 +} + +// 3. オブジェクトリテラルの問題 +let obj = { + prop1: "value1", + prop1: "value2", // 重複したプロパティ名 + "invalid-key": 123, + function() { // メソッド定義の誤り + console.log("Hello"); + } +}; + +// 4. 配列リテラルの問題 +let arr = [1, 2, 3,]; // 末尾のカンマ(一部の環境ではエラー) + +// 5. 制御構造の問題 +if (x === 5) { + console.log("x is 5"); +else { // else の前に閉じ括弧がない + console.log("x is not 5"); +} + +// 6. ループの問題 +for (let i = 0; i < 5; i++) { + console.log(i); + break; // 不必要な break 文 + console.log("This will never be reached"); +} + +// 7. switch 文の問題 +switch (x) { + case 1: + console.log("One"); + break; + case 2: + console.log("Two"); + // break 文が抜けている + default: + console.log("Other"); +} + +// 8. 非同期処理の問題 +async function fetchData() { + let response = await fetch('https://api.example.com/data'); + return response.json; // メソッド呼び出しの括弧が抜けている +} + +// 9. クラス定義の問題 +class MyClass { + constructor(name) { + this.name = name; + } + + static { // 静的ブロックの誤った使用 + console.log("Static block"); + } + + getName() { + return this.name; + } + + setName(name) { // setter メソッドの誤った定義 + this.name = name; + } +} + +// 10. モジュールの問題 +import { nonExistentFunction } from './non-existent-module.js'; +export default = function() {}; // デフォルトエクスポートの誤った構文 \ No newline at end of file diff --git a/javascript/security-sample.js b/javascript/security-sample.js new file mode 100644 index 0000000..84043ac --- /dev/null +++ b/javascript/security-sample.js @@ -0,0 +1,40 @@ +// 警告: このコードには意図的にセキュリティ上の問題が含まれています。 +// 実際の環境で使用しないでください。 + +// 1. クロスサイトスクリプティング (XSS) の脆弱性 +function displayUserInput() { + var userInput = document.getElementById('userInput').value; + document.getElementById('output').innerHTML = userInput; +} + +// 2. SQL インジェクションの脆弱性 +function queryDatabase(userId) { + var query = "SELECT * FROM users WHERE id = " + userId; + // この query を直接データベースに送信する(危険) +} + +// 3. 機密情報の露出 +var apiKey = "1234567890abcdef"; +console.log("API Key: " + apiKey); + +// 4. eval() の使用(潜在的に危険) +function executeUserCode(code) { + eval(code); +} + +// 5. 安全でないランダム値の生成 +function generateToken() { + return Math.random().toString(36).substring(7); +} + +// 6. パスワードを平文で保存 +var users = [ + { username: "admin", password: "password123" }, + { username: "user", password: "qwerty" } +]; + +// 7. 安全でない正規表現 +function validateEmail(email) { + var re = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; + return re.test(email); +} \ No newline at end of file