Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 1x 1x 15x 15x 15x 15x 15x 16x 11x 11x 11x 11x 11x 11x 11x 11x 11x 16x 16x 1x 1x 10x 3x 10x 7x 7x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 1x 5x 6x 1x 1x 1x 4x 6x 1x 1x 1x 3x 6x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 1x 1x 4x 4x 4x 4x 4x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 9x 9x 1x 1x 9x 9x 1x 1x 9x 9x 9x 1x 1x 23x 1x 1x 22x 23x 1x 1x 21x 23x 1x 1x 20x 20x 20x 23x 17x 17x 17x 23x 23x 23x 23x 23x 23x 23x 13x 13x 13x 13x 13x 13x 13x 23x 12x 12x 23x 4x 1x 1x 23x 4x 1x 1x 4x 4x 4x 4x 4x 4x 23x 3x 3x 23x 1x 1x 23x 1x 1x 12x 12x 12x 11x 11x 12x 10x 10x 12x 12x 12x 12x 12x 12x 2x 2x 12x 1x 1x 8x 8x 7x 8x 2x 2x 5x 8x 8x 3x 3x 8x 1x 1x 2x 2x 8x 1x 1x 5x 1x 1x 4x 5x 1x 1x 3x 3x 3x 3x 5x 1x 1x 5x 1x 1x 4x 4x 4x 1x 1x 3x 4x 1x 1x 2x 2x 1x 1x 1x | /**
* Descargador de adjuntos de Work Items
* Encapsula validaciones, sanitizacion de nombres y descarga autenticada
*/
const axios = require("axios");
const net = require("net");
const path = require("path");
const fs = require("fs").promises;
const MAX_URL_LENGTH = 2048;
const MAX_FILE_NAME_LENGTH = 80;
const SAFE_EXTENSION_REGEX = /^\.[a-z0-9]{1,10}$/i;
const RESERVED_WIN_NAMES = new Set([
"CON",
"PRN",
"AUX",
"NUL",
"COM1",
"COM2",
"COM3",
"COM4",
"COM5",
"COM6",
"COM7",
"COM8",
"COM9",
"LPT1",
"LPT2",
"LPT3",
"LPT4",
"LPT5",
"LPT6",
"LPT7",
"LPT8",
"LPT9",
]);
class WorkItemAttachmentDownloader {
/**
* @param {Object} options
* @param {string} options.pat - PAT de Azure Boards
* @param {Function} [options.logCallback] - Logger opcional
* @param {Object} [options.httpClient] - Cliente HTTP inyectable (axios-like)
* @param {Object} [options.fsModule] - FS inyectable (fs.promises-like)
*/
constructor({ pat, logCallback = null, httpClient = null, fsModule = fs }) {
if (!pat || typeof pat !== "string") {
throw new Error("PAT requerido para descargar adjuntos");
}
this.pat = pat;
this.logCallback = logCallback;
this.fs = fsModule;
this.httpClient =
httpClient ||
axios.create({
auth: {
username: "",
password: pat,
},
headers: {
"Content-Type": "application/octet-stream",
},
timeout: 30000,
});
}
log(message) {
if (this.logCallback) {
this.logCallback(message);
} else {
console.log(message);
}
}
/**
* Descarga un adjunto en la carpeta destino.
* @param {Object} attachment
* @param {string} attachment.name
* @param {string} attachment.url
* @param {string} outputDirectory
* @returns {Promise<Object>} Resultado de descarga
*/
async downloadAttachment(attachment, outputDirectory) {
if (!attachment || typeof attachment !== "object") {
this.log("Adjunto invalido: entrada no es objeto");
throw new Error("Adjunto invalido");
}
if (!attachment.url || typeof attachment.url !== "string") {
this.log("Adjunto invalido: URL faltante");
throw new Error("Adjunto sin URL valida");
}
if (!outputDirectory || typeof outputDirectory !== "string") {
this.log("Adjunto invalido: directorio de salida faltante");
throw new Error("Directorio de salida invalido");
}
if (!this._isAllowedAttachmentUrl(attachment.url)) {
this.log(
`URL de adjunto no permitida: ${this._sanitizeUrlForLog(attachment.url)}`,
);
throw new Error("URL de adjunto no permitida");
}
const originalName = attachment.name;
const fileName = this._sanitizeFileNameWithExtension(originalName);
const outputPath = path.join(outputDirectory, fileName);
await this._ensureDirectory(outputDirectory);
const downloadUrl = this._buildDownloadUrl(attachment.url);
this.log(
`Descargando adjunto: ${this._sanitizeLogText(originalName)} -> ${fileName}`,
);
const startTime = Date.now();
const response = await this.httpClient.get(downloadUrl, {
responseType: "arraybuffer",
});
const durationMs = Date.now() - startTime;
await this.fs.writeFile(outputPath, response.data);
const extension = path.extname(fileName).toLowerCase();
this.log(
`Descarga completada: ${fileName} (${response.data.length} bytes en ${durationMs}ms)`,
);
return {
fileName,
path: outputPath,
isDocx: extension === ".docx",
};
}
_buildDownloadUrl(url) {
const hasQuery = url.includes("?");
const separator = hasQuery ? "&" : "?";
const downloadParam = url.includes("download=true") ? "" : "&download=true";
return `${url}${separator}api-version=7.0${downloadParam}`;
}
_sanitizeFileNameWithExtension(name) {
const safeName = name && typeof name === "string" ? name : "untitled";
const normalized = safeName.normalize("NFC");
const baseName = path.basename(normalized);
const parsed = path.parse(baseName);
const rawExt = parsed.ext ? parsed.ext.toLowerCase() : "";
const extension = SAFE_EXTENSION_REGEX.test(rawExt) ? rawExt : "";
let base = parsed.name || "untitled";
base = base
.replace(/[<>:"/\\|?*\s]+/g, "-")
.replace(/[^\w-]/g, "")
.replace(/-+/g, "-")
.replace(/^-+|-+$/g, "");
if (!base || base === "." || base === ".." || base.includes("..")) {
base = "untitled";
}
if (RESERVED_WIN_NAMES.has(base.toUpperCase())) {
base = "untitled";
}
if (base.length > MAX_FILE_NAME_LENGTH) {
base = base.substring(0, MAX_FILE_NAME_LENGTH).replace(/-+$/, "");
}
return `${base}${extension}`;
}
_isAllowedAttachmentUrl(url) {
if (!url || typeof url !== "string") {
return false;
}
if (url.length > MAX_URL_LENGTH) {
return false;
}
if (/[\x00-\x1f\x7f]/.test(url)) {
return false;
}
try {
const parsed = new URL(url);
if (parsed.protocol !== "https:") return false;
const rawHostname = parsed.hostname.toLowerCase();
const hostname =
rawHostname.startsWith("[") && rawHostname.endsWith("]")
? rawHostname.slice(1, -1)
: rawHostname;
if (!hostname || hostname.length > 253) return false;
if (hostname.includes("..")) return false;
if (hostname.startsWith(".") || hostname.endsWith(".")) return false;
const blockedHosts = [
"localhost",
"127.0.0.1",
"0.0.0.0",
"169.254.169.254",
];
if (blockedHosts.includes(hostname)) return false;
const ipVersion = net.isIP(hostname);
if (ipVersion === 4) {
if (!this._isAllowedIPv4(hostname)) return false;
return false;
}
if (ipVersion === 6) {
if (!this._isAllowedIPv6(hostname)) return false;
return false;
}
const allowedDomains = ["dev.azure.com", "visualstudio.com"];
const isAllowedDomain = allowedDomains.some(
(domain) => hostname === domain || hostname.endsWith(`.${domain}`),
);
if (!isAllowedDomain) return false;
return true;
} catch (error) {
return false;
}
}
_isAllowedIPv4(hostname) {
const ipv4Regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
const match = hostname.match(ipv4Regex);
if (!match) return false;
const octets = match.slice(1).map(Number);
if (octets.some((octet) => octet > 255)) return false;
const [a, b] = octets;
if (a === 10) return false;
if (a === 127) return false;
if (a === 169 && b === 254) return false;
if (a === 172 && b >= 16 && b <= 31) return false;
if (a === 192 && b === 168) return false;
if (a === 0) return false;
return true;
}
_isAllowedIPv6(hostname) {
const normalized = hostname.toLowerCase();
if (normalized === "::" || normalized === "::1") return false;
if (normalized.startsWith("fc") || normalized.startsWith("fd")) {
return false;
}
if (/^fe[89ab]/i.test(normalized)) return false;
if (normalized.startsWith("fec0:")) return false;
const ipv4Mapped = normalized.match(/^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/);
if (ipv4Mapped) {
return this._isAllowedIPv4(ipv4Mapped[1]);
}
return true;
}
_sanitizeUrlForLog(url) {
if (!url || typeof url !== "string") {
return "";
}
if (url.length > MAX_URL_LENGTH) {
return "[URL_TRUNCADA]";
}
try {
const parsed = new URL(url);
return `${parsed.origin}${parsed.pathname}`;
} catch {
return "[URL_INVALIDA]";
}
}
_sanitizeLogText(value) {
const raw = value && typeof value === "string" ? value : "";
const trimmed = raw.replace(/[\x00-\x1f\x7f]/g, "");
if (trimmed.length > 200) {
return `${trimmed.slice(0, 200)}...`;
}
return trimmed;
}
async _ensureDirectory(dirPath) {
await this.fs.mkdir(dirPath, { recursive: true });
}
}
module.exports = WorkItemAttachmentDownloader;
|