Skip to content

Commit

Permalink
Added js samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 1, 2024
1 parent c6a2d34 commit 0cafe20
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
78 changes: 78 additions & 0 deletions javascript/error-sample.js
Original file line number Diff line number Diff line change
@@ -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() {}; // デフォルトエクスポートの誤った構文
40 changes: 40 additions & 0 deletions javascript/security-sample.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 0cafe20

Please sign in to comment.