-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
236 lines (208 loc) · 8.23 KB
/
script.js
File metadata and controls
236 lines (208 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* ==============================================================
Shepherd – utility to read / write the stack from the URL query string
============================================================== */
function getStackFromUrl() {
const p = new URLSearchParams(window.location.search);
const raw = p.get('stack');
return raw ? raw.split(',').map(decodeURIComponent) : [];
}
function setStackToUrl(stack) {
const p = new URLSearchParams();
if (stack.length) {
const enc = stack.map(encodeURIComponent).join(',');
p.set('stack', enc);
}
const newUrl = `${window.location.pathname}?${p.toString()}`;
window.history.replaceState(null, '', newUrl);
}
/* ==============================================================
Global state for Shepherd
============================================================== */
let lastSelectedProject = null; // URL of the project whose notices are shown
let cachedRss = null; // memoised RSS feed for the session
/* ==============================================================
Rendering helpers
============================================================== */
const projectsUl = document.getElementById('projects');
const resultsDiv = document.getElementById('results');
const refreshBtn = document.getElementById('refresh-btn');
function renderProjectList(stack) {
projectsUl.innerHTML = '';
stack.forEach((url, idx) => {
const li = document.createElement('li');
li.textContent = url;
li.dataset.idx = idx;
li.addEventListener('click', () => {
lastSelectedProject = url;
refreshBtn.disabled = false;
loadVulnerabilities(url);
});
projectsUl.appendChild(li);
});
}
/* ==============================================================
Centralised “add project” routine (used by all UI entry points)
============================================================== */
function addProject(url) {
const stack = getStackFromUrl();
if (!stack.includes(url)) {
stack.push(url);
setStackToUrl(stack);
renderProjectList(stack);
}
}
/* ==============================================================
Manual project entry (form)
============================================================== */
document.getElementById('project-form')
.addEventListener('submit', e => {
e.preventDefault();
const input = document.getElementById('project-url');
const newUrl = input.value.trim();
if (newUrl) {
addProject(newUrl);
input.value = '';
}
});
/* ==============================================================
Popular‑project buttons
============================================================== */
document.querySelectorAll('.pop-btn')
.forEach(btn => btn.addEventListener('click', () => {
addProject(btn.dataset.url);
}));
/* ==============================================================
GitHub search UI
============================================================== */
const ghForm = document.getElementById('gh-search-form');
const ghQueryInput = document.getElementById('gh-query');
const ghResultsUl = document.getElementById('gh-results');
ghForm.addEventListener('submit', async e => {
e.preventDefault();
const query = ghQueryInput.value.trim();
if (!query) return;
ghResultsUl.innerHTML = '<li>Searching…</li>';
try {
const resp = await fetch(
`https://api.github.com/search/repositories?q=${encodeURIComponent(query)}&per_page=5`
);
if (!resp.ok) throw new Error(`GitHub search failed (${resp.status})`);
const data = await resp.json();
if (!data.items || data.items.length === 0) {
ghResultsUl.innerHTML = '<li>No repositories found.</li>';
return;
}
ghResultsUl.innerHTML = '';
data.items.forEach(repo => {
const li = document.createElement('li');
const info = document.createElement('span');
info.textContent = `${repo.full_name}: ${repo.description || ''}`;
const addBtn = document.createElement('button');
addBtn.textContent = 'Add';
addBtn.className = 'add-from-gh';
addBtn.addEventListener('click', () => {
addProject(repo.html_url);
});
li.appendChild(info);
li.appendChild(addBtn);
ghResultsUl.appendChild(li);
});
} catch (err) {
console.error(err);
ghResultsUl.innerHTML = `<li>Error searching GitHub.</li>`;
}
});
/* ==============================================================
Refresh button – reload the RSS feed for the currently selected
============================================================== */
refreshBtn.addEventListener('click', () => {
if (lastSelectedProject) {
// Force a fresh fetch by clearing the cache for this session
cachedRss = null;
loadVulnerabilities(lastSelectedProject);
}
});
/* ==============================================================
RSS‑feed processing (Canadian Cyber Centre)
--------------------------------------------------------------
Public RSS feed: https://www.cyber.gc.ca/rss/alerts.xml
-------------------------------------------------------------- */
const CYBER_CENTRE_RSS = 'https://www.cyber.gc.ca/rss/alerts.xml';
async function fetchRssFeed() {
const resp = await fetch(CYBER_CENTRE_RSS);
if (!resp.ok) throw new Error(`RSS fetch failed (${resp.status})`);
const txt = await resp.text();
const parser = new DOMParser();
return parser.parseFromString(txt, 'application/xml');
}
function extractItems(rssDoc) {
const items = Array.from(rssDoc.querySelectorAll('item'));
return items.map(it => ({
title: it.querySelector('title')?.textContent?.trim() ?? '',
link: it.querySelector('link')?.textContent?.trim() ?? '',
pubDate: it.querySelector('pubDate')?.textContent?.trim() ?? '',
description: it.querySelector('description')?.textContent?.trim() ?? ''
}));
}
/**
* Turn a project URL into a simple keyword that is likely to appear
* in the RSS title/description.
* • GitHub → repository name (last path segment)
* • Anything else → hostname
*/
function deriveKeyword(projectUrl) {
try {
const u = new URL(projectUrl);
if (u.hostname === 'github.com') {
const parts = u.pathname.split('/').filter(Boolean);
return parts[1] || parts[0]; // repo name
}
return u.hostname;
} catch (_) {
return projectUrl;
}
}
/**
* Load and display vulnerability notices for a given project URL.
*/
async function loadVulnerabilities(projectUrl) {
resultsDiv.textContent = 'Fetching latest advisories…';
const keyword = deriveKeyword(projectUrl).toLowerCase();
try {
const rssDoc = cachedRss ?? await fetchRssFeed();
cachedRss = rssDoc; // keep for subsequent calls
const items = extractItems(rssDoc);
const matches = items.filter(it =>
it.title.toLowerCase().includes(keyword) ||
it.description.toLowerCase().includes(keyword)
);
if (matches.length === 0) {
resultsDiv.textContent = `No advisories found for "${keyword}".`;
return;
}
const lines = matches.map(it => {
const date = new Date(it.pubDate).toLocaleDateString(undefined, {
year: 'numeric', month: 'short', day: 'numeric'
});
return `• ${it.title}
Date: ${date}
Link: ${it.link}
${it.description}`;
});
resultsDiv.textContent = lines.join('\n\n');
} catch (err) {
console.error(err);
resultsDiv.textContent = `Error loading advisories. Please try again later.`;
}
}
/* ==============================================================
Page initialisation – Shepherd
============================================================== */
const initialStack = getStackFromUrl();
renderProjectList(initialStack);
if (initialStack.length) {
// Auto‑load the first project's notices for a quick start
lastSelectedProject = initialStack[0];
refreshBtn.disabled = false;
loadVulnerabilities(initialStack[0]);
}