protected internal void SetIncludes(AppConf conf, LayoutModel layoutModel) { Args.ThrowIfNull(conf, "AppConf"); Args.ThrowIfNull(conf.BamConf, "BamConf"); Args.ThrowIfNull(conf.BamConf.ContentRoot, "ContentRoot"); ApplicationServiceRegistry reg = ApplicationServiceRegistry.ForApplication(conf.Name); IIncludesResolver includesResolver = reg.Get <IIncludesResolver>(); Includes commonIncludes = new Includes(); if (IncludeCommon) { commonIncludes = includesResolver.ResolveCommonIncludes(conf.BamConf.ContentRoot); } Includes appIncludes = includesResolver.ResolveApplicationIncludes(conf.Name, conf.BamConf.ContentRoot); Includes combined = commonIncludes.Combine(appIncludes); StringBuilder styleSheetLinkTags = new StringBuilder(); foreach (string css in combined.Css) { styleSheetLinkTags.AppendLine(StyleSheetLinkTag.For(css).Render()); } layoutModel.StyleSheetLinkTags = styleSheetLinkTags.ToString(); StringBuilder scriptLinkTags = new StringBuilder(); foreach (string script in combined.Scripts) { scriptLinkTags.Append(ScriptTag.For(script).Render()); } layoutModel.ScriptTags = scriptLinkTags.ToString(); }
private IEnumerable <ScriptTag> GetScriptTags(string cshtmlFile) { string contents; using (var reader = new StreamReader(File.OpenRead(cshtmlFile))) { contents = reader.ReadToEnd(); } var match = _scriptRegex.Match(contents); while (match != null && match != Match.Empty) { var tag = new ScriptTag { Src = match.Groups["src"].Value, Integrity = match.Groups["integrity"].Value, FileName = Path.GetFileName(cshtmlFile), Path = cshtmlFile, }; yield return(tag); _output.WriteLine($"Found script tag in '{tag.FileName}', src='{tag.Src}' integrity='{tag.Integrity}'"); match = match.NextMatch(); } }
public static async Task <string> GetVidDownloadLink(string url) { try { // Get All Iframe Elements string pageSource = getSource(url); List <FrameTag> frames = await Parser.GetFrames(pageSource); if (frames == null || frames.Count < 1) { return(null); } // Use First IFrame element FrameTag selectedFrame = null; foreach (FrameTag frame in frames) { if (frame.Title == "MP4Upload") { selectedFrame = frame; break; } } if (string.IsNullOrEmpty(selectedFrame?.Src)) { return(null); } // Get All Script Elements string frameSource = getSource(selectedFrame.Src); List <ScriptTag> scripts = await Parser.GetScripts(frameSource); if (scripts == null || scripts.Count < 1) { return(null); } // Get Video Script ScriptTag selectedScript = null; foreach (ScriptTag script in scripts) { if (script.InnerHtml.Contains("var|player")) { selectedScript = script; break; } } if (string.IsNullOrEmpty(selectedScript?.InnerHtml)) { return(null); } return(assembleDownloadLink(selectedScript.InnerHtml)); } catch (WebException) { throw; } }
private async Task <string> GetShaIntegrity(ScriptTag scriptTag, HashAlgorithm algorithm, string prefix) { using (var respStream = await _httpClient.GetStreamAsync(scriptTag.Src)) using (var alg = SHA256.Create()) { var hash = alg.ComputeHash(respStream); return($"{prefix}-" + Convert.ToBase64String(hash)); } }
public async Task CheckScriptSubresourceIntegrity(ScriptTag scriptTag) { var expectedIntegrity = await GetShaIntegrity(scriptTag); if (!string.Equals(expectedIntegrity, scriptTag.Integrity, StringComparison.OrdinalIgnoreCase)) { Assert.False(true, $"Expected {scriptTag.Src} to have Integrity '{expectedIntegrity}' but it had '{scriptTag.Integrity}'."); } }
/// <summary> /// </summary> /// <param name="tag"></param> /// <param name="innerException"></param> public ScriptTagException(ScriptTag tag, Exception innerException) : base(tag, innerException) { // validate arguments if (tag == null) throw new ArgumentNullException("tag"); // add to trace ScriptStackTrace.Add(tag); }
private ScriptTag Read_ScriptTag(BinaryReader reader) { var result = new ScriptTag(); result.Data = ReadString(reader); result.Value = ReadString(reader); return(result); }
public void has_a_cdn_and_fallback() { theAsset.CdnUrl = "http://cdn.me.com/foo.js"; theAsset.FallbackTest = "foo == null"; var tag = new ScriptTag(x => x, theAsset); tag.ToString().ShouldContain("<script>if (foo == null)"); }
public async Task IdentityUI_ScriptTags_FallbackSourceContent_Matches_CDNContent(ScriptTag scriptTag) { var wwwrootDir = Path.Combine(GetProjectBasePath(), "assets", scriptTag.Version); var cdnContent = await _httpClient.GetStringAsync(scriptTag.Src); var fallbackSrcContent = File.ReadAllText( Path.Combine(wwwrootDir, scriptTag.FallbackSrc.Replace("Identity", "").TrimStart('~').TrimStart('/'))); Assert.Equal(RemoveLineEndings(cdnContent), RemoveLineEndings(fallbackSrcContent)); }
private async Task <string> GetShaIntegrity(ScriptTag scriptTag) { var prefix = scriptTag.Integrity.Substring(0, 6); using (var respStream = await _httpClient.GetStreamAsync(scriptTag.Src)) using (HashAlgorithm alg = string.Equals(prefix, "sha256") ? (HashAlgorithm)SHA256.Create() : (HashAlgorithm)SHA384.Create()) { var hash = alg.ComputeHash(respStream); return($"{prefix}-" + Convert.ToBase64String(hash)); } }
///<summary> ///</summary> ///<param name="attributeName"></param> ///<param name="scriptTag"></param> public AttributeOutOfRangeException(string attributeName, ScriptTag scriptTag) { // validate arguments if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("attributeName"); if (scriptTag == null) throw new ArgumentNullException("scriptTag"); // format the exception message message = string.Format("The attribute '{0}' is out of range on tag of type '{1}'", attributeName, scriptTag.GetType()); }
///<summary> ///</summary> ///<param name="attributeName"></param> ///<param name="scriptTag"></param> public AttributeNullException(string attributeName, ScriptTag scriptTag) { // validate arguments if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("attributeName"); if (scriptTag == null) throw new ArgumentNullException("scriptTag"); // format the exception message message = string.Format("The value of attribute '{0}' can not be null or empty on tag of type '{1}'", attributeName, scriptTag.GetType()); }
///<summary> ///</summary> ///<param name="attributeName"></param> ///<param name="scriptTag"></param> public AttributeNotSpecifiedException(string attributeName, ScriptTag scriptTag) { // validate arguments if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("attributeName"); if (scriptTag == null) throw new ArgumentNullException("scriptTag"); // format the exception message message = string.Format("Could not find required attribute '{0}' on tag of type '{1}'", attributeName, scriptTag.GetType()); }
public async Task FallbackSrcContent_Matches_CDNContent(ScriptTag scriptTag) { var fallbackSrc = scriptTag.FallbackSrc .TrimStart('~') .TrimStart('/'); var cdnContent = await _httpClient.GetStringAsync(scriptTag.Src); var fallbackSrcContent = GetFileContentFromArchive(scriptTag, fallbackSrc); Assert.Equal(RemoveLineEndings(cdnContent), RemoveLineEndings(fallbackSrcContent)); }
public async Task CheckSubresourceIntegrity(ScriptTag scriptTag) { string expectedIntegrity; using (var responseStream = await _httpClient.GetStreamAsync(scriptTag.Src)) using (var alg = SHA384.Create()) { var hash = alg.ComputeHash(responseStream); expectedIntegrity = "sha384-" + Convert.ToBase64String(hash); } Assert.Equal(expectedIntegrity, scriptTag.Integrity); }
public async Task <bool> InstallAsync(string shopUrl, string accessCode) { ShopService shopService = null; string accessToken = null; try { accessToken = await AuthorizationService.Authorize(accessCode, shopUrl, _shopifyConfig.ApiKey, _shopifyConfig.SecretKey); _logger.LogInformation($"accessToken: {accessToken}"); if (string.IsNullOrEmpty(accessToken)) { return(false); } shopService = new ShopService(shopUrl, accessToken); Shop shop = await shopService.GetAsync(); string scriptUrl = _shopifyConfig.ScriptUrl; var scriptService = new ScriptTagService(shopUrl, accessToken); var existingScripts = await scriptService.ListAsync(); foreach (var s in existingScripts) { if (s.Id.HasValue) { await scriptService.DeleteAsync(s.Id.Value); } } ScriptTag newScriptTag = await scriptService.CreateAsync(new ScriptTag { Src = scriptUrl, Event = _shopifyConfig.Event }); return(true); } catch (Exception ex) { _logger.LogError(ex, ""); await TryUninstallAsync(shopService, accessToken); return(false); } }
public async Task CheckScriptSubresourceIntegrity(ScriptTag scriptTag) { string expectedIntegrity; using (var responseStream = await _httpClient.GetStreamAsync(scriptTag.Src)) using (var alg = SHA256.Create()) { var hash = alg.ComputeHash(responseStream); expectedIntegrity = "sha256-" + Convert.ToBase64String(hash); } if (!expectedIntegrity.Equals(scriptTag.Integrity, StringComparison.OrdinalIgnoreCase)) { Assert.False(true, $"Expected {scriptTag.Src} to have Integrity '{expectedIntegrity}' but it had '{scriptTag.Integrity}'."); } }
private static string GetFileContentFromArchive(ScriptTag scriptTag, string relativeFilePath) { var file = MondoHelpers.GetNupkgFiles().Single(f => f.EndsWith(scriptTag.FileName)); using (var zip = new ZipArchive(File.OpenRead(file), ZipArchiveMode.Read, leaveOpen: false)) { var entry = zip.Entries .Where(e => e.FullName.EndsWith(relativeFilePath, StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(); if (entry != null) { using (var reader = new StreamReader(entry.Open())) { return(reader.ReadToEnd()); } } } return(null); }
private async Task <string> GetShaIntegrity(ScriptTag scriptTag) { var isSha256 = scriptTag.Integrity.StartsWith("sha256"); var prefix = isSha256 ? "sha256" : "sha384"; using (var respStream = await _httpClient.GetStreamAsync(scriptTag.Src)) using (var alg256 = SHA256.Create()) using (var alg384 = SHA384.Create()) { byte[] hash; if (isSha256) { hash = alg256.ComputeHash(respStream); } else { hash = alg384.ComputeHash(respStream); } return($"{prefix}-" + Convert.ToBase64String(hash)); } }
public ScriptTag GetById(int id, int storeId) { ScriptTag scriptTag = null; var request = new RestRequest("/script_tags/{id}", Method.GET); request.AddUrlSegment("id", id.ToString()); var response = ApiClient.Execute <ScriptTagResponse>(request); switch (response.StatusCode) { case HttpStatusCode.OK: scriptTag = response.Data.ScriptTag; break; default: break; } return(scriptTag); }
private void ReadIndex() { string text = File.ReadAllText(indexPath); Lexer lexer = new Lexer(text); Parser parser = new Parser(lexer); NodeList htmlNodes = parser.Parse(new OrFilter(new NodeClassFilter(typeof(RemarkNode)), new NodeClassFilter(typeof(ScriptTag)))); MergeJSData mergeJSData = null; for (int i = 0; i < htmlNodes.Count; i++) { var node = htmlNodes[i]; ScriptTag script = node as ScriptTag; RemarkNode remark = node as RemarkNode; if (remark != null) { string remarkText = remark.GetText(); if (remarkText.StartsWith("MergeJSBegin")) { mergeJSData = new MergeJSData(); mergeJSData.name = remarkText.Split(":")[1].Trim(); list.Add(mergeJSData); } else if (remarkText.StartsWith("MergeJSEnd")) { mergeJSData = null; } } if (script != null) { if (mergeJSData != null) { mergeJSData.AddPath(script.GetAttribute("src")); } } } }
public static Tag Parse(byte[] headerBytes, byte[] bodyBytes) { TagType tagType = (TagType)(headerBytes[0] & 0b00011111); Tag tagBase; switch (tagType) { case TagType.Audio: tagBase = new AudioTag(headerBytes, bodyBytes); break; case TagType.Video: tagBase = new VideoTag(headerBytes, bodyBytes); break; case TagType.Script: tagBase = new ScriptTag(headerBytes, bodyBytes); break; default: throw new UnsupportedFormat(string.Format("Unsupported Tag type 0x{0}", ((uint)tagType).ToString("X2"))); } return(tagBase); }
public void Run(IndexType indexType) { this.indexType = indexType; string indexTmp = null; switch (indexType) { case IndexType.Merge: indexTmp = Setting.Options.binCache + "/index-merge.html"; break; case IndexType.Min: indexTmp = Setting.Options.binCache + "/index-min.html"; break; } string indexSave = null; switch (indexType) { case IndexType.Merge: indexSave = Setting.Options.binRelease + "/index-merge.html"; break; case IndexType.Min: indexSave = Setting.Options.binRelease + "/index-min.html"; break; } string indexHtml = Setting.Options.binRelease + "/index.html"; if (File.Exists(indexHtml)) { if (File.ReadAllLines(indexHtml).Length > 100) { string indexBak = Setting.Options.binRelease + "/index-src.html"; PathHelper.CheckPath(indexBak); if (File.Exists(indexBak)) { File.Delete(indexBak); } File.Copy(indexHtml, indexBak); } } string text = File.ReadAllText(indexTmp); Lexer lexer = new Lexer(text); Parser parser = new Parser(lexer); NodeList htmlNodes = parser.Parse(new NodeClassFilter(typeof(ScriptTag))); List <string> folders = new List <string> (); for (int i = 0; i < htmlNodes.Count; i++) { var node = htmlNodes[i]; ScriptTag script = node as ScriptTag; if (script != null) { string key = script.GetAttribute("src").Trim(); key = Path.GetDirectoryName(key); if (folders.IndexOf(key) == -1) { Console.WriteLine("Folder:" + key); folders.Add(key); } } } List <string> list = new List <string>(); foreach (string folder in folders) { string path = Setting.Options.binRelease + "/" + folder; if (!Directory.Exists(path)) { Console.WriteLine($"不存在目录 {path}"); continue; } string[] files = Directory.GetFiles(path, "*.js"); list.AddRange(files); } Dictionary <string, long> dateDict = new Dictionary <string, long>(); Dictionary <string, string> dict = new Dictionary <string, string>(); foreach (string path in list) { FileInfo fileInfo = new FileInfo(path); long time = fileInfo.LastWriteTime.ToFileTime(); string js = path.Replace("\\", "/").Replace(Setting.Options.binRelease + "/", ""); string key = js; key = key.Substring(0, key.Length - 11) + ".js"; if (dateDict.ContainsKey(key)) { if (time > dateDict[key]) { dateDict[key] = time; dict[key] = js; } } else { dateDict.Add(key, time); dict.Add(key, js); } } for (int i = 0; i < htmlNodes.Count; i++) { var node = htmlNodes[i]; ScriptTag script = node as ScriptTag; if (script != null) { Console.WriteLine(script.GetAttribute("src")); string key = script.GetAttribute("src").Trim(); if (dict.ContainsKey(key)) { text = text.Replace(key, dict[key]); } } } File.WriteAllText(indexSave, text); File.WriteAllText(indexHtml, text); }
public async Task IdentityUI_ScriptTags_SubresourceIntegrityCheck(ScriptTag scriptTag) { var integrity = await GetShaIntegrity(scriptTag); Assert.Equal(scriptTag.Integrity, integrity); }
internal static FlvTag ReadTag(Stream stream) { try { FlvTag tag; byte[] buffer = new byte[4]; int rtn; rtn = stream.Read(buffer, 0, 4); if (rtn <= 0) { return null; } int type = stream.ReadByte(); if (type == 8) tag = new AudioTag(); else if (type == 9) tag = new VideoTag(); else if (type == 0x12) tag = new ScriptTag(); else tag = new FlvTag(); tag.presize = ByteUtil.ByteToUInt(buffer, 4); tag.tagtype = type; tag.datasize = ByteUtil.ReadUI24(stream); tag.timestamp = ByteUtil.ReadUI24(stream); tag.timestamp_ex = stream.ReadByte(); tag.streamid = ByteUtil.ReadUI24(stream); tag.offset = stream.Position; if (tag is ScriptTag) { (tag as ScriptTag).ReadScript(stream); stream.Seek(tag.offset + tag.DataSize, SeekOrigin.Begin); } else if (tag is AudioTag) { rtn = stream.Read(buffer, 0, 1); if (rtn <= 0) return null; tag.taginfo = buffer[0]; stream.Seek(tag.DataSize - 1, SeekOrigin.Current); } else if (tag is VideoTag) { rtn = stream.Read(buffer, 0, 2); if (rtn <= 0) return null; tag.taginfo = buffer[0]; tag.avcpaktype = buffer[1]; stream.Seek(tag.DataSize - 2, SeekOrigin.Current); } return tag; } catch { return null; } }
internal static FlvTag ReadTag(Stream stream) { try { FlvTag tag; byte[] buffer = new byte[4]; int rtn; rtn = stream.Read(buffer, 0, 4); if (rtn <= 0) { return(null); } int type = stream.ReadByte(); if (type == 8) { tag = new AudioTag(); } else if (type == 9) { tag = new VideoTag(); } else if (type == 0x12) { tag = new ScriptTag(); } else { tag = new FlvTag(); } tag.presize = ByteUtil.ByteToUInt(buffer, 4); tag.tagtype = type; tag.datasize = ByteUtil.ReadUI24(stream); tag.timestamp = ByteUtil.ReadUI24(stream); tag.timestamp_ex = stream.ReadByte(); tag.streamid = ByteUtil.ReadUI24(stream); tag.offset = stream.Position; if (tag is ScriptTag) { (tag as ScriptTag).ReadScript(stream); stream.Seek(tag.offset + tag.DataSize, SeekOrigin.Begin); } else if (tag is AudioTag) { rtn = stream.Read(buffer, 0, 1); if (rtn <= 0) { return(null); } tag.taginfo = buffer[0]; stream.Seek(tag.DataSize - 1, SeekOrigin.Current); } else if (tag is VideoTag) { rtn = stream.Read(buffer, 0, 2); if (rtn <= 0) { return(null); } tag.taginfo = buffer[0]; tag.avcpaktype = buffer[1]; stream.Seek(tag.DataSize - 2, SeekOrigin.Current); } return(tag); } catch { return(null); } }
public async Task IdentityUI_ScriptTags_FallbackSourceContent_Matches_CDNContent(ScriptTag scriptTag) { var slnDir = GetSolutionDir(); var wwwrootDir = Path.Combine(slnDir, "UI", "src", "wwwroot", scriptTag.Version); var cdnContent = await _httpClient.GetStringAsync(scriptTag.Src); var fallbackSrcContent = File.ReadAllText( Path.Combine(wwwrootDir, scriptTag.FallbackSrc.TrimStart('~').TrimStart('/'))); Assert.Equal(RemoveLineEndings(cdnContent), RemoveLineEndings(fallbackSrcContent)); }
public void Visit(ScriptTag host) { throw new NotImplementedException(); }
private Task <string> GetShaIntegrity(ScriptTag scriptTag) { return(GetShaIntegrity(scriptTag.Integrity, scriptTag.Src)); }
public async Task IdentityUI_ScriptTags_SubresourceIntegrityCheck(ScriptTag scriptTag) { var sha256Integrity = await GetShaIntegrity(scriptTag, SHA256.Create(), "sha256"); Assert.Equal(scriptTag.Integrity, sha256Integrity); }
public async System.Threading.Tasks.Task <ActionResult> auth() { DB list = new DB(); string code = Request.QueryString["code"]; string myShopifyUrl = Request.QueryString["shop"]; /*TRY-Catch*/ try { string accessToken = await AuthorizationService.Authorize(code, myShopifyUrl, API, Secret); Session["token"] = accessToken; var qs = Request.QueryString.ToKvps(); if (AuthorizationService.IsAuthenticRequest(qs, Secret)) { Session["shop"] = myShopifyUrl; if (db.ShopLinks.Where(w => w.Shop == myShopifyUrl).Count() > 0) { list.shopLinks = db.ShopLinks.ToList(); if (db.ShopLinks.Where(w => w.Shop == myShopifyUrl).FirstOrDefault().IsPremium == true) { Session["premium"] = true; } list.users = db.Users.ToList(); } else { ShopLink NewShop = new ShopLink(); NewShop.Shop = myShopifyUrl; NewShop.Token = accessToken; NewShop.InstallDate = DateTime.Now; NewShop.IsPremium = false; NewShop.SendLimit = 500; db.ShopLinks.Add(NewShop); db.SaveChanges(); list.shopLinks = db.ShopLinks.ToList(); list.users = db.Users.ToList(); var service = new ScriptTagService(myShopifyUrl, accessToken); var tag = new ScriptTag() { Event = "onload", Src = "https://www.leaderpush.com/public/scripts/main.js", }; tag = await service.CreateAsync(tag); } } else { //Request is not authentic and should not be acted on. return(Content("Error, Please Try Again...")); } return(View(list)); } catch { return(RedirectToAction("UrlBreak", "Home")); } }
private void ProcessScriptTag(ScriptTag obTag) { }