-
Notifications
You must be signed in to change notification settings - Fork 54
Fix Google Fonts corruption from incorrect text/html mime type #2068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ng sanitization Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
| // Handles Google Fonts MIME type detection and override | ||
| // Google Fonts sometimes returns font files with text/html mime type | ||
| // This function detects the actual font format from the file content | ||
| export function handleGoogleFontMimeType(urlObj, mimeType, body, log, meta) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const FONT_HOST_PATTERNS = [
/fonts.gstatic.com$/,
/fonts.googleapis.com$/,
/googleapisserver.com/fonts/i,
/use.typekit.net$/, // Adobe Fonts
/fonts.adobe.com$/,
];
function isKnownFontProvider(urlObj) {
return FONT_HOST_PATTERNS.some(pattern =>
pattern.test(urlObj.hostname + urlObj.pathname)
);
}
something like this i was saying
FONT_HOST_PATTERNS -> this array you can get it from env or config file, i will say add 1 config under discovery
discovery:
fonts-domains:
- fonts.gstatic.com
- adobefonts.com- default you can add fonts.gstatic.com
These are some of the font provider
https://fonts.googleapis.com/css | Free, large library, easy embed
-- | --
- Bunny Fonts | https://fonts.bunny.net/css | Privacy-focused, Google Fonts alternative
- Cloudflare Fonts | https://fonts.cloudflare.com | Serve fonts from your domain, privacy
- CDNFonts | https://www.cdnfonts.com | 30,000+ free fonts, CDN hosted
Font Awesome | https://kit.fontawesome.com | Icon fonts, free & paid options
- Adobe Fonts | https://use.typekit.com | Premium fonts, cloud hosted
Typography.com | https://typography.com/webfonts
- https://fonts.googleapis.com/css Free, large library, easy embed
- Bunny Fonts https://fonts.bunny.net/css Privacy-focused, Google Fonts alternative
- Cloudflare Fonts https://fonts.cloudflare.com/ Serve fonts from your domain, privacy
- CDNFonts https://www.cdnfonts.com/ 30,000+ free fonts, CDN hosted
- Font Awesome https://kit.fontawesome.com/ Icon fonts, free & paid options
- Adobe Fonts https://use.typekit.com/ Premium fonts, cloud hosted
Typography.com https://typography.com/webfonts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This you can push in followup PR
| const detectedFontMime = detectFontMimeType(body); | ||
| if (detectedFontMime) { | ||
| mimeType = detectedFontMime; | ||
| log.debug(`- Detected Google Font as ${detectedFontMime} from content, overriding mime type`, meta); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| log.debug(`- Detected Google Font as ${detectedFontMime} from content, overriding mime type`, meta); | |
| log.warn(`- Detected Google Font as ${detectedFontMime} with incorrect mimeType ${originalmimeType}`, meta); | |
| } | ||
|
|
||
| // Check for OpenType signature: 'OTTO' | ||
| if (header === 'OTTO') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
header.toLowerCase() === 'otto'
in case header comes in lower, this will not get detected
| } | ||
|
|
||
| // Read the first 4 bytes as the file header | ||
| const header = buffer.slice(0, 4).toString('binary'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- you can normalize header here, in lowerCase itself
| // Returns the detected MIME type or null if not a recognized font format | ||
| export function detectFontMimeType(buffer) { | ||
| try { | ||
| // Ensure buffer has at least 4 bytes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in follow up PR, add a comment
- For checking a valid font file, the first 4 characters are font type
this-is-shivamsingh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Approved, with conditional comments for follow up PR, before stable release
| if (header === 'wOFF') { | ||
| return 'font/woff'; | ||
| } | ||
|
|
||
| // Check for WOFF2 signature: 'wOF2' | ||
| if (header === 'wOF2') { | ||
| return 'font/woff2'; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (header === 'wOFF') { | |
| return 'font/woff'; | |
| } | |
| // Check for WOFF2 signature: 'wOF2' | |
| if (header === 'wOF2') { | |
| return 'font/woff2'; | |
| } | |
| obj = { | |
| 'wof2': 'font/woff2', | |
| 'otto': 'fonts/woff', | |
| '0x000x010x000x00': 'font/ttf' | |
| } | |
| if obj[header]: | |
| return obj[header] | |
| elseif obj[header.[first 4 char code]: | |
| return obj[header[first 4 char code] | |
| return null |
f41fecf to
0f1146f
Compare
|
@this-is-shivamsingh ACK. |
What does this PR do?
Fixes Google Fonts being corrupted when
fonts.gstatic.comreturns font files with incorrectcontent-type: text/htmlheader.Changes
detectFontMimeType()utility function inpackages/core/src/utils.jsthat uses magic bytes detection to identify font formats (WOFF, WOFF2, TTF, OTF)saveResponseResource()inpackages/core/src/network.jsto detect Google Fonts served withtext/htmlmime type and override with the actual font mime typedetectFontMimeType()utilityWhy?
Google Fonts API sometimes returns font files with
content-type: text/htmlinstead of the correctfont/*mime type. This causes Percy CLI to process these font files as HTML, leading to corruption of the binary font data.Jira Ticket
https://browserstack.atlassian.net/browse/PPLT-4742
Related PRs