CodeSampleX

Ejemplo

golang.org/x/net v0.24.0: html.EscapeString, html.UnescapeString, html.Parse

Muestra verificada para golang golang.org/x/net v0.24.0: html.EscapeString, html.UnescapeString, html.Parse. El contrato se ejecutó en go 1.26 · linux…

sha256:c0057ed72fd15ff9bb0207d28ba4694103b346af39b11da05088620c2837a54b

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado linux 24 · ubuntu · glibc 2.39 x64 go

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-28

Caso

HOW
Objetivo
verify pkg:golang/golang.org/x/net@v0.24.0
Paquetes
Símbolos
  • golang.org/x/net/html.EscapeString
  • golang.org/x/net/html.UnescapeString
  • golang.org/x/net/html.Parse
  • golang.org/x/net/html.Render
Creado
2026-08-28T08:59:15Z

Contrato

  1. html.EscapeString escapes special characters for safe HTML inclusion
  2. html.UnescapeString unescapes HTML entities back to plain text
  3. html.Parse parses an HTML document into a Node tree
  4. html.Render serializes an HTML Node tree back into formatted HTML written to an io.Writer

Archivos

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

Descargar el artefacto de código fuente (tar.gz)

Código fuente

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify pkg:golang/golang.org/x/net@v0.24.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/net@v0.24.0

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:155c54b729d254d6467dbc3eae41f943e3107749022c03d671a41b46f61bb124","contract":["html.EscapeString escapes special characters for safe HTML inclusion","html.UnescapeString unescapes HTML entities back to plain text","html.Parse parses an HTML document into a Node tree","html.Render serializes an HTML Node tree back into formatted HTML written to an io.Writer"],"goal":"verify pkg:golang/golang.org/x/net@v0.24.0","kind":"HOW","packages":["pkg:golang/golang.org/x/net@v0.24.0"],"schemaVersion":1,"symbols":["golang.org/x/net/html.EscapeString","golang.org/x/net/html.UnescapeString","golang.org/x/net/html.Parse","golang.org/x/net/html.Render"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/net@v0.24.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/net@v0.24.0","symbols":["golang.org/x/net/html.EscapeString","golang.org/x/net/html.UnescapeString","golang.org/x/net/html.Parse","golang.org/x/net/html.Render"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.22.0

require golang.org/x/net v0.24.0
go.sum
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
main.go
package main

import (
	"bytes"
	"fmt"
	"log"
	"strings"

	"golang.org/x/net/html"
)

func main() {
	rawHTML := `<!DOCTYPE html><html><head><title>Sample</title></head><body><p class="greeting">Hello &amp; welcome to CodeSampleX!</p></body></html>`

	doc, err := html.Parse(strings.NewReader(rawHTML))
	if err != nil {
		log.Fatalf("failed to parse HTML: %v", err)
	}

	var buf bytes.Buffer
	if err := html.Render(&buf, doc); err != nil {
		log.Fatalf("failed to render HTML: %v", err)
	}
	fmt.Printf("Rendered HTML:\n%s\n", buf.String())

	raw := `<script>alert("hello & welcome")</script>`
	escaped := html.EscapeString(raw)
	fmt.Printf("Escaped: %s\n", escaped)

	unescaped := html.UnescapeString(escaped)
	fmt.Printf("Unescaped: %s\n", unescaped)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/golang.org/x/net@v0.24.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/net@v0.24.0"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"
	"strings"

	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
)

func main() {
	// Assertion 1: html.EscapeString escapes special characters for safe HTML inclusion
	{
		input := `<div class="test" data-val='1 & 2'>Hello & Welcome</div>`
		escaped := html.EscapeString(input)
		expected := `&lt;div class=&#34;test&#34; data-val=&#39;1 &amp; 2&#39;&gt;Hello &amp; Welcome&lt;/div&gt;`
		if escaped != expected {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected %q, got %q\n", expected, escaped)
			os.Exit(1)
		}
	}

	// Assertion 2: html.UnescapeString unescapes HTML entities back to plain text
	{
		input := `&lt;div class=&#34;test&#34;&gt;Hello &amp; Welcome&lt;/div&gt;`
		unescaped := html.UnescapeString(input)
		expected := `<div class="test">Hello & Welcome</div>`
		if unescaped != expected {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected %q, got %q\n", expected, unescaped)
			os.Exit(1)
		}
	}

	// Assertion 3: html.Parse parses an HTML document into a Node tree
	{
		src := `<!DOCTYPE html><html><head><title>Test Page</title></head><body><h1>Header</h1><p id="content">Body paragraph</p></body></html>`
		doc, err := html.Parse(strings.NewReader(src))
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: html.Parse returned error: %v\n", err)
			os.Exit(1)
		}
		if doc == nil || doc.Type != html.DocumentNode {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: expected root DocumentNode, got %v\n", doc)
			os.Exit(1)
		}

		var foundH1, foundP bool
		var crawler func(*html.Node)
		crawler = func(n *html.Node) {
			if n.Type == html.ElementNode {
				if n.DataAtom == atom.H1 || n.Data == "h1" {
					if n.FirstChild != nil && n.FirstChild.Data == "Header" {
						foundH1 = true
					}
				}
				if n.DataAtom == atom.P || n.Data == "p" {
					for _, attr := range n.Attr {
						if attr.Key == "id" && attr.Val == "content" {
							if n.FirstChild != nil && n.FirstChild.Data == "Body paragraph" {
								foundP = true
							}
						}
					}
				}
			}
			for c := n.FirstChild; c != nil; c = c.NextSibling {
				crawler(c)
			}
		}
		crawler(doc)

		if !foundH1 || !foundP {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: DOM structure not parsed correctly: foundH1=%v, foundP=%v\n", foundH1, foundP)
			os.Exit(1)
		}
	}

	// Assertion 4: html.Render serializes an HTML Node tree back into formatted HTML written to an io.Writer
	{
		src := `<!DOCTYPE html><html><head><title>Test Page</title></head><body><h1>Header</h1><p id="content">Body paragraph</p></body></html>`
		doc, err := html.Parse(strings.NewReader(src))
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: parse error: %v\n", err)
			os.Exit(1)
		}

		var buf bytes.Buffer
		if err := html.Render(&buf, doc); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: html.Render returned error: %v\n", err)
			os.Exit(1)
		}
		rendered := buf.String()
		if !strings.Contains(rendered, "<title>Test Page</title>") || !strings.Contains(rendered, `id="content"`) {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: rendered HTML missing expected content: %s\n", rendered)
			os.Exit(1)
		}
	}

	fmt.Println("All contract assertions passed.")
}

Seeder de origen

anónimo