typeface: stricter outlines, any fvar is variable, private caching
Review fixes, per the controller's rulings: - WOFF: every table must inflate to exactly its origLength, not just the four Inspect reads (a corrupt glyf stream is now refused). - Outlines: OTTO needs a CFF or CFF2 table; TrueType needs glyf and loca. Deeper checks stay with the browser and the app's parser. - Any fvar makes a face Variable; WeightMin/Max still come from a wght axis when there is one, else equal Weight. - Pictures is false when fsType has 0x0100 (no subsetting), since the PDF embeds subsets. - Headers sends Cache-Control: private, max-age=31536000, immutable.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+77
−30
typeface/typeface.go+26 −15typeface/typeface_test.go+27 −4typeface/woff.go+24 −11
diff --git a/typeface/typeface.go b/typeface/typeface.go| index 70f56b1..49537c8 100644 |
| --- a/typeface/typeface.go |
| +++ b/typeface/typeface.go |
| @@ -68,7 +68,7 @@ type Face struct { |
| WeightMin int // = Weight unless variable |
| WeightMax int // = Weight unless variable |
| Italic bool // OS/2 fsSelection bit 0 or head macStyle bit 1 |
| - Variable bool // fvar present with a 'wght' axis |
| + Variable bool // fvar present (any axis) |
| FSType uint16 // OS/2 fsType, the embedding flags |
| } |
| @@ -206,13 +206,18 @@ func readSFNT(data []byte) (fontTables, error) { |
| // describe turns the tables into a Face (all but Ref and Format). |
| func describe(ft fontTables) (Face, error) { |
| var f Face |
| - switch { |
| - case ft.version == sigOTTO || ft.has["CFF "] || ft.has["CFF2"]: |
| + // Outlines must be there; checking them deeper is left to the |
| + // browser's sanitiser and to the app's own parser when it uses them. |
| + if ft.version == sigOTTO { |
| + if !ft.has["CFF "] && !ft.has["CFF2"] { |
| + return Face{}, malformed("OTTO with no CFF or CFF2 table") |
| + } |
| f.Outline = CFF |
| - case ft.has["glyf"]: |
| + } else { |
| + if !ft.has["glyf"] || !ft.has["loca"] { |
| + return Face{}, malformed("TrueType with no glyf or loca table") |
| + } |
| f.Outline = TrueType |
| - default: |
| - return Face{}, malformed("no glyf or CFF outlines") |
| } |
| head, ok := ft.data["head"] |
| @@ -254,13 +259,15 @@ func describe(ft fontTables) (Face, error) { |
| return Face{}, malformed("no family name") |
| } |
| + // Any fvar makes the face variable; only a wght axis widens its |
| + // weight range. |
| if fv, ok := ft.data["fvar"]; ok { |
| - lo, hi, isVar, err := weightAxis(fv) |
| + f.Variable = true |
| + lo, hi, hasWght, err := weightAxis(fv) |
| if err != nil { |
| return Face{}, err |
| } |
| - if isVar { |
| - f.Variable = true |
| + if hasWght { |
| f.WeightMin, f.WeightMax = lo, hi |
| } |
| } |
| @@ -426,15 +433,17 @@ func nameRank(k nameKey) int { |
| // Pictures reports whether the app's own layout and PDF engines may use |
| // the face: a static, TrueType-outline, bare sfnt (not WOFF or WOFF2) |
| -// whose fsType allows embedding it. fsType 0 (installable), |
| +// whose fsType allows embedding a subset of it. fsType 0 (installable), |
| // 0x0004 (preview and print) and 0x0008 (editable) allow it; the |
| -// restricted-licence bit (0x0002) and bitmap-only (0x0200) do not, even |
| -// alongside a less restrictive bit. |
| +// restricted-licence bit (0x0002), no-subsetting (0x0100, since the PDF |
| +// embeds subsets) and bitmap-only (0x0200) do not, even alongside a less |
| +// restrictive bit. |
| func (f Face) Pictures() bool { |
| return !f.Variable && |
| f.Outline == TrueType && |
| (f.Format == TTF || f.Format == OTF) && |
| f.FSType&0x0002 == 0 && |
| + f.FSType&0x0100 == 0 && |
| f.FSType&0x0200 == 0 |
| } |
| @@ -458,12 +467,14 @@ func contentType(f Format) string { |
| } |
| // Headers sets the headers for serving a font file by its ref: its media |
| -// type, no sniffing, a year's immutable caching (the ref is the file's |
| -// hash, so its bytes never change) and a policy that lets it load nothing. |
| +// type, no sniffing, a year's immutable caching in the browser only (the |
| +// ref is the file's hash, so its bytes never change; private, because a |
| +// person's fonts are theirs and shared caches should not keep them) and a |
| +// policy that lets it load nothing. |
| func Headers(w http.ResponseWriter, f Format) { |
| h := w.Header() |
| h.Set("Content-Type", contentType(f)) |
| h.Set("X-Content-Type-Options", "nosniff") |
| - h.Set("Cache-Control", "public, max-age=31536000, immutable") |
| + h.Set("Cache-Control", "private, max-age=31536000, immutable") |
| h.Set("Content-Security-Policy", "default-src 'none'") |
| } |
diff --git a/typeface/typeface_test.go b/typeface/typeface_test.go| index 7006c0b..8d338c5 100644 |
| --- a/typeface/typeface_test.go |
| +++ b/typeface/typeface_test.go |
| @@ -466,8 +466,8 @@ func TestInspectReadsCFFAndVariable(t *testing.T) { |
| } |
| s := mustInspect(t, variable(t, axis{"wdth", 75, 100, 100})) |
| - if s.Variable || s.WeightMin != 400 || s.WeightMax != 400 { |
| - t.Errorf("fvar with no wght axis: %+v; want static at 400", s) |
| + if !s.Variable || s.WeightMin != 400 || s.WeightMax != 400 { |
| + t.Errorf("fvar with no wght axis: %+v; want variable, weight range 400..400", s) |
| } |
| } |
| @@ -480,12 +480,14 @@ func TestPicturesOnlyForStaticTrueTypeThatMayEmbed(t *testing.T) { |
| {"ttf, installable", liberation(t), true}, |
| {"preview and print", patched(t, 0x0004, 0), true}, |
| {"editable", patched(t, 0x0008, 0), true}, |
| - {"editable, no subsetting", patched(t, 0x0108, 0), true}, |
| + {"editable, no subsetting", patched(t, 0x0108, 0), false}, |
| + {"installable, no subsetting", patched(t, 0x0100, 0), false}, |
| {"restricted licence", patched(t, 0x0002, 0), false}, |
| {"restricted with preview and print", patched(t, 0x0006, 0), false}, |
| {"bitmap embedding only", patched(t, 0x0200, 0), false}, |
| {"cff", cff(t), false}, |
| {"variable", variable(t, axis{"wght", 100, 400, 900}), false}, |
| + {"variable in width only", variable(t, axis{"wdth", 75, 100, 100}), false}, |
| {"woff", buildWOFF(t, liberation(t)), false}, |
| {"woff2", buildWOFF2(t, liberation(t)), false}, |
| } { |
| @@ -507,7 +509,7 @@ func TestHeaders(t *testing.T) { |
| for k, v := range map[string]string{ |
| "Content-Type": ctype, |
| "X-Content-Type-Options": "nosniff", |
| - "Cache-Control": "public, max-age=31536000, immutable", |
| + "Cache-Control": "private, max-age=31536000, immutable", |
| "Content-Security-Policy": "default-src 'none'", |
| } { |
| if h.Get(k) != v { |
| @@ -565,6 +567,22 @@ func TestInspectRefuses(t *testing.T) { |
| } |
| } |
| + badGlyf := bytes.Clone(woff) |
| + { |
| + // Corrupt the glyf table's compressed bytes: a table Inspect |
| + // does not otherwise read. |
| + n := int(binary.BigEndian.Uint16(woff[12:])) |
| + for i := 0; i < n; i++ { |
| + rec := woff[44+20*i:] |
| + if string(rec[:4]) == "glyf" { |
| + off := binary.BigEndian.Uint32(rec[4:]) |
| + for j := off + 2; j < off+40; j++ { |
| + badGlyf[j] ^= 0xA5 |
| + } |
| + } |
| + } |
| + } |
| + |
| v, ts := tablesOf(t, ttf) |
| shortFvar := fvar(axis{"wght", 100, 400, 900}) |
| binary.BigEndian.PutUint16(shortFvar[8:], 40) // 40 axes, room for one |
| @@ -587,9 +605,14 @@ func TestInspectRefuses(t *testing.T) { |
| {"no name", buildSFNT(v, withoutTable(ts, "name")), ErrMalformed}, |
| {"no family name", named(t, nameRec{3, 1, 0x409, 2, "Regular"}), ErrMalformed}, |
| {"no outlines", buildSFNT(v, withoutTable(ts, "glyf", "loca")), ErrMalformed}, |
| + {"a TrueType without glyf", buildSFNT(v, withoutTable(ts, "glyf")), ErrMalformed}, |
| + {"a TrueType without loca", buildSFNT(v, withoutTable(ts, "loca")), ErrMalformed}, |
| + {"an OTTO without CFF", buildSFNT(0x4F54544F, withoutTable(ts, "glyf", "loca")), ErrMalformed}, |
| + {"an OTTO with glyf but no CFF", buildSFNT(0x4F54544F, ts), ErrMalformed}, |
| {"an fvar shorter than its axes", buildSFNT(v, withTable(ts, "fvar", shortFvar)), ErrMalformed}, |
| {"a truncated WOFF", woff[:len(woff)/2], ErrMalformed}, |
| {"a WOFF whose zlib is bad", badZlib, ErrMalformed}, |
| + {"a WOFF whose glyf stream is corrupt", badGlyf, ErrMalformed}, |
| {"a WOFF2 whose brotli stream is bad", assembleWOFF2(flavor, es, badStream), ErrMalformed}, |
| {"a WOFF2 whose brotli stream is cut short", shortStream, ErrMalformed}, |
| {"a WOFF2 table past the stream", assembleWOFF2(flavor, longer, good), ErrMalformed}, |
diff --git a/typeface/woff.go b/typeface/woff.go| index f5c1c09..1c6c4fa 100644 |
| --- a/typeface/woff.go |
| +++ b/typeface/woff.go |
| @@ -15,8 +15,9 @@ import ( |
| // sfnt. |
| // readWOFF reads a WOFF 1.0 file (zlib per table). Every table must lie |
| -// inside the file; the tables Inspect reads are inflated, each to exactly |
| -// the length the directory gives it. |
| +// inside the file and inflate to exactly the length the directory gives |
| +// it (or be stored, when its packed and unpacked lengths are equal); only |
| +// the tables Inspect reads are kept. |
| func readWOFF(data []byte) (fontTables, error) { |
| const headerLen, entryLen = 44, 20 |
| if len(data) < headerLen { |
| @@ -57,31 +58,43 @@ func readWOFF(data []byte) (fontTables, error) { |
| return fontTables{}, malformed("table %q appears twice", tag) |
| } |
| ft.has[tag] = true |
| - if !wanted(tag) { |
| - continue |
| - } |
| packed := data[off : off+compLen] |
| if compLen == origLen { |
| - ft.data[tag] = packed // stored, not compressed |
| + if wanted(tag) { |
| + ft.data[tag] = packed // stored, not compressed |
| + } |
| continue |
| } |
| - b, err := inflate(packed, int(origLen)) |
| + b, err := inflate(packed, int(origLen), wanted(tag)) |
| if err != nil { |
| return fontTables{}, malformed("table %q: %v", tag, err) |
| } |
| - ft.data[tag] = b |
| + if wanted(tag) { |
| + ft.data[tag] = b |
| + } |
| } |
| return ft, nil |
| } |
| -// inflate unpacks a zlib stream that must hold exactly n bytes. |
| -func inflate(packed []byte, n int) ([]byte, error) { |
| +// inflate unpacks a zlib stream that must hold exactly n bytes, returning |
| +// them if keep is set and otherwise only checking them. |
| +func inflate(packed []byte, n int, keep bool) ([]byte, error) { |
| r, err := zlib.NewReader(bytes.NewReader(packed)) |
| if err != nil { |
| return nil, err |
| } |
| defer r.Close() |
| - return readExactly(r, n) |
| + if keep { |
| + return readExactly(r, n) |
| + } |
| + got, err := io.Copy(io.Discard, io.LimitReader(r, int64(n)+1)) |
| + if err != nil { |
| + return nil, err |
| + } |
| + if got != int64(n) { |
| + return nil, fmt.Errorf("unpacks to %d bytes, not %d", got, n) |
| + } |
| + return nil, nil |
| } |
| // readExactly reads n bytes from r and checks that r then ends cleanly. |