fix(tours): a short link whose name does not resolve just now, or that Google does not know, is said as such in the walk import (CW-06, CW-07)

When the name of maps.app.goo.gl or goo.gl could not be looked up for a
moment, the walk import said the short link leads somewhere other than
Google Maps. It now says the link could not be followed just now and answers
502, as for any other failure on the way; a name that leads into this
network is still refused.

A short link Google answers with 404 or 410 is one it does not know, and
asking again changes nothing. The dialog now says so in the reader's
language and asks to share the route from Google Maps again, with a 400 and
the reason short_link_unknown, instead of suggesting to try again later. A
busy or blocking answer from Google stays a failure that may pass.

The MCP tool preview_google_maps_walk is marked open world, since it follows
a short link to Google on every call, and the English text REST and MCP send
for each reason is held to the English text the planner shows.
This commit is contained in:
Maurice
2026-10-20 04:42:14 +00:00
parent 4ca5728072
commit 7470a2c6d4
38 changed files with 212 additions and 29 deletions
@@ -106,8 +106,10 @@ export class CollectionGpxError extends Error {
/**
* CW-06: where a Google Maps short link led. `refused` when a hop or the
* landing link is not one the walk import may follow or read, `unreachable`
* when the chain could not be followed in time or answered nonsense,
* `too_many_redirects` and `no_redirect` as they say.
* when the chain could not be followed in time, a name did not resolve, or it
* answered nonsense, `not_found` when Google answered that it has no such
* link (404 or 410), `too_many_redirects` and `no_redirect` as they say.
*/
export type ShortLinkOutcome =
{ ok: true; url: string } | { ok: false; why: 'refused' | 'unreachable' | 'too_many_redirects' | 'no_redirect' };
export type ShortLinkOutcome = { ok: true; url: string } | { ok: false; why: ShortLinkFailure };
export type ShortLinkFailure = 'refused' | 'unreachable' | 'not_found' | 'too_many_redirects' | 'no_redirect';
@@ -31,6 +31,15 @@ export interface ShortLinkLimits {
export const SHORT_LINK_LIMITS: ShortLinkLimits = { hops: 3, deadlineMs: 8_000, locationChars: 8_192 };
/** Google's final answer that it has no such link (404), or no longer (410); asking again changes nothing. */
const NO_SUCH_LINK: ReadonlySet<number> = new Set([404, 410]);
/** What one hop answered: its status, and the Location of a 3xx or null. */
interface HopAnswer {
status: number;
location: string | null;
}
/** The lookup inside the SSRF guard cannot be aborted; this holds it to the chain's deadline as well. */
function beforeDeadline<T>(work: Promise<T>, signal: AbortSignal): Promise<T> {
return new Promise<T>((resolve, reject) => {
@@ -58,7 +67,7 @@ export class GoogleMapsShortLinkProvider {
try {
return await this.hop(start, 0, signal, limits);
} catch (err: unknown) {
// A deadline passed, a name that does not resolve, a connection that failed.
// A deadline passed, a name lookup that hung, a connection that failed.
logDebug(`[walk import] Google Maps short link not followed: ${err instanceof Error ? err.name : 'error'}`);
return { ok: false, why: 'unreachable' };
}
@@ -77,8 +86,10 @@ export class GoogleMapsShortLinkProvider {
// Strict: a private address stays refused whatever ALLOW_INTERNAL_NETWORK says.
const ssrf = await beforeDeadline(checkSsrf(url.href, true), signal);
const addresses = ssrf.allowed ? (ssrf.resolvedIps ?? []) : [];
if (addresses.length === 0) return { ok: false, why: 'refused' };
const location = await this.locationOf(url, addresses, signal);
// A name leading into this network is refused; one that does not resolve just now is Google's side, not the link's.
if (addresses.length === 0) return { ok: false, why: ssrf.isPrivate ? 'refused' : 'unreachable' };
const { status, location } = await this.answerOf(url, addresses, signal);
if (NO_SUCH_LINK.has(status)) return { ok: false, why: 'not_found' };
if (location === null) return { ok: false, why: 'no_redirect' };
if (location.length > limits.locationChars) return { ok: false, why: 'unreachable' };
let next: URL;
@@ -92,8 +103,8 @@ export class GoogleMapsShortLinkProvider {
return isGoogleMapsLongLink(next) ? { ok: true, url: next.href } : { ok: false, why: 'refused' };
}
/** One request through the pinned dispatcher, nothing followed and no body read: the Location of a 3xx, or null. */
private async locationOf(url: URL, addresses: string[], signal: AbortSignal): Promise<string | null> {
/** One request through the pinned dispatcher, nothing followed and no body read. */
private async answerOf(url: URL, addresses: string[], signal: AbortSignal): Promise<HopAnswer> {
const dispatcher = createOutboundDispatcher(url.href, addresses);
// Nothing of the caller's goes along: no cookie, no authorization, no referrer.
// Node's fetch keeps no cookie jar, and credentials 'omit' says so for the record.
@@ -110,7 +121,8 @@ export class GoogleMapsShortLinkProvider {
// Object.assign joins the two without casting either away.
const response = await fetch(url.href, Object.assign(init, { dispatcher }));
discardBody(response);
return response.status >= 300 && response.status < 400 ? response.headers.get('location') : null;
const { status } = response;
return { status, location: status >= 300 && status < 400 ? response.headers.get('location') : null };
} finally {
void dispatcher.close().catch((err: unknown) => logDebug(`[walk import] dispatcher not closed: ${String(err)}`));
}
@@ -1,6 +1,6 @@
import { ADDON_IDS } from '../../addons';
import { noAccess, permissionDenied } from '../../mcp/tools/_shared';
import { McpController, Tool, TOOL_ANNOTATIONS_READONLY, ok, type McpContext } from '../../nest-mcp';
import { McpController, Tool, TOOL_ANNOTATIONS_OPEN_WORLD_READONLY, ok, type McpContext } from '../../nest-mcp';
import { addonGate } from '../addons/addon-gate';
import { AddonsService } from '../addons/addons.service';
import { McpToolGuardsService } from '../mcp-shared/mcp-tool-guards.service';
@@ -36,7 +36,8 @@ export class TourWalkImportMcp {
tripId: idSchema.describe('The trip the walk is meant for'),
url: preview.url,
},
annotations: TOOL_ANNOTATIONS_READONLY,
// Open world: a short link is followed to Google on every call.
annotations: TOOL_ANNOTATIONS_OPEN_WORLD_READONLY,
when: addonGate(ADDON_IDS.TOURS),
access: { group: 'trips', mode: 'read' },
})
@@ -7,6 +7,7 @@ import {
PlaceImportService,
type DirWaypoint,
} from '../place-import/place-import.service';
import type { ShortLinkFailure } from '../place-import/place-import.types';
import { Injectable } from '@nestjs/common';
import {
TOUR_STATION_NAME_MAX,
@@ -25,6 +26,7 @@ const REFUSAL_TEXT: Readonly<Record<WalkImportRefusal, string>> = {
'The link does not say clearly that the route is on foot. Choose walking in Google Maps and share it again.',
short_link_refused: 'The short link leads somewhere other than Google Maps, so it was not followed.',
short_link_failed: 'The short link could not be followed just now. Try again, or paste the long link.',
short_link_unknown: 'Google Maps does not know that short link. Share the route in Google Maps again.',
too_few_stops: 'Fewer than two stops could be read from that link.',
too_many_stops: 'That route has more stops than a walk can take.',
};
@@ -32,6 +34,19 @@ const REFUSAL_TEXT: Readonly<Record<WalkImportRefusal, string>> = {
/** Reasons that are Google's answer rather than the link's. */
const UPSTREAM: ReadonlySet<WalkImportRefusal> = new Set<WalkImportRefusal>(['short_link_failed']);
/**
* Why a short link was not followed, as the walk import says it: a link that
* leaves Google Maps, one Google does not know, and the rest a failure on
* the way that may pass.
*/
const SHORT_LINK_REFUSAL: Readonly<Record<ShortLinkFailure, WalkImportRefusal>> = {
refused: 'short_link_refused',
not_found: 'short_link_unknown',
unreachable: 'short_link_failed',
too_many_redirects: 'short_link_failed',
no_redirect: 'short_link_failed',
};
function refuse(reason: WalkImportRefusal): DomainError {
return new DomainError(UPSTREAM.has(reason) ? 502 : 400, REFUSAL_TEXT[reason], { code: reason, details: { reason } });
}
@@ -76,7 +91,7 @@ export class TourWalkImportService {
private async followed(link: URL): Promise<URL> {
const outcome = await this.placeImport.resolveGoogleShortLink(link.href);
if ('why' in outcome) throw refuse(outcome.why === 'refused' ? 'short_link_refused' : 'short_link_failed');
if ('why' in outcome) throw refuse(SHORT_LINK_REFUSAL[outcome.why]);
return new URL(outcome.url);
}
}