Skip to content

Commit cbe1f84

Browse files
committed
Try to avoid adding localhost/local network addresses to the index
1 parent 126da3d commit cbe1f84

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/buildtime/generate-apis.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,31 @@ const IGNORED_SPEC_IDS = [
333333
'github.com/api.github.com.2022-11-28'
334334
];
335335

336+
// For some specs, we do want to include the spec in the collection, but the server URLs shouldn't
337+
// be indexed, because they're not valid public URLs. For example, relative URLs, localhost/local
338+
// network URLs, etc. The specs can still be required by id, where required, it's just that they're
339+
// ambiguous enough that looking up these addresses in the index shouldn't return these APIs.
340+
function shouldIndexUrl(url: string) {
341+
if (!url) return false; // Bizarrely, yes, some specs list an empty server URL
342+
if (url.startsWith('/')) return false; // Purely relative URLs aren't indexable
343+
344+
// Make protocol-less URLs (common from Swagger?) parseable:
345+
if (!url.match(/^http(s)?:\/\//)) url = `https://${url}`;
346+
347+
try {
348+
const parsedUrl = new URL(url);
349+
350+
return parsedUrl.hostname !== 'localhost' && // Localhost URLs
351+
!parsedUrl.hostname.endsWith('.localhost') &&
352+
!parsedUrl.hostname.endsWith('.local') && // mDNS local addresses
353+
!parsedUrl.hostname.match(/^(127|10|192|0)\.\d+\.\d+\.\d+$/) && // Local network ips
354+
parsedUrl.hostname.includes('.'); // Local-only hostnames
355+
} catch (e) {
356+
console.log('Failed to parse', url);
357+
return false; // If it's not a parseable URL, it's definitely not indexable
358+
}
359+
}
360+
336361
export async function generateApis(globs: string[], options: ApiGenerationOptions = {}) {
337362
const [specs] = await Promise.all([
338363
globby(globs),
@@ -405,10 +430,7 @@ export async function generateApis(globs: string[], options: ApiGenerationOption
405430
specIds[specId.toLowerCase()] = specSource;
406431

407432
serverUrls.forEach((url) => {
408-
// The index stores full URLs, so we skip indexing all server URLS that are relative - it's not
409-
// really practical or helpful to include or match against these. We want a domain etc. The
410-
// specs are still built & available, they're just not discoverable from the index.
411-
if (url.startsWith('/')) return;
433+
if (!shouldIndexUrl(url)) return; // Skip adding this spec to the index
412434

413435
if (index[url]) {
414436
index[url] = [specId].concat(index[url]);

0 commit comments

Comments
 (0)