| 12345678910111213141516171819202122232425262728293031323334 |
- // Only identifiers from the observed Teams AMS image schema are retained. Never
- // retain arbitrary img URLs, query strings, cookies or signed download links.
- export const AMS_OBJECT_ID = /^[a-zA-Z0-9_-]{1,200}$/;
- export function messageImage(attributes) {
- const type = attributes.itemtype || '';
- if (type && !/^https?:\/\/schema\.skype\.com\/AMSImage$/.test(type)) return null;
- try {
- const url = new URL(attributes.src);
- if (url.protocol !== 'https:' || url.username || url.password || url.port || url.hash
- || !['as-api.asm.skype.com', 'as-prod.asyncgw.teams.microsoft.com'].includes(url.hostname)) return null;
- const match = url.pathname.match(/^\/v1\/(?:[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}\/)?objects\/([a-zA-Z0-9_-]{1,200})\/views\/imgo$/);
- if (!match) return null;
- const image = { objectId: match[1], label: cleanImageLabel(attributes.alt) || '图片' };
- for (const key of ['width', 'height']) {
- const value = Number(attributes[key]);
- if (Number.isInteger(value) && value > 0 && value <= 100000) image[key] = value;
- }
- return image;
- } catch { return null; }
- }
- export function cleanImageLabel(value) {
- return typeof value === 'string' ? [...value.replace(/[\u0000-\u001f\u007f]/g, '').trim()].slice(0, 200).join('') : '';
- }
- export function validMessageImages(value) {
- if (!Array.isArray(value)) return [];
- const seen = new Set(), result = [];
- for (const image of value.slice(0, 100)) {
- if (!image || typeof image.objectId !== 'string' || !AMS_OBJECT_ID.test(image.objectId) || seen.has(image.objectId)) continue;
- seen.add(image.objectId);
- result.push({ objectId: image.objectId, label: cleanImageLabel(image.label) || '图片',
- ...Object.fromEntries(['width', 'height'].filter(k => Number.isInteger(image[k]) && image[k] > 0 && image[k] <= 100000).map(k => [k, image[k]])) });
- }
- return result;
- }
|