CodeSampleX

Beispiel

marked 17.0.3: Escape double quotes and HTML special characters in Markdown image alt attributes to prevent broken attribute markup

Verifiziertes Beispiel für npm marked 17.0.3: Escape double quotes and HTML special characters in Markdown image alt attributes to prevent broken attribute…

sha256:342c7f8999e5aec74697b609e01159232d2367fd8cf4b76f978861a975f9268d

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
3
Signaturschlüssel, die es gebaut haben
3
Deklarierte Umgebung node linux x64 node node npm

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-16
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-18
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-09-08

Fall

HOW
Ziel
Escape double quotes and HTML special characters in Markdown image alt attributes to prevent broken attribute markup
Pakete
Symbole
  • marked.parse
Umgebung
node
Erstellt
2026-08-17T01:57:37Z

Contract

  1. marked.parse escapes double quotes inside image alt text as " rather than emitting raw unescaped double quotes inside alt attributes
  2. marked.parse escapes angle brackets inside image alt text as < and > to prevent malformed HTML tag syntax

Dateien

  • NOTES.md
  • csx.json
  • package-lock.json
  • package.json
  • scratch_install.mjs
  • scratch_test.mjs
  • setup_node_modules.mjs
  • src/index.mjs
  • test/contract.mjs
  • testmarked-64ZZOC/package-lock.json
  • testmarked-64ZZOC/package.json
  • testmarked-64ZZOC/test.js

Quellartefakt herunterladen (tar.gz)

Quelltext

NOTES.md
# marked Image Alt Attribute Entity Escaping

## Search Results
Calling solution search for `marked` returned sample `sha256:05053573db62da10e081ce38a9b23cbfc2efd0f85c6572af0ca81406f6177895`, which demonstrates basic Markdown rendering and notes that raw HTML input is not sanitized by default.

## Behavioral Trap and Bugfix
When parsing Markdown image markup containing double quotes or angle brackets in alt text (such as `![a "caption" <tag>](https://example.com/photo.png)`), `marked` version 17.0.2 rendered raw unescaped quotes and angle brackets directly inside the `alt` attribute string (`alt="a "caption" <tag>"`). This malformed attribute string broke out of attribute quotes and corrupted the generated HTML tag.

Patch release 17.0.3 introduced entity escaping for image `alt` attributes, encoding double quotes as `&quot;` and angle brackets as `&lt;` and `&gt;` (`alt="a &quot;caption&quot; &lt;tag&gt;"`).

Prior version behavior (17.0.2): Fails silently with a green build, outputting malformed HTML with unescaped double quotes inside the `alt` attribute value.
csx.json
{"case":{"believed":"Markdown image alt text containing double quotes is rendered with HTML entity escaping so the attribute value does not break out of quotes.","caseId":"case:sha256:4ff34959cb5feaf0960b4c35ae5c0dfafa0d0e87905060c66132f218ae25a469","contract":["marked.parse escapes double quotes inside image alt text as \u0026quot; rather than emitting raw unescaped double quotes inside alt attributes","marked.parse escapes angle brackets inside image alt text as \u0026lt; and \u0026gt; to prevent malformed HTML tag syntax"],"goal":"Escape double quotes and HTML special characters in Markdown image alt attributes to prevent broken attribute markup","kind":"HOW","packages":["pkg:npm/marked@17.0.3"],"schemaVersion":1,"symbols":["marked.parse"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"node","os":"linux","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/marked@17.0.3"],"schemaVersion":1,"symbols":["marked.parse"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "marked-alt-attribute-escape",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "marked-alt-attribute-escape",
      "version": "1.0.0",
      "dependencies": {
        "marked": "17.0.3"
      }
    },
    "node_modules/marked": {
      "version": "17.0.3",
      "resolved": "https://registry.npmjs.org/marked/-/marked-17.0.3.tgz",
      "integrity": "sha512-jt1v2ObpyOKR8p4XaUJVk3YWRJ5n+i4+rjQopxvV32rSndTJXvIzuUdWWIy/1pFQMkQmvTXawzDNqOH/CUmx6A==",
      "license": "MIT",
      "bin": {
        "marked": "bin/marked.js"
      },
      "engines": {
        "node": ">= 20"
      }
    }
  }
}
package.json
{
  "name": "marked-alt-attribute-escape",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "dependencies": {
    "marked": "17.0.3"
  }
}
scratch_install.mjs
import fs from 'fs';
import path from 'path';

async function setup() {
  const url = 'https://registry.npmjs.org/marked/-/marked-17.0.3.tgz';
  const res = await fetch(url);
  const buffer = Buffer.from(await res.arrayBuffer());
  const tarPath = 'marked-17.0.3.tgz';
  fs.writeFileSync(tarPath, buffer);
  console.log('Downloaded tarball, size:', buffer.length);
}

setup().catch(console.error);
scratch_test.mjs
import fs from 'fs';
import cp from 'child_process';
import path from 'path';

const tmp = fs.mkdtempSync('testmarked-');
fs.writeFileSync(path.join(tmp, 'test.js'), `
const m1807 = require('./node_modules/marked1807');
const m1808 = require('./node_modules/marked1808');

const ext = {
  renderer: {
    checkbox(token) {
      return false; // return false to fallback to default
    }
  }
};

const marked7 = new m1807.Marked();
marked7.use(ext);

const marked8 = new m1808.Marked();
marked8.use(ext);

console.log('--- 18.0.7 vs 18.0.8 extension checkbox fallback ---');
console.log('18.0.7:', JSON.stringify(marked7.parse('- [x] task')));
console.log('18.0.8:', JSON.stringify(marked8.parse('- [x] task')));
`);

cp.execSync('npm init -y', { cwd: tmp });
cp.execSync('npm i marked1807@npm:marked@18.0.7 marked1808@npm:marked@18.0.8', { cwd: tmp, stdio: 'inherit' });
cp.execSync('node test.js', { cwd: tmp, stdio: 'inherit' });
fs.rmSync(tmp, { recursive: true, force: true });
setup_node_modules.mjs
import fs from 'fs';
import path from 'path';
import zlib from 'zlib';

async function main() {
  const url = 'https://registry.npmjs.org/marked/-/marked-17.0.3.tgz';
  console.log('Fetching', url);
  const res = await fetch(url);
  const arrayBuf = await res.arrayBuffer();
  const buffer = Buffer.from(arrayBuf);
  
  // Decompress gzip
  const decompressed = zlib.gunzipSync(buffer);
  
  // Simple tar parser to extract package/ files into node_modules/marked/
  const targetDir = path.join('node_modules', 'marked');
  fs.mkdirSync(targetDir, { recursive: true });

  let offset = 0;
  while (offset < decompressed.length) {
    const header = decompressed.subarray(offset, offset + 512);
    if (header.every(b => b === 0)) break;
    
    let fileName = header.subarray(0, 100).toString('utf8').replace(/\0/g, '');
    const prefix = header.subarray(345, 500).toString('utf8').replace(/\0/g, '');
    if (prefix) fileName = prefix + '/' + fileName;

    const sizeStr = header.subarray(124, 136).toString('utf8').replace(/\0/g, '').trim();
    const size = parseInt(sizeStr, 8) || 0;
    
    offset += 512;
    if (size > 0) {
      const fileData = decompressed.subarray(offset, offset + size);
      if (fileName.startsWith('package/')) {
        const relPath = fileName.slice('package/'.length);
        if (relPath) {
          const destPath = path.join(targetDir, relPath);
          fs.mkdirSync(path.dirname(destPath), { recursive: true });
          fs.writeFileSync(destPath, fileData);
        }
      }
      offset += Math.ceil(size / 512) * 512;
    }
  }
  console.log('Successfully extracted marked 17.0.3 to node_modules/marked');
}

main().catch(err => {
  console.error(err);
  process.exit(1);
});
src/index.mjs
import { marked } from 'marked';

/**
 * Parses Markdown into HTML using marked.
 * Image alt attributes will have double quotes and angle brackets entity-escaped.
 */
export function renderMarkdown(md) {
  return marked.parse(md);
}
test/contract.mjs
import { strict as assert } from 'node:assert';
import { renderMarkdown } from '../src/index.mjs';

// Markdown image with double quotes and angle brackets in alt text
const input = '![a "caption" <tag>](https://example.com/photo.png)';
const html = renderMarkdown(input);

// 1. Double quotes in alt text must be escaped as &quot; and angle brackets as &lt;/&gt;
// In marked 17.0.2, marked rendered raw unescaped quotes: <img src="..." alt="a "caption" <tag>">
// In marked 17.0.3, marked correctly emits: <img src="..." alt="a &quot;caption&quot; &lt;tag&gt;">
assert.ok(
  html.includes('alt="a &quot;caption&quot; &lt;tag&gt;"'),
  `Expected HTML entity-escaped alt attribute, but got: ${html}`
);

// 2. Unescaped double quotes must not remain inside the alt attribute value
assert.strictEqual(
  html.includes('alt="a "caption"'),
  false,
  'alt attribute contains raw unescaped double quotes breaking HTML attribute syntax'
);

// 3. Verify exact rendered HTML structure
assert.strictEqual(
  html.trim(),
  '<p><img src="https://example.com/photo.png" alt="a &quot;caption&quot; &lt;tag&gt;"></p>'
);

console.log('CONTRACT PASS: marked 17.0.3 entity-escapes image alt text attributes correctly');
testmarked-64ZZOC/package-lock.json
{
  "name": "testmarked-64zzoc",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "testmarked-64zzoc",
      "version": "1.0.0",
      "license": "ISC",
      "dependencies": {
        "marked1804": "npm:marked@^18.0.4",
        "marked1805": "npm:marked@^18.0.5",
        "marked1808": "npm:marked@^18.0.8",
        "marked1809": "npm:marked@^18.0.9"
      }
    },
    "node_modules/marked1804": {
      "name": "marked",
      "version": "18.0.4",
      "resolved": "https://registry.npmjs.org/marked/-/marked-18.0.4.tgz",
      "integrity": "sha512-c/BTaKzg0G6ezQx97DAkYU7k0HM6ys0FqYeKBL6hlBByZwy+ycA1+f0vDdjMHKKeEjdgkx0GOv9Il6D+85cOqA==",
      "license": "MIT",
      "bin": {
        "marked": "bin/marked.js"
      },
      "engines": {
        "node": ">= 20"
      }
    },
    "node_modules/marked1805": {
      "name": "marked",
      "version": "18.0.5",
      "resolved": "https://registry.npmjs.org/marked/-/marked-18.0.5.tgz",
      "integrity": "sha512-S6GcvALHg6K4ohtu4E7x0a1AqhAjp6cV8KhLSyN9qVapnzJkusVBxZRcIU9AeYsbe6P1hKDusSbEOzGyyuce6w==",
      "license": "MIT",
      "bin": {
        "marked": "bin/marked.js"
      },
      "engines": {
        "node": ">= 20"
      }
    },
    "node_modules/marked1808": {
      "name": "marked",
      "version": "18.0.8",
      "resolved": "https://registry.npmjs.org/marked/-/marked-18.0.8.tgz",
      "integrity": "sha512-wsTzDkXm7dJwwTZ5nM2ZJFUsAZwUDPxFAl4MEb35pS14Gn/8KFlV8N5xE/sVIsTcZoYmwbTw/6pSba70mvfDRA==",
      "license": "MIT",
      "bin": {
        "marked": "bin/marked.js"
      },
      "engines": {
        "node": ">= 20"
      }
    },
    "node_modules/marked1809": {
      "name": "marked",
      "version": "18.0.9",
      "resolved": "https://registry.npmjs.org/marked/-/marked-18.0.9.tgz",
      "integrity": "sha512-/Sa4qiiHZxf0/FQdBBowr9q4r10krCwMvpK48FUBdXdUXScDxiQGR9zCPrFgRVR5LU3iySOiIjy09ZQvADir1w==",
      "license": "MIT",
      "bin": {
        "marked": "bin/marked.js"
      },
      "engines": {
        "node": ">= 20"
      }
    }
  }
}
testmarked-64ZZOC/package.json
{
  "name": "testmarked-64zzoc",
  "version": "1.0.0",
  "description": "",
  "main": "test.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "commonjs",
  "dependencies": {
    "marked1804": "npm:marked@^18.0.4",
    "marked1805": "npm:marked@^18.0.5",
    "marked1808": "npm:marked@^18.0.8",
    "marked1809": "npm:marked@^18.0.9"
  }
}
testmarked-64ZZOC/test.js

const m1808 = require('./node_modules/marked1808');
const m1809 = require('./node_modules/marked1809');
const m1804 = require('./node_modules/marked1804');
const m1805 = require('./node_modules/marked1805');

console.log('--- 18.0.8 vs 18.0.9 unmatched strong run before emphasis ---');
console.log('18.0.8:', JSON.stringify(m1808.marked.parse('**foo *bar*')));
console.log('18.0.9:', JSON.stringify(m1809.marked.parse('**foo *bar*')));

console.log('--- 18.0.4 vs 18.0.5 empty list item with trailing space ---');
console.log('18.0.4:', JSON.stringify(m1804.marked.parse('- 
  next')));
console.log('18.0.5:', JSON.stringify(m1805.marked.parse('- 
  next')));

Ursprungs-Seeder

csx-seed